不常用的伪类 :is() 和 :where()

902天前1796

以前写CSS时我们可能会用到很长的选择器,您想对很多标签进行颜色调整,我们都是这样写的:

h1 > b, h2 > b, h3 > b, h4 > b, h5 > b, h6 > b {
  color: hotpink;
}

现在可以使用:is()缩减并提高其可读性:

:is(h1,h2,h3,h4,h5,h6) > b {
  color: hotpink;
}

:is()由:match()改名而来,其兼容性如下:

:where()伪类和其功能相同,仅选择器权重不同,下面是几个例子:


/* at the beginning */
:where(h1,h2,h3,h4,h5,h6) > b {
  color: hotpink;
}

/* in the middle */
article :is(header,footer) > p {
  color: gray;
}

/* at the end */
.dark-theme :where(button,a) {
  color: rebeccapurple;
}

/* multiple */
:is(.dark-theme, .dim-theme) :where(button,a) {
  color: rebeccapurple;
}

/* stacked */
:is(h1,h2):where(.hero,.subtitle) {
  text-transform: uppercase;
}

/* nested */
.hero:is(h1,h2,:is(.header,.boldest)) {
  font-weight: 900;
}

* 若非特殊说明,本站文章均为博主原创,码字不易,如需转载,请注明出处!有疑问可留言交流,谢谢。

CSS0 

不常用的伪类 :is() 和 :where() - Jdeal | Life is like a Design.