 PHPz
PHPz批改状态:合格
老师批语:
 
            
        
<div class="window">
<h1 id="leteral" class="leteracy">hello world</h1>
</div>
其中导入CSS文件:
div {
background-color: red;
width: 200px;
}
div > h1 {
color: blue;
}
此时div标签的样式的权重是:0个id,0个class,1 个标签 ——(0,0,1)h1标签的权重是: 0个id,0个class,2 个标签 ——(0,0,2)

div {
background-color: red;
width: 200px;
}
div > h1 {
color: blue;
}
div > h1 {
color: cyan;
}
这里通过后写样式覆盖先前的样式将字的颜色经行改变

div {
background-color: red;
width: 200px;
}
div > h1.leteracy {
color: blue;
}
div > h1 {
color: cyan;
}
现在div > h1.leteracy的权重是:0个id,1个class,2个tagName —— (0,1,2)
而先前div > h1的权重是:0个id,0个class,2个tagName —— (0,0,2)
显然(0,1,2) > (0 , 0 ,2)
通过这样的写法,css通过选择器的权重从而可以忽略书写的顺序

div {
background-color: red;
width: 200px;
}
div > h1.leteracy {
color: blue;
}
div > h1 {
color: cyan !important;
}
此时虽然上一个h1的权重大于后一个的权重,但是后一个样式中加了!important就只显示后一个的样式

<ul class="list">
<li class="item1">item1</li>
<li class="item2">item2</li>
<li class="item3">item3</li>
<li class="item4">item4</li>
<li class="item5">item5</li>
<li class="item6">item6</li>
<li class="item7">item7</li>
<li class="item8">item8</li>
<li class="item9">item9</li>
<li class="item10">item10</li>
<li class="item11">item11</li>
<li class="item12">item12</li>
<li class="item13">item13</li>
<li class="item14">item14</li>
<li class="item15">item15</li>
</ul>

写法一:
ul {
width:100px;
background-color: black;
}
ul>li {
color:aqua;
background-color: violet;
}

写法二:
使用伪类:匹配分组的任意位置的子元素th-of-type(n)
ul {
width:100px;
background-color: black;
}
ul>:nth-of-type(n) {
color:aqua;
background-color: violet;
}
效果和上图一样
ul {
width:100px;
background-color: black;
}
ul>:nth-of-type(-n+5) {
color:aqua;
background-color: violet;
}

ul {
width:100px;
background-color: black;
}
ul>:nth-last-of-type(-n+5) {
color:aqua;
background-color: violet;
}

ul {
width:100px;
background-color: black;
}
ul>:nth-of-type(odd) {
color:aqua;
background-color: violet;
}

:nth-of-type(an+b):如 -n+3 :
| -n | result | 
|---|---|
| 0 | 5 | 
| -1 | 4 | 
| -2 | 3 | 
| -3 | 2 | 
| -4 | 1 | 
忽略 =< 0 的数字
即改变前五个的样式
将其改成:nth-last-type-(an+b):一样的参数就会变成后五个
又如::nth-of-type(-2n+15):
| n | -2n+15 | 
|---|---|
| 0 | 15 | 
| 1 | 13 | 
| 2 | 11 | 
| 3 | 9 | 
| 4 | 7 | 
| 5 | 5 | 
| 6 | 3 | 
| 7 | 1 | 
将选中这些样式
 
                 
                        
                    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号