在相同标签多的情况下,不用再起类名对其中一个进行css样式设计:
例: p:nth-child(3){}
第一个p也可用p:first-child{}
最后一个p可用 p:last-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>nth-child()选择器</title>
<style type="text/css">
*{margin: 0;padding: 0;}
div{width: 800px;height: 600px;background: rgb(182, 177, 177); margin: 0 auto;}
div p{font-size: 50px;color: black;}
div p:nth-child(3){color: green;font-size: 60px;}
div p:nth-child(5){color: pink;font-size: 60px;}
div p:first-child{color: yellow;font-size: 60px;}
div p:last-child{color: blue;font-size: 60px;}
</style>
</head>
<body>
<div>
<p>AAA</p>
<p>BBB</p>
<p>CCC</p>
<h2>DDD</h2>
<p>EEE</p>
<p>FFF</p>
</div>
</body>
</html>