伪类 太监
1.一个冒号
2.可以有多个
3.可在前 或在后
4.基于DOM,不产生新对象 (太监不能生育)
伪类:
状态类::link :visited :hover :active :focus
结构类::first-child :last-child :nth-child :nth-of-type
表单类::checked :disabled :valid :rqueired
语言类::dir :lang
伪元素 皇帝
1.两个冒号
2.只能有一个
3.一定在最后面
4.创建对象不存在DOM的对象,并且可以对这个DOM操作 (皇帝可以生育)
伪元素:
::before ::after
::first-letter ::first-line
::selection
::planceholder
::backdrop
语法 数量 位置 类与元素 使用场景
伪类 : 多个 前方和后方修饰 多
伪元素 :: 单个 后方 创建对象 少
以下是代码:
伪类伪元素个数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
background-color: #ccc;
color: #eee;
margin-left: 30px;
}
input {
font-size: 3em;
width: 200px;
height: 40px;
border-radius: 10px;
}
input:out-of-range:focus {
background: gold;
}
input:out-of-range+h2::after {
content: '超出范围';
color: red;
}
</style>
</head>
<body>
<div>
<h1>请输入数字</h1>
<input type="number" min="1" max="20">
<h2>结果 : </h2>
</div>
</body>
</html>
![在这里插入图片描述](https://img-blog.csdnimg.cn/202003280441110.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1ajE5MzU=,size_16,color_FFFFFF,t_70)
伪类伪元素位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
background-color: #ccc;
color: #eee;
margin-left: 30px;
}
input {
margin: 100px 80px auto;
width: 100px;
height: 100px;
}
input::after {
content: '已勾选';
display: none;
width: 150px;
height: 50px;
font-size: 30px;
margin-left: 110px;
}
input:checked::after {
display: block;
}
</style>
</head>
<body>
<div>
<!-- 位置 -->
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
</body>
</html>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200328044159509.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1ajE5MzU=,size_16,color_FFFFFF,t_70)
伪类、伪元素 DOM操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
background-color: #00a8cc;
padding: 30px;
font-size: 50px;
color: #eee;
}
li:nth-child(2n-1) {
color: gold;
}
</style>
</head>
<body>
<div>
<ol>
<li>第一个</li>
<li>第二个</li>
<li>第三个</li>
<li>第四个</li>
<li>第五个</li>
</ol>
</div>
</body>
</html>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200328044710752.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1ajE5MzU=,size_16,color_FFFFFF,t_70)
伪类伪元素优缺点对比
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
background-color: #00a8cc;
padding: 30px;
font-size: 50px;
color: #eee;
}
p::first-letter {
/*
伪元素设置 对元素修饰
伪元素添加了一个元素对象,不会占用DOM对象节点
*/
color: gold;
}
</style>
</head>
<body>
<div>
<!--
伪类 太监
1.一个冒号
2.可以有多个
3.可在前 或在后
4.基于DOM,不产生新对象 (太监不能生育)
伪类:
状态类::link :visited :hover :active :focus
结构类::first-child :last-child :nth-child :nth-of-type
表单类::checked :disabled :valid :rqueired
语言类::dir :lang
伪元素 皇帝
1.两个冒号
2.只能有一个
3.一定在最后面
4.创建对象不存在DOM的对象,并且可以对这个DOM操作 (皇帝可以生育)
伪元素:
::before ::after
::first-letter ::first-line
::selection
::planceholder
::backdrop
-->
<!-- 语法 数量 位置 类与元素 使用场景
伪类 : 多个 前方和后方修饰 多
伪元素 :: 单个 后方 创建对象 少
-->
<p>一二三四五</p>
</div>
</body>
</html>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200328044912437.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1ajE5MzU=,size_16,color_FFFFFF,t_70)