CSS3 给我们新增了选择器,可以更加便捷,更加自由的选择目标元素。
- 属性选择器
- 结构伪类选择器
- 伪元素选择器
属性选择器
- 属性选择器可以根据元素特定的属性来选择元素,这样就可以不用借助于类或者id选择器
选择符 | 简介 |
---|---|
E[att] | 选择具有att属性的E元素 |
E[att=“val”] | 选择具有att属性且属性值等于val的E元素 |
E[att^=“val”] | 匹配具有att属性且值以val开头的E元素 |
E[att$=“val”] | 匹配具有att属性且值以val结尾的E元素 |
E[att*=“val”] | 匹配具有att属性且值中含有val的E元素 |
利用属性选择器就可以不借助于类或者id选择器
<head> <style> input[value] { color: pink; } </style> </head> <body> <!-- 1.利用属性选择器可以不借助类或者id选择器 --> <input type="text" value="请输入用户名"> <input type="text"> </body>
本篇文章所有代码不适合给出结果演示
属性选择器还可以选择 属性 = 值的某些元素
<head> <style> input[type=text] { color: pink; } </style> </head> <body> <!-- 2.属性选择器还可以选择 属性=值的某些元素 --> <input type="text" name="" id=""> <input type="password" name="" id=""> </body>
属性选择器可以选择属性值开头的某些元素
<head> /* 选择首先是div,然后具有class属性,并且是icon开头的值 */ <style> div[class^=icon] { color: pink; } </style> </head> <body> <!-- 3.属性选择器可以选择属性值开头的某些元素 --> <div class="icon1">小图标1</div> <div class="icon2">小图标2</div> <div class="icon3">小图标3</div> <div class="icon4">小图标4</div> </body>
属性选择器可以选择属性值结尾的某些元素
<head> <style> section[class$=data] { color: pink; } </style> </head> <body> <!-- 4.属性选择器可以选择属性值结尾的某些元素 --> <section class="icon1-data">1</section> <section class="icon2-data">2</section> <section class="icon3-data">3</section> </body>
类选择器,属性选择器,伪类选择器,权重为10
结构伪类选择器
- 结构伪类选择器主要根据文档结构来选择元素
- 常用于根据父级选择器选择里面的子元素
选择符 | 简介 |
---|---|
E:first-child | 匹配父元素中的第一个子元素E |
E:last-child | 匹配父元素中最后一个E元素 |
E:nth-child(n) | 匹配父元素中的第n个子元素E |
E:first-of-type | 指定类型E的第一个 |
E:last-of-type | 指定类型E的最后一个 |
E:nth-of-type(n) | 指定类型E的第n个 |
E:first-child 和E:last-child
<head> <style> /* 1.选择ul里面的第一个孩子 小li */ ul li:first-child { background-color: pink; } /* 2.选择ul里面的最后一个孩子 小li */ ul li:last-child { background-color: pink; } </style> </head> <body> <ul> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ul>
nth-child(n)
nth-child(n)
选择某个父级元素的一个或多个特定的子元素(重点)
- n可以是数字,关键字和公式
- n如果是数字,就是选择第n个子元素,里面数字从1开始
- n可以是关键字:even 偶数,odd奇数
- n可以是公式:常见的公式如下(如果n是公式,则从0开始计算,但是第0个元素或者超出了元素的个数会被忽略)
公式 | 取值 |
---|---|
2n | 偶数(等价于even) |
2n+1 | 奇数(等价于odd) |
5n | 5 10 15 …(5的倍数) |
n+5 | 从第5个开始(包含第五个)到最后 |
-n+5 | 前5个(包含第5个) |
<head> <style> /* 选择ul里面的第2个孩子 小li */ ul li:nth-child(2) { background-color: pink; } </style> </head> <body> <ul> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ul> </body>
<style> /* 1.把所有的偶数 even的孩子选出来 */ ul li:nth-child(even) { background-color: #ccc; } /* 2.把所有的奇数 odd的孩子选出来 */ ul li:nth-child(odd) { background-color: gray; } /* 3.nth-child(n) 从0开始 每次加1 往后面计算 这里面必须是n 不能是其他的字母 选择了所有的孩子*/ /* ol li:nth-child(n) { background-color: pink; } */ /* 4.nth-child(2n)母选择了所有的偶数孩子 等价于 even*/ /* ol li:nth-child(2n) { background-color: pink; } ol li:nth-child(2n+1) { background-color: skyblue; } */ /* ol li:nth-child(n+3) { background-color: pink; } */ ol li:nth-child(-n+3) { background-color: pink; } </style> </head> <body> <ul> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ul> <ol> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ol> </body>
E:first-of-type和E:last-of-type
E:first-of-type | 指定类型E的第一个 |
---|---|
E:last-of-type | 指定类型E的最后一个 |
<head> <style> ul li:first-of-type { background-color: pink; } ul li:last-of-type { background-color: pink; } ul li:nth-last-child(2) { background-color: pink; } </style> </head> <body> <ul> <li>我是第1个孩子</li> <li>我是第2个孩子</li> <li>我是第3个孩子</li> <li>我是第4个孩子</li> <li>我是第5个孩子</li> <li>我是第6个孩子</li> <li>我是第7个孩子</li> <li>我是第8个孩子</li> </ul> </body>
nth-child(n)
和nth-of-type(n)
区别
- nth-child 对父元素里面所有孩子排序选择(序号是固定的),先找到第n个孩子,然后看看是否和E匹配
- nth-of-type 对父元素里面指定子元素进行排序选择,先去匹配E,然后再根据E 找第n个孩子
伪元素选择器
- 伪元素选择器可以帮我们利用CSS创建新标签元素,而不需要HTML标签,从而简化HTML结构
选择符 | 简介 |
---|---|
::before | 在元素内部的前面插入内容 |
::after | 在元素内部的后面插入内容 |
注意:
- before 和 after 创建一个元素,但是是属于行内元素
- before和after 都是一个盒子,都作为父元素的孩子
- 新创建的这个元素在文档树中是找不到的,所以我们称为伪元素
before是放在内容的前面,after是放在了内容的后面
<head> <style> div { width: 200px; height: 200px; background-color: pink; } /* div::before 权重是2 */ div::before { /* 这个content是必须要写的 */ content: '我'; } div::after { content: '小猪佩奇'; } </style> </head> <body> <div> 是 </div>
- before 和 after 必须有 content 属性
- before 在父元素内容的前面创建元素 ,after 在父元素内容的后面插入元素
- 伪元素选择器 和 标签选择器 一样,权重为1
CSS3盒子模型
- CSS3 中可以通过
box-sizing
来指定盒模型 - 有2个值:这样我们计算盒子大小的方式就发生了改变
content-box
border-box
content-box
box-sizing: content-box;
- 第一种情况是 CSS 的盒子模型,盒子大小为 width + padding + border
- 此种情况盒子大小为 宽度 + 内边距 + 边框,这也是我们之前写盒子所默认的
border-box
box-sizing: border-box;
- 第二种情况是 CSS3 的盒子模型,盒子大小为 width
- 此种情况盒子大小为 宽度,不包括内边距和边框,这样 padding 和 border 就不会撑大盒子了(前提是 padding 和 border 不会超过 width 宽度)
- 我们可以在以后的 css 通配符中添加 CSS3 盒子模型
* { margin: 0; padding: 0; box-sizing: border-box; /* 这样的话padding和border就不会撑大盒子了 */ }
calc函数
calc()
此CSS函数让你在声明CSS属性值时执行一些计算(计算盒子宽度 width: calc 函数)
width:calc(100% - 80px); 括号里面可以使用 + - * / 来进行计算
<head> <style> .father { width: 300px; height: 200px; background-color: pink; } .son { /* width: 150px; */ /* son盒子和父亲一样宽,都是100%,son盒子-30px */ width: calc(100%-30px); height: 30px; background-color: skyblue; } </style> </head> <body> <!-- 需求:我们的子盒子宽度永远比父盒子小30像素 --> <div class="father"> <div class="son"></div> </div> </body>