[英]how to style two nested elements with the same parent
You can take a look at the nth-child selector.您可以查看第 n 个子选择器。
The:nth-child(n) selector matches every element that is the nth child, regardless of type, of its parent.
:nth-child(n) 选择器匹配其父元素的第 n 个子元素,无论其类型如何。
n can be a number, a keyword, or a formula.
n 可以是数字、关键字或公式。
Tip: Look at the:nth-of-type() selector to select the element that is the nth child, of a particular type, of its parent.
提示:查看:nth-of-type() 选择器到 select 元素,该元素是其父元素的特定类型的第 n 个子元素。
Or the nth-of-type property.或第n 个类型的属性。
If your HTML is:如果您的 HTML 是:
div:nth-of-type(1) > p { color: red; } div:nth-of-type(2) > p { color: blue; }
<div> <p>First paragraph</p> </div> <div> <p>Second paragraph</p> </div>
You can give class for every "p" tag.您可以为每个“p”标签提供 class。
<!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">
<style>
.p1{
height: 50px;
width: 200px;
border: 2px solid red;
}
.p2{
height: 50px;
width: 200px;
border: 2px solid green;
}
</style>
<title>Document</title>
</head>
<body>
<div>
<p class="p1">This is paragraph</p>
<p class="p2">This is paragraph</p>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.