2016-10-07 24 views
1

我想给下缘,底部具有同一类的兄弟姐妹,除了:last-child不同的填充,如果下次div有同一类以前

<style> 
    .text { 
     margin-bottom: 20px; 
    } 
</style> 
<div class="text"></div> 
<div class="text"></div> 
<div class="text myText"></div> <!-- margin-bottom this 5px; --> 
<div class="text myText"></div> <!-- margin-bottom this 5px; --> 
<div class="text myText"></div> <!-- margin-bottom this 20px; --> 
<div class="text"></div> 
<div class="text myText"></div> <!-- margin-bottom this 20px; --> 
<div class="text"></div> 
+0

这可能工作'.text {margin:bottom-20px; } .text.myText {margin} {margin:bottom} 5px; } .text + .text:not(.myText){ margin-top:20px; }' – Nikita

回答

2

由于CSS没有以前或家长选择,您可以通过这种方式使用margin-top

.myText + .myText {margin-top: 5px;} 
 
.text:not(.myText) + .myText, 
 
.myText + .text:not(.myText) {margin-top: 20px;} 
 

 
div {border: 1px solid #99f;} 
 

 
.text:not(.myText) + .myText:last-of-type, 
 
.text:not(.myText) + .myText:last-of-type {margin: 0;}
<div class="text">No Bot Margin</div> 
 
<div class="text">No Bot Margin</div> 
 
<div class="text myText">margin-bottom this 5px;</div> <!-- margin-bottom this 5px; --> 
 
<div class="text myText">margin-bottom this 5px;</div> <!-- margin-bottom this 5px; --> 
 
<div class="text myText">margin-bottom this 20px;</div> <!-- margin-bottom this 20px; --> 
 
<div class="text">No Bot Margin</div> 
 
<div class="text myText">margin-bottom this 20px;</div> <!-- margin-bottom this 20px; --> 
 
<div class="text">No Bot Margin</div>

这是最接近我可以帮你。此外,它看起来像你的逻辑不一致。

+1

我不能使用margin-bottom,而不是margin-top?我只想覆盖myText css的margin-bottom,因为我们的聊天应用程序的所有边距都是默认值。不想通过改变任何东西来打破它.. – Basit

+0

@Basit我明白了,但正如我刚才说的理由,CSS只能从左上角到右下角。它不能在对面旅行,所以不幸的是没有。 '(' –

相关问题