簡體   English   中英

CSS:最后一個孩子壓倒一切

[英]CSS :last-child overriding all

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;                                             
}    

我的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 那些div元素的:last-child 相反,您需要將這些偽類選擇器放在div元素本身上:

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

每個.item都位於它自己的.col-md-4元素中,因此你無法用:first-child方式定位它們:first-child:last-child .item應該在一個容器內,或者你應該以.col-md-4元素為目標。 例如:

.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;  
}

您的.item元素是唯一的,因此它們的父元素中的最后一個子元素。 為了實現我認為您正在尋找的東西,您需要:

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

這不是一個建議的解決方案,因為您應該嘗試遠離使用框架類作為樣式的一部分,但是通過對標記的一些小更新,您可以獲得相同的結果。

這是因為在范圍div.item ,每個項目被認為是第一個最后一個孩子。

改為:

.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