1.css3属性选择器
注意!!!:类选择器、属性选择器、伪类选择器,权重为 10
属性选择器代码演示
<button disabled="disabled">按钮</button>
<button >按钮</button>
<input type="text" name="" id="" value="文本框">
<input type="search" name="" id="" value="搜索框">
<div class="icon1">图标1</div>
<div class="icon2">图标2</div>
<div class="icon3">图标3</div>
<div class="iicon3">图标4</div>
<div class="absicon">图标5</div>
<style>
button {
/*按钮放入变为小手状*/
cursor: pointer;
}
/*disabled是禁用*/
button[disabled] {
cursor: default
}
input[type="search"] {
color: pink;
}
/* 3. 以某个值开头的 属性值 */
div[class^="icon"] {
color: red;
}
/* 4. 以某个值结尾的 */
div[class$="icon"] {
color: green;
}
/* 5. 可以在任意位置的 */
div[class*="icon"] {
color: blue;
}
</style>
结构伪类选择器
- 属性列表
- 代码演示
//匹配第一个元素 ul li:first-child { background-color: lightseagreen; } //匹配最后一个元素 ul li:last-child { background-color: lightcoral; }
nth-child
参数详解
- nth-child 详解
- 注意:本质上就是选中第几个子元素
- n 可以是数字、关键字、公式
- n 如果是数字,就是选中第几个
- 常见的关键字有
even
偶数、odd
奇数 - 常见的公式如下(如果 n 是公式,则从 0 开始计算)
- 但是第 0 个元素或者超出了元素的个数会被忽略
代码演示
<style> /* 偶数 */ ul li:nth-child(even) { background-color: aquamarine; } /* 奇数 */ ul li:nth-child(odd) { background-color: blueviolet; } /*n 是公式,从 0 开始计算 */ ul li:nth-child(n) { background-color: lightcoral; } /* 偶数 */ ul li:nth-child(2n) { background-color: lightskyblue; } /* 奇数 */ ul li:nth-child(2n + 1) { background-color: lightsalmon; } /* 选择第 0 5 10 15, 应该怎么选 */ ul li:nth-child(5n) { background-color: orangered; } /* n + 5 就是从第5个开始往后选择 */ ul li:nth-child(n + 5) { background-color: peru; } /* -n + 5 前五个 */ ul li:nth-child(-n + 5) { background-color: tan; } </style>
nth-child
和nt-of-type
的区别
nth-child
选择父元素里面的第几个子元素,不管是第几个类型nt-of-type
选择指定类型的元素
代码演示
<style> div :nth-child(1) { background-color: lightblue; } div :nth-child(2) { background-color: lightpink; } div span:nth-of-type(2) { background-color: lightseagreen; } div span:nth-of-type(3) { background-color: #fff; } </style>
3.伪元素选择器
- 伪类选择器 | ::before | 在元素之前插入内容 | | :—-: | —- | | ::after | 在元素之后插入内容 |
伪类选择器注意事项
before
和after
必须有content
属性before
在内容前面,after 在内容后面before
和after
创建的是一个元素,但是属于行内元素- 创建出来的元素在
Dom
中查找不到,所以称为伪元素 伪元素和标签选择器一样,权重为 1
- 代码演示 ```css ```
1.伪元素的案例
- 添加字体图标
p { width: 220px; height: 22px; border: 1px solid lightseagreen; margin: 60px; position: relative; } p::after { content: '\ea50'; font-family: 'icomoon'; position: absolute; top: -1px; right: 10px; }