【发布时间】:2023-03-27 05:28:02
【问题描述】:
我不想使用 !important 或为每个可以使用辅助类处理的案例编写新规则。比如我想做;
HTML
<body id="sameId">
<div class="wrapper">
<div class="article text-center text-bold">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
<div class="article">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
<div class="article">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
<div class="article">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
<div class="article">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
<div class="article">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
</div>
</body>
CSS
/* Helper Classes */
#sameId .text-center {
text-align:center;
}
#sameId .text-bold {
font-weight: bold;
}
/* Article Styles */
.wrapper .article {
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ddd;
text-align:left;
font-weight: normal;
}
在所有页面的正文标签上使用相同的 id 以提高 css 特异性是否会导致将来出现问题?
【问题讨论】:
-
您在
.wrapper .article中显式设置text-align和font-weight是否有原因?如果它们没有被 CSS 前面的其他规则覆盖,您可以简单地 omit them。
标签: html css semantics markup css-specificity