当我需要将页边距底部设置为新值时,它将被覆盖

Margin bottom is being overridden when I need it to be set to a newer value

本文关键字:新值时 覆盖 底部 页边距 设置      更新时间:2023-09-26

我有以下两种样式:

.no-bottom-margin{margin-bottom:0;}
.tab-content{border:1px solid #000;margin-bottom:1em;}

在我的页面上,我做了以下操作:

<div class="tab-content no-bottom-margin">
     Some data here..
</div>
<div class="tab-content">
   Some data here..
</div>

底部边距没有从第一个div中删除,我不明白为什么?当我看到Fire Bug时,然后底部边距:0;被覆盖。我需要将边距设置为0。我该怎么做?

这个示例看起来很愚蠢,但我的视图中有将样式tab-content添加到div的逻辑,但如果它是视图中唯一的div,那么我不希望有边距,那么我的设计看起来就不一致。

您可以尝试制作您的第一个CSS模型:

.tab-content.no-bottom-margin {
  margin-bottom: 0;
 }

组合类将增加它的特异性。

此外,您可以重新排序规则。没有底部边距是第二个-在CSS中,最后一个规则获胜。

.no-bottom-margin {
    margin-bottom: 0 !important;
}