结构伪类选择器
(一)基本用法
这里主要介绍nth-child和nth-of-type两种结构伪类选择器.
图中的E代表类名,标签名等目标标签的名字,也就是你想要选中的标签.
1.nth-child
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/*这里选中ul后代中第一个标签,并且为li标签,这只红色背景色*/
li:first-child{
background-color: red;
}
/*这里选中ul后代中第二个标签,并且标签为li,设置绿色背景色*/
li:nth-child(2n){
background-color: green;
}
</style>
</head>
<body>
<ul>
<li>这是第1个li</li>
<li>这是第2个li</li>
<li>这是第3个li</li>
<li>这是第4个li</li>
<li>这是第5个li</li>
<li>这是第6个li</li>
<li>这是第7个li</li>
<li>这是第8个li</li>
</ul>
</body>
</html>
效果如下:
2.nth-of-type
nth-child和nth-of-type的区别:
nth-child对父级元素内的子元素按顺序排序,但不分类排序.nth-of-type分类排序.
nth-of-type的写法:
1.first-of-type
2.last-of-type
3.nth-of-type(同样可以写nth-child的公式)
(二)公式
注意:必须严格按照公式的样式来写,不能颠倒顺序
伪元素
注意:伪元素可以添加样式,且是行内元素.先写伪类选择器,再写伪类元素.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 结构中只有father中的爱这个字是实际存在的结构,下面添加伪元素,生成"老鼠袋大米" */
.father {
width: 300px;
height: 300px;
background-color: pink;
}
/* 伪元素 */
.father::before {
content:'老鼠';
}
.father::after {
content:'大米';
}
</style>
</head>
<body>
<div class="father">爱</div>
</body>
</html>
效果图如下:
伪类选择器
伪类选择器选中的是一个状态
(一)链接伪类选择器
(二)焦点伪类选择器
占位符伪类选择器
input::placeholder { }
添加占位符的样式