【问题标题】:different padding, if next div has same class as previous不同的填充,如果下一个 div 与前一个具有相同的类
【发布时间】:2017-02-15 21:01:40
【问题描述】:

除了:last-child:last-child之外,我想为具有相同班级的兄弟姐妹提供较低的底边距

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

【问题讨论】:

  • 这可能有效.text{ margin-bottom:20px; } .text.myText{ margin-bottom:5px; } .text + .text:not(.myText){ margin-top:20px; }

标签: css sass css-selectors styles


【解决方案1】:

由于 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>

这是我能找到的最接近你的地方。而且看起来你的逻辑并不一致。

【讨论】:

  • 我不能使用margin-bottom,而不是margin-top吗?我只是想覆盖 myText css 的 margin-bottom,因为所有的边距都是我们的聊天应用程序的默认设置。不想通过改变其他任何东西来破坏它..
  • @Basit 我理解,但正如我所说的原因,CSS 只能从左上角到右下角。它不能在对面旅行,所以很遗憾没有。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
相关资源
最近更新 更多