高级选择器
层次选择器
-
后代选择器
在某个元素的后面 祖爷爷 爷爷 爸爸 你
body后面的所有p标签都会变色
/* 后代选择器*/ body p{ background: red; }
-
子选择器
一代 儿子,只有body下的第一代的p标签才会变色
body>p{ background: green; }
-
相邻兄弟选择器
同辈
只有一个 向下
p1会变色
.c1+p{ background: green; } </style> </head> <body> <p class="c1">p0</p> <p>p1</p>
-
通用选择器
通用兄弟选择器,当前选中元素的向下的所有同辈元素
p1 p2 p3都会变色
.c1~p{
background: green;
}
</style>
</head>
<body>
<p class="c1">p0</p>
<p>p1</p>
<p>p2</p>
<p>p3</p>
结构 伪类选择器
带冒号的都是伪类 过滤
ul li:first-child{
background: #b4a53b;
}
ul li:last-child{
background: red;
}
选中当前p元素的父级元素,然后选中父级元素的第1个子元素,并且是当前元素才生效。
/*不变色*/
p:nth-child(1){
background: blueviolet;
}
/*变色*/
p:nth-child(2){
background: chocolate;
}
h>p0</h>
<p>p1</p>
<p>p2</p>
<p>p3</p>
p:nth-of-type(2){
background: deepskyblue;
}
选中父元素,下的p元素的第二个
与p:nth-child(2)区别 选择父元素下的第二个元素,并且是p元素才生效
P:nth-of-type(2) 选择父元素下的p元素的第二个
属性选择器
id和class结合
语法a[]{}
=,*=,^=,$=
标签名为a ,[]中写属性名,属性名=属性值
a[id]{
background: #f8da02;
}
a[id=windows]{
background: cyan;
}
- =表示绝对等于
*=表示包含这个元素即可
a[class*=links]{
background: #042626;
}
-
a[href^=http]{ background: darkviolet; }
^=表示a标签中的href属性其中以http开头的元素
a[href^=http]
= 表 示 a 标 签 中 的 h r e f 属 性 其 中 以 c o m ∗ ∗ 结 尾 ∗ ∗ 的 元 素 ‘ a [ h r e f 表示a标签中的href属性其中以com**结尾**的元素`a[href 表示a标签中的href属性其中以com∗∗结尾∗∗的元素‘a[href=com]`