html - CSS :last-child overriding all

标签 html css css-selectors

带有 :last-child 的 css 覆盖所有其他。

.service-teaser .item:first-child {                                     
    margin-left: 0px;                                               
    margin-right: 50px;
}                                                                       

.service-teaser .item:last-child {                                      
    margin-left: 50px;                                              
    margin-right: 0px;                                             
}    

我的 html 如下所示:

<div class="container service-teaser>
  <div class="row">
    <div class="col-md-4">
      <a href="#" class="item"></a>
    </div>
    <div class="col-md-4">
      <a href="#" class="item"></a>
    </div>
    <div class="col-md-4">
      <a href="#" class="item"></a>
    </div>
  </div>
</div>

现在每个类为“item”的元素都将被 :last-child 覆盖。我的问题在哪里? :/

最佳答案

问题是您的 .item 元素被包裹在 div 元素中。它们总是 :first-child :last-child div元素。相反,您需要将这些伪类选择器放在 div 元素本身上:

.service-teaser .row div:first-child .item { ... }
.service-teaser .row div:last-child .item { ... }

关于html - CSS :last-child overriding all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31433296/

相关文章:

javascript - 如何使用 CasperJS 将 html 数据表解析/映射到 JSON 对象?

javascript - 附加复选框值 jQuery

css - Angular Material 表 - 表内边框

javascript - 生成图像的复选框

css - 我在菜单中的元素似乎没有宽度?

css - 设置所有 Bootstrap 3 导航栏链接的样式,但 ul 下的链接除外

html - 在大屏幕(> 1920px)上居中一行的内容

HTML CSS 菜单 - 无法使子菜单内容全宽

javascript - 如何取消或重置特定的表单值?

html - 将类选择器与 ID 组合