百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>CSS教程> 5分钟读懂css伪类选中器:is:not
分享文章到:

5分钟读懂css伪类选中器:is:not

发布时间:09/01 来源:未知 浏览: 关键词:
本文介绍了Css伪类::is和:not的用途,并解释了is、not、matches、any之间的关系。:not单从名字上我们应当能对它有大约的认知,非选中,排除括号内的其它元素。:is和matches本质上是一个东西,用途完全同样。 本文介绍一下Css伪类:is和:not,并解释一下is、not、matches、any之间的关系

:not

The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class.

以上是MDN对not的解释

举荐学习:CSS视频教程

单从名字上我们应当能对它有大约的认知,非选中,排除括号内的其它元素

最简略的例子,用CSS将div内,在不转变html的条件下,除了P标签,其它的字体色彩酿成蓝色,

    我是蓝色
    

我是玄色

我是蓝色

我是蓝色

我是蓝色

我是蓝色

我是蓝色

以前的做法

div span,div h2,div h3, div h4,{
  color: blue;
}

not写法

div:not(p){
  color: blue;
}

从上面的例子可以显明体味到not伪类选中器的作用

下面升级一下,问:将div内除了span和p,其它字体色彩变蓝色

div:not(p):not(span){
  color: blue;
}

还有更为简约的办法,如下,但是当前兼容不太好,不倡议运用

div:not(p,span){
  color: blue;
}

兼容

除IE8,当前所有主流阅读器都支撑,可以安心运用

:is

The :is() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list. This is useful for writing large selectors in a more compact form.

以上是MDN的解释

在说is前,需要先理解一下matches

matches跟is有哪些关系?

matches是is的前世,但是本质上的确一个东西,用途完全同样

matches这个单词意思跟它的作用非常匹配,但是它跟not作用刚好相反,作为not的对峙面,matches这个次看起来的确扞格难入,并且单词不足简约,所以它被改名了,这里还有一个issue https://github.com/w3c/csswg-drafts/issues/3258,也就是它改名的泉源

好了,此刻晓得matches和is其实是一个东西,那么is的用途是如何的呢?

举例:将header和main下的p标签,在鼠标hover时文字变蓝色

  • 鼠标放上去变蓝色

  • 鼠标放上去变蓝色

正常字体

  • 鼠标放上去变蓝色

  • 鼠标放上去变蓝色

  • 正常字体

  • 正常字体

  • 正常字体

以前的做法

header ul p:hover,main ul p:hover{
  color: blue;
}

is写法

:is(header, main) ul p:hover{
  color: blue;
}

从上面的例子大约能看出is的摆布,但是并没有完全表现出is的强大之处,但是中选择的内容变多之后,特殊是那种层级较多的,会发明is的写法有多简约,拿MDN的一个例子看下

以前的写法

/* Level 0 */
h1 {
  font-size: 30px;
}
/* Level 1 */
section h1, article h1, aside h1, nav h1 {
  font-size: 25px;
}
/* Level 2 */
section section h1, section article h1, section aside h1, section nav h1,
article section h1, article article h1, article aside h1, article nav h1,
aside section h1, aside article h1, aside aside h1, aside nav h1,
nav section h1, nav article h1, nav aside h1, nav nav h1 {
  font-size: 20px;
}

is写法

/* Level 0 */
h1 {
  font-size: 30px;
}
/* Level 1 */
:is(section, article, aside, nav) h1 {
  font-size: 25px;
}
/* Level 2 */
:is(section, article, aside, nav)
:is(section, article, aside, nav) h1 {
  font-size: 20px;
}

可以看出,随着嵌套层级的添加,is的优势越来越显明

说完了is,那就必需相识一下any,前面说到is是matches的替换者,

any跟is又有哪些关系呢?

是的,is也是any的替换品,它解决了any的一些毛病,比方阅读器前缀、选中机能等

any作用跟is完全同样,独一不一样的是它需要加阅读器前缀,用途如下

:-moz-any(.b, .c) {
}
:-webkit-any(.b, .c) { 
}

结论

通过上面的介绍大约讲述了css伪类is,not,matches,any它们三者的关系

is+not组合是大势所趋

本文来自百分百源码网,CSS教程栏目,欢送学习

以上就是5分钟读懂css伪类选中器:is :not的细致内容,更多请关注 百分百源码网 其它相干文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有151人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板