从零开始前端学习[29]:Css3中新增加的选择器一

来源:互联网 发布:淘宝pc端搭配套餐 编辑:程序博客网 时间:2024/06/07 01:24

Css3中新增加的选择器

  • 以前的选择器
  • 新增加的选择器

提示
博主:章飞_906285288
博客地址:http://blog.csdn.net/qq_29924041


以前的选择器

通配符选择器

*{margin:10px auto;padding:10px}

ID选择器:

#main{width:100px;height:100px;background:blue;}

class类选择器

.main{width:1200px;height;50px;background:green;}

标签选择器

a{color:red;font:bold 22px/30px ""}

组合选择器

.main div #box1{width:100px;height:100px;background:#152345}

后代选择器

.main .p_style{color:white;font:bolder 18px/22px "";width:100px;height:22px} 

子代选择器

.main>p{width:100px;height:100px;box-shadow:0 0 10p 0 blue}

毗邻选择器

div+p{height:100px;width:100px;border:1px solid green;}

属性选择器

E[attr=val][attr]{height:100px;width:100px;border:1px solid green;}div[class~="somebody"]{height: 100px; width: 100px;} //属性包含选择器

伪类选择器

div:hover{height: 100px; width: 100px; background: deeppink;}

新增加的选择器

关联选择器

E1~E2(选择E1后面紧邻的E2元素)//注意与毗邻选择器的区别其会跳过中间其他的,而毗邻选择器如果遇到下一个标签不是,则终止 .box1 p~p{ background: deeppink; }
<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <meta charset="UTF-8"><!--申明当前网页的编码集UTF-8-->  <meta name="Generator" content="EditPlus®">   <!--编辑器的名称-->  <meta name="Author" content="作者是谁">         <meta name="Keywords" content="关键词">  <meta name="Description" content="描述和简介">  <style type="text/css">                                                body,dl,dd,dt,p,h1,h2,h3,h4,h5,h6{ margin: 0;}        ul,ol{margin: 0; list-style: none; padding: 0;}        a{ text-decoration: none; }        *{ margin: 0; padding: 0; }    .main{width: 1200px ;margin: 20px auto}    .main div{width: 200px;margin: auto;box-shadow: 0 0 10px 0 #00dd00}    .main div p,.main div div{width: 40px;height: 40px;box-shadow: 0 0 10px 0 #000;margin: auto}    .main .box1 p~p{background: greenyellow}  /**关联选择器*/    .main .box2 p+p{background: greenyellow}  /**毗邻选择器*/  </style></head><body><div class="main">  <div class="box1">    关联选择器    <p></p>    <p></p>    <p></p>    <p></p>    <div></div>    <p></p>  </div>  <div class="box2">    毗邻选择器    <p></p>    <p></p>    <p></p>    <p></p>    <div></div>    <p></p>  </div></div></body></html>

显示如下:这里写图片描述


模糊包含选择器

模糊包含也就是类似于我们的正则表达式:
1:特殊符号^,表示以什么开头

^="text" 以text作为开头的类选择器p[class^="text"]{ height: 100px; width: 100px; background: deeppink; }

2:特殊符号$表示以什么结尾

$="text" 以text作为结尾的选择器p[class$="text"]{ height: 100px; width: 100px; background: deeppink; }

3:特殊符号*,表示包含什么

*="text"只要包含text的,p[class$="text"]{ height: 100px; width: 100px; background: deeppink; }

注意这三种运算符与属性运算符之间的区别,如果属性运算符E[class~=”aaaa”],aaaa表示的是一个完整的类名,而上述的三种其实都是在类名字符串中所做的一些模糊匹配

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <meta charset="UTF-8"><!--申明当前网页的编码集UTF-8-->  <meta name="Generator" content="EditPlus®">   <!--编辑器的名称-->  <meta name="Author" content="作者是谁">         <meta name="Keywords" content="关键词">  <meta name="Description" content="描述和简介">  <style type="text/css">                                                body,dl,dd,dt,p,h1,h2,h3,h4,h5,h6{ margin: 0;}        ul,ol{margin: 0; list-style: none; padding: 0;}        a{ text-decoration: none; }        *{ margin: 0; padding: 0; }    .main{width: 1200px ;margin: 20px auto}    .main div{width: 200px;margin: auto;box-shadow: 0 0 10px 0 #00dd00}    .main div p,.main div div{width: 40px;height: 40px;box-shadow: 0 0 10px 0 #000;margin: auto}    .main .box1 p[class^="head"]{background: greenyellow} /*  ^表示以head开头的模糊匹配*/    .main .box1 p[class$="footer"]{background: deeppink} /*  $表示以footer结尾的模糊匹配*/    .main .box1 p[class*="middle"]{background: blue}   /*  *表示包含middle的类*/    .main .box1 p[class~="p1_1"]{background: brown}   /*表示属性中必须是一个完整的名字*/  </style></head><body><div class="main">  <div class="box1">    模糊匹配选择器    <p class="headtext1"></p>    <p class="textfooter"></p>    <p class="textmiddlefooter"></p>    <div></div>    <p class="p1 p1_1 p1_2"></p>    <p></p>  </div></div></body></html>

这里写图片描述


新增加的伪类选择器:first-of-type,last-of-type,only-of-type,nth-of-type(n),nth-last-of-type

first-of-type主要是筛选出父类中所有相同的子元素,然后选出其中的第一个

p:first-of-type{background:blue};从父元素中筛选出所有的p元素,然后将第一个赋值

last-of-type主要是筛选出所有相同的子元素,然后对最后一个进行赋值

p:last-of-type{background:green}筛选出父级中所有的p元素,然后对最后一个进行赋值

only-of-type主要筛选出父级元素中所有的标签,判断里面是否是唯一的,如果是唯一的则选出,如果不是唯一的,则不会选中

pre:only-of-type{background:deeppink}筛选出父级中唯一的pre标签

nth-of-type(n);按照顺序的方式, 主要是筛选出所有相同的子元素,然后选中其中的第n个元素,注意下表从1开始

p:nth-of-type(3){background:black;}按照顺序的方式,主要是筛选出所有相同的子元素,然后选中其中的第3个p元素

nth-last-of-type(n);按照逆序的方式,主要是筛选出所有相同子元素,然后按照逆序的形式选中其中的第n个元素,注意下标从1开始

p:nth-last-of-type(3){background:gold;}按照逆序的方式,主要是筛选出所有相同的子元素,然后选中其中的第3个p元素

代码如下所示:

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <meta charset="UTF-8"><!--申明当前网页的编码集UTF-8-->  <meta name="Generator" content="EditPlus®">   <!--编辑器的名称-->  <meta name="Author" content="作者是谁">         <meta name="Keywords" content="关键词">  <meta name="Description" content="描述和简介">  <style type="text/css">                                                body,dl,dd,dt,p,h1,h2,h3,h4,h5,h6{ margin: 0;}        ul,ol{margin: 0; list-style: none; padding: 0;}        a{ text-decoration: none; }        *{ margin: 0; padding: 0; }    .main{width: 1200px ;margin: 20px auto}    .main div{width: 200px;margin: auto;box-shadow: 0 0 10px 0 #00dd00}    .main div p,.main div div,.main div pre{width: 40px;height: 40px;box-shadow: 0 0 10px 0 #000;margin: auto}    .main div p:first-of-type{background: green}    .main div p:last-of-type{background: blue}    .main .box1 pre:only-of-type{background: deeppink}    .main div p:nth-of-type(5){background: gold}    .main div p:nth-last-of-type(3){background: darkmagenta}  </style></head><body><div class="main">  <div class="box1">    first-of-type<br>    last-type-of<br>    only-of-type    <div>1</div>    <p >2</p>    <p >3</p>    <p >4</p>    <pre>5</pre>    <p >6</p>    <p>7</p>    <div>8</div>    <p >9</p>    <p>10</p>    <p >11</p>    <p>12</p>  </div></div></body></html>

显示如下所示:

这里写图片描述


伪类选择器child系列之only-child,last-child,nth-child,nth-last-child;

首先child是子类的意思,这组伪类选择器也就是选中在某个位置的子类,注意是从1开始的下标,子类标签对应的位置和其伪类对应都要相互对应,中间不能包括文字或其他标签

only-child排列在第一个位置的标签的选择

  .main div div:first-child{background: green}  注意这里的div必须是排列在父类中的第一个元素

last-child排列在最后一个元素

 .main div p:last-child{background: darkmagenta}  注意这里的p必须是排列在父类中的最后一个元素

nth-child(n)

.main div p:nth-child(3){background: blue}注意这里的p必须是排列在父类中的第三个元素,并且元素对应的标签是p

nth-last-child(n)

.main .box1 pre:nth-last-child(8){background: deeppink}注意这里的p必须是排列在父类中的倒数第8个元素,并且元素对应的标签是pre

实例代码如下所示:

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <meta charset="UTF-8"><!--申明当前网页的编码集UTF-8-->  <meta name="Generator" content="EditPlus®">   <!--编辑器的名称-->  <meta name="Author" content="作者是谁">         <meta name="Keywords" content="关键词">  <meta name="Description" content="描述和简介">  <style type="text/css">                                                body,dl,dd,dt,p,h1,h2,h3,h4,h5,h6{ margin: 0;}        ul,ol{margin: 0; list-style: none; padding: 0;}        a{ text-decoration: none; }        *{ margin: 0; padding: 0; }    .main{width: 1200px ;margin: 20px auto}    h3{text-align: center}    .main div{width: 200px;margin: auto;box-shadow: 0 0 10px 0 #00dd00;text-align: center}    .main div p,.main div div,.main div pre{width: 40px;height: 40px;box-shadow: 0 0 10px 0 #000;margin: auto}    .main div div:first-child{background: green}    .main div p:nth-child(3){background: blue}    .main .box1 pre:nth-last-child(8){background: deeppink}    .main div p:last-child{background: darkmagenta}  </style></head><body><div class="main">  <h3>first-child, last-child, nth-child,nth-last-child</h3>  <div class="box1">    <div>1</div>    <p >2</p>    <p >3</p>    <p >4</p>    <pre>5</pre>    <p >6</p>    <p>7</p>    <div>8</div>    <p >9</p>    <p>10</p>    <p >11</p>    <p>12</p>  </div></div></body></html>

这里写图片描述

以上就是关于新增加的一些伪类选择器

欢迎持续访问博客

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 清理空调时湿纸巾被卷进去怎么办 超市买的尿不湿质量太差怎么办 把卫生巾和衣服一起洗了怎么办 全面屏面对vo华为手机怎么办屏 雅漾喷雾的喷头坏了怎么办 悦诗风吟水里面有小颗粒怎么办 林肯mkz钥匙锁在车内怎么办 八四消毒液弄到衣服上怎么办 微信朋友圈发过的文章想修改怎么办 白色衣服被洗衣液染色了怎么办 准迁证和迁移证不想迁了怎么办 出了迁移证又想迁到其他地方怎么办 高中的会考如果没g合格怎么办 鞋子里自带的鞋垫坏了怎么办 入厕纸把私处伤了一下怎么办? 夏天做月子用姨妈巾热怎么办 涂了痔疮膏后怕粘到内裤怎么办 眼罩里面的蓝色液体干了怎么办 新买的饮水机有塑料味怎么办 白色衣服被洗衣液染荧光了怎么办 衣服碰到了酒店的毛巾被单怎么办 防晒喷雾弄衣服上有荧光怎么办 剑网3重置版删除后有残留怎么办 在超市买到变质的水果怎么办 微信官方电话一直打不通怎么办 对方欠货款股东换了不还怎么办 闲鱼买的东西确认收货有问题怎么办 不让微信好友看到吃鸡的名字怎么办 金鹰贵宾积分卡过期了怎么办 小宝机器人一直停在联网界面怎么办 手机版的有道云笔记忘记邮箱怎么办 钡灌肠复查钡剂排空不良怎么办 两个月宝宝灌肠后不排便怎么办 一岁宝宝肠套叠灌肠后拉肚子怎么办 苹果手机自带的天气没有了怎么办? 衣服在洗衣机里忘记拿出来怎么办 苹果se手机系统占内存太大怎么办 客人把饭店老板打了民警怎么办 商场嫌品牌低端不让入驻怎么办 带着孩子坐飞机座位不在一起怎么办 公司老板跑路了员工该怎么办