您当前的位置:首页 > 计算机 > 编程开发 > Html+Div+Css(前端)

css中 :not() 伪类选择器用法

时间:12-14来源:作者:点击数:

单条件筛选

单个的not写法:

/*选中非段落元素*/
:not(p)
{
 
}

在前端项目开发过程中,如果希望某个样式不作用到选择器上,可以使用:not(选择器),如:

<input type="text" value="1" />
<input type="text" value="2" />
<input type="text" class="no-red" value="3"/>
input[type="text"] {
    display: block;
    width: 300px;
    height: 30px;
    margin-bottom: 20px;
    padding-left: 10px;
    color: red;
}

这样写效果如下:

在这里插入图片描述

如果希望input[type=“text”]的样式不作用到第三个input上,可以这样写:

 input[type="text"]:not(.no-red) {
    display: block;
    width: 300px;
    height: 30px;
    margin-bottom: 20px;
    padding-left: 10px;
    color: red;
}

则效果如图所示:

在这里插入图片描述

多条件筛选

多个not的写法

/*选中div里面非首个、非最后一个的中间p元素*/

div p:not(:first-child):not(:last-child){
}

:not 伪类选择器可以通过多条件筛选不符合表达式的元素。

table tbody tr:not(:first-child):not(:last-child) td
{
     text-align: right;
}

以上代码可以选择table表格中tbody部分非首个、非最后一个的tr,并设置其子元素td文本样式居右。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐