1基本选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 标签选择器 选中所有的p标签*/
p{
color: aqua;
}
/* id选择器 */
#box1{
color: pink;
}
/* 类选择器 */
.box2{
color: blueviolet;
}
/* 通配符选择器 */
*{
background-color:pink;
}
</style>
</head>
<body>
<p>我是一段文字</p>
<p>我是一段文字</p>
<div id="box1">我是一个盒子</div>
<div class="box2">我是一个盒子</div>
</body>
</html>
2.包含选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 子代选择器 选中亲生儿子*/
.a>li{
background-color: pink;
}
/* 后代选择器 找到后代所有要找的元素*/
.a li{
color: aqua;
}
</style>
</head>
<body>
<div>
<ul class="a">
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<ul>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</ul>
</div>
</body>
</html>
3.复合选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 复合选择器 */
div,
p,
span{
color:red;
}
</style>
</head>
<body>
<div>asdasfaf</div>
<p>safafewaaf</p>
<span>rgewgwq</span>
<ul>
<li>拆的花朵</li>
</ul>
</body>
</html>
4.属性选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
input[type="password"]{
background-color: aquamarine;
}
div[id]{
width: 200px;
height: 300px;
background-color: black;
}
/* type^="te" 以te开头 */
input[type^="te"]{
background-color: aquamarine;
}
/* type$="l" 以l结尾*/
input[type$="l"]{
background-color: green;
}
/* type*="e" type值里包含e的*/
input[type*="e"]{
background-color: green;
}
</style>
</head>
<body>
<input type="text"> <br/>
<input type="password"> <br/>
<input type="search"> <br/>
<input type="url"> <br/>
<input type="number"> <br/>
<div id="aquamarine">1</div>
</body>
</html>
5.伪类选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
a:link{
color:pink;
}
a:visited{
color:black;
}
/* hover 鼠标悬停 */
a:hover{
/* 鼠标样式 */
cursor:cell;
font-size:40px;
}
/* 长按 */
a:active{
font-size:70px;
}
a:hover+div{
/* 元素消失 */
display: none;
}
</style>
</head>
<body>
<a href="#">去百度</a>
<div> </div>
</body>
</html>
结构伪类选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 父元素 子元素:nth-child(n):父元素第n个子元素 -->
<style>
ul li:first-child{
background-color: pink;
}
ul li:last-child{
background-color: green;
}
ul li:nth-child(4){
background-color: blue;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> ul li:nth-child(2){ background-color: pink; } ul li:nth-of-type(2){ background-color: green; } </style> </head> <body> <ul> <p>我是一段文字</p> <li>1</li> <li>2</li> <li>3</li> </ul> </body> </html>
6.伪元素选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul li::before{
/* content 必有属性 */
content: "~";
}
ul li::after{
content: "<";
}
/* 表单提示词 */
input::placeholder{
color:red;
}
/* selection 选中时 */
ul li:nth-child(4)::selection {
color: pink;
}
</style>
</head>
<body>
用户名:<input type="text" name="user" value="" placeholder="请输入用户名:" maxlength="4">
<ul>
<li>asfafs</li>
<li>gegsg</li>
<li>asfafs</li>
<li>hdrrhd</li>
</ul>
</body>
</html>