如果在特定元素中则忽略 CSS 规则

发布于 2025-01-10 15:50:00 字数 691 浏览 1 评论 0原文

假设我有一个名为 .root 的类,其中包含大量 CSS 规则,这些规则几乎适用于我的所有网站。

我还有一些“特殊”元素(包含在 .root 中),但我不想将 .root 规则应用于 中包含的任何内容>.special 元素。我不想“覆盖”所有根规则,只是不应用它们。

只用CSS可以吗?

谢谢你!

.root {
  color: blue;
  
  /* Lots of other styles */
}

.special {
  font-weight: bold;
}
<div class="root">
  This is blue
  
  <div class="special">
    <span>This is bold and blue, but I'd like this to be only bold</span>
  </div>
</div>

Let's say I have a class called .root which contains a lot of CSS rules which applies to almost all of my website.

I also have some "special" elements (included in the .root one), but I don't want to apply the .root rules to whatever is included in .special elements. I don't want to "override" all root rules, just not apply them.

Is it possible using only CSS?

Thank you!

.root {
  color: blue;
  
  /* Lots of other styles */
}

.special {
  font-weight: bold;
}
<div class="root">
  This is blue
  
  <div class="special">
    <span>This is bold and blue, but I'd like this to be only bold</span>
  </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2025-01-17 15:50:00

您可以使用值 property:initial 到一个属性来获得该属性的初始值

,或者您可以使用 all:initial 将选择器的所有值设置为其初始值值

https://developer.mozilla.org/fr/docs/Web/CSS /首字母

.root {
  color: blue;
  
  /* Lots of other styles */
}

.special {
  font-weight: bold;
  color: initial;
}
<div class="root">
  This is blue
  
  <div class="special">
    <span>This is bold and blue, but I'd like this to be only bold</span>
  </div>
</div>

you can use value property:initial to one property to the have the initial value of this property

or you can use all: initialto set all value of a selector to their inital value

https://developer.mozilla.org/fr/docs/Web/CSS/initial

.root {
  color: blue;
  
  /* Lots of other styles */
}

.special {
  font-weight: bold;
  color: initial;
}
<div class="root">
  This is blue
  
  <div class="special">
    <span>This is bold and blue, but I'd like this to be only bold</span>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文