目录标题
CSS3属性选择器
对以上选择符做代码验证:
<style>
/* 小手样式 */
/* 权重是 1 */
button {
cursor: pointer;
}
/* 属性选择器使用方法 */
/* 箭头样式 */
/* 选择的是:既是button 又是 disabled 这个属性的元素 */
/* 属性选择器的权重是10 1+10=11*/
/* 1.直接写属性 */
/* 显示箭头样式 */
button[disabled] {
cursor: default;
}
/* 2.属性等于值 */
input[type="search"] {
color: pink;
}
/* 3.以某个值开头的属性 */
/* 选中以icon开头的 */
div[class^="icon"] {
color: red;
}
/* 4.以某个值结尾的 */
/* 选中以icon结尾的 */
div[class$="icon"] {
color: green;
}
/* 5.可以在任意位置的 */
/* 只要包含的都选中 这些字母也是要连续的*/
div[class*="icon"] {
color: purple;
}
</style>
<body>
<!-- disabled 是禁用我们的按钮 -->
<button>按钮</button>
<button>按钮</button>
<button disabled="disabled">按钮</button>
<button disabled="disabled">按钮</button>
<input type="text" name="" id="" value="文本框">
<input type="text" name="" id="" value="文本框">
<input type="text" name="" id="" value="文本框">
<input type="search" name="" id="" value="搜索框">
<input type="search" 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="abcicon">图标5</div>
</body>
显示样式:
CSS3结构伪类选择器
示例一:
<style>
/* 选择的是第一个 li */
ul li:first-child {
background-color: pink;
}
/* 选择的是最后一个li */
ul li:last-child {
background-color: deeppink;
}
/* nth-child(n) 我们要第几个 n就是几 */
ul li:nth-child(8) {
background-color: green;
}
</style>
<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>
<li>9</li>
<li>10</li>
</ul>
</body>
显示样式:
nth-child(n)
- n可以是数字、关键字和公式
- n如果是数字,就是选择第几个
- 常见的关键词有 even 偶数 odd 奇数
- 常见公式如下(如果n是公式,则从0开始计算)
- 但是第0个元素或者超出了元素的个数会被忽略
公式 | 取值 |
---|---|
2n | 偶数 |
2n+1 | 奇数 |
5n | 5 10 15… |
n+5 | 从第5个开始(包含第五个)到最后 |
-n+5 | 前5个(包含第5个) |
示例二:
<style>
/* n 可以是关键词 even 是偶数 odd 是奇数 */
ul li:nth-child(even) {
background-color: pink;
}
ul li:nth-child(odd) {
background-color: red;
}
/* n 可以是公式 */
/* 2n 偶数 相当于 even */
ul li:nth-child(2n) {
background-color: green;
}
/* 选择 5 的倍数 */
ul li:nth-child(5n) {
background-color: purple;
}
/* 选择 后五个 */
ul li:nth-child(n+5) {
background-color: hotpink;
}
/* 选择 前五个 */
ul li:nth-child(-n+5) {
background-color: skyblue;
}
</style>
<body>
<!-- ul 里面我们只允许放li 所以对于这个例子来说 nth-child 和 nth-of-type 就一样了 -->
<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>
<li>9</li>
<li>10</li