html - 从 CSS 中的所有选择器中排除一个类

标签 html css css-selectors

首先抱歉我的英语不好。 我想从 CSS 的所有选择器中排除一个类。

我需要使用类似的东西

(h1, h2, h3, h4, ..., p, span, a, ...):not(.exclude){
   font-family: arial;
}

代替:

h1:not(.exclude), h2:not(.exclude), ..., a:not(.exclude){
   font-family: arial;
}

最佳答案

使用 CSS4 是可能的 :is选择器。您可以在此处注意浏览器支持:https://caniuse.com/?search=is但用现代网络开发人员的术语来说,您可以安全地使用它。

:is(h1,h2,h3):not(.exclude) {
  background: #bada55;
  color: #565656;
  padding: 10px;
}
<h1 class="exclude">
  Excluded
</h1>
<h2 class="include">
  Included
</h2>
<h3 class="exclude">
  Excluded
</h3>

上面的代码是这样的

h1:not(.exclude), h2:not(.exclude), h3:not(.exclude) { Properties... }

您还可以使用 :where做同样的事情:

:where(h1,h2,h3):not(.exclude) {
  background: #bada55;
  color: #565656;
  padding: 10px;
}
<h1 class="exclude">
  Excluded
</h1>
<h2 class="include">
  Included
</h2>
<h3 class="exclude">
  Excluded
</h3>

关于html - 从 CSS 中的所有选择器中排除一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67456250/

相关文章:

html - 为什么我的属性选择器不起作用?

html - 奇怪的 SVG 动画行为

html - CSS链接到硬盘上的文件?

单击时隐藏/显示的 javascript - 想再次添加单击时隐藏

css - 如何通过 laravel mix 将两个 SASS 文件合并到一个 CSS 文件中?

html - 使用 CSS 选择器选择最后一个元素

css - :last-child does not select the last element

html - 100% 宽度行中的不同尺寸图像

html - 链接与图像大小不匹配

javascript - HTML/CSS/JavaScript : Can't click slideshow indicator button