首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在同一个<ul>父元素中应用<li>中的不同样式?

如何在同一个<ul>父元素中应用<li>中的不同样式?
EN

Stack Overflow用户
提问于 2020-09-14 21:22:52
回答 4查看 65关注 0票数 2

我已经创建了一个包含3个<li><ul>列表。但我希望每个<li>都有不同的页边距-顶部。但是每当我通过给出不同的类名来应用边距来分隔<li>时,所有的<li>都会以那个边距而不是那个特定的<li>来下降。我更改了每个<li>的left-left属性,用类名选择它们,这样就行了,但是顶边距不起作用。有没有人能教我怎么做?下面是我的代码。

代码语言:javascript
复制
 .person-rating li{
    list-style: none;
    display: inline-block;
    width: 25%;
    border: 1px solid rgb(104, 69, 104);
    background-color: rgb(104, 69, 104);
    padding: 30px;
    border-radius: 8px;
代码语言:javascript
复制
    <div class="person-rating">
      <ul>
        <li class="new-1">
          <img src="images/image-anne.jpg">
          <h4>Colton Smith <br>
            Verified Buyer<br></h4>
          <p>"We needed the same printed design as the one we had ordered a week prior.
          Not only did they find the original order, but we also received it in time.
          Excellent!"</p>
        </li>
        <li class="new-2">
          <img src="images/image-colton.jpg">
          <h4>Irene Roberts <br>
          Verified Buyer<br></h4>
          <p>"Customer service is always excellent and very quick turn around. Completely
          delighted with the simplicity of the purchase and the speed of delivery."</p>
        </li>
        <li class="new-3">
          <img src="images/image-irene.jpg">
          <h4>Anne Wallace <br>
          Verified Buyer<br></h4>
          <p>"Put an order with this company and can only praise them for the very high
          standard. Will definitely use them again and recommend them to everyone!"</p>
        </li>
      </ul>
    </div>

EN

回答 4

Stack Overflow用户

发布于 2020-09-14 21:34:06

添加此CSS

li上添加垂直对齐的vertical-align:top;

代码语言:javascript
复制
.person-rating li.new-1{}
.person-rating li.new-2{margin-top:30px;}
.person-rating li.new-3{margin-top:60px;}

代码语言:javascript
复制
.person-rating li{
    list-style: none;
    display: inline-block;
    width: 25%;
    border: 1px solid rgb(104, 69, 104);
    background-color: rgb(104, 69, 104);
    padding: 30px;
    vertical-align:top;
    border-radius: 8px;
}
.person-rating li.new-1{}
.person-rating li.new-2{margin-top:30px;}
.person-rating li.new-3{margin-top:60px;}
代码语言:javascript
复制
<div class="person-rating">
      <ul>
        <li class="new-1">
          <img src="images/image-anne.jpg">
          <h4>Colton Smith <br>
            Verified Buyer<br></h4>
          <p>"We needed the same printed design as the one we had ordered a week prior.
          Not only did they find the original order, but we also received it in time.
          Excellent!"</p>
        </li>
        <li class="new-2">
          <img src="images/image-colton.jpg">
          <h4>Irene Roberts <br>
          Verified Buyer<br></h4>
          <p>"Customer service is always excellent and very quick turn around. Completely
          delighted with the simplicity of the purchase and the speed of delivery."</p>
        </li>
        <li class="new-3">
          <img src="images/image-irene.jpg">
          <h4>Anne Wallace <br>
          Verified Buyer<br></h4>
          <p>"Put an order with this company and can only praise them for the very high
          standard. Will definitely use them again and recommend them to everyone!"</p>
        </li>
      </ul>
    </div>

票数 2
EN

Stack Overflow用户

发布于 2020-09-14 21:40:18

您已经指定了.person-rating li {。因此,现在指定级联顺序的样式。

示例:

代码语言:javascript
复制
.person-rating li.new-1 { ... }

.person-rating li.new-2 { ... }

.person-rating li.new-3 { ... }

当然,在你的.person-rating li { }中也有vertical-align: top;

JsFiddle

票数 2
EN

Stack Overflow用户

发布于 2020-09-14 21:35:54

你可以在你的<li>上使用float: left;。然后,您将能够设置愿意的margin-top

警告:它会将您的<li>显示更改为display: block;。您可以显式地定义它,也可以让浏览器为您定义它。

更多细节:https://developer.mozilla.org/en-US/docs/Web/CSS/float

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63885218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档