简体   繁体   English

CSS:最后一个孩子压倒一切

[英]CSS :last-child overriding all

The css with :last-child is overriding all other. css with:last-child覆盖了所有其他的。

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

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

My html looks like is the following : 我的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>

Now every element with the class "item" will be overriden by :last-child. 现在,具有“item”类的每个元素都将被覆盖:last-child。 Where is my problem? 我的问题在哪里? :/ :/

The problem is that your .item elements are wrapped in div elements. 问题是你的.item元素包含在div元素中。 They are always both the :first-child and :last-child of those div elements. 它们总是 :first-child 那些div元素的:last-child Instead you need to place those pseudo-class selectors on the div elements themselves: 相反,您需要将这些伪类选择器放在div元素本身上:

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

Each .item is located within it's own .col-md-4 element, so there is no way you can target them with :first-child or :last-child . 每个.item都位于它自己的.col-md-4元素中,因此你无法用:first-child方式定位它们:first-child:last-child .item s should either be within one container or you should target .col-md-4 elements instead. .item应该在一个容器内,或者你应该以.col-md-4元素为目标。 For example: 例如:

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

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

Your .item elements are the only, and therefore last children within their parent elements. 您的.item元素是唯一的,因此它们的父元素中的最后一个子元素。 To achieve what I think you are looking for you need: 为了实现我认为您正在寻找的东西,您需要:

.service-teaser .col-md-4 .item {                                      
    margin-left: 50px;                                              
    margin-right: 0px;                                             
} 

This would not be an advised solution as you should try to stay away from using framework classes as part of your styling, but with some small updates to markup you could achieve the same result. 这不是一个建议的解决方案,因为您应该尝试远离使用框架类作为样式的一部分,但是通过对标记的一些小更新,您可以获得相同的结果。

This is because in the scope of div.item , each item is considered to be both the first and last child. 这是因为在范围div.item ,每个项目被认为是第一个最后一个孩子。

Do this instead: 改为:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM