Sur*_*esh -2 html css css-selectors
假设我有 2 个与“MyTitle”相同的值。我如何设计 2 个值以具有不同的颜色?
<div class="MyTitle">Insert text here</div>
<div class="MyTitle">Insert text here</div>
Run Code Online (Sandbox Code Playgroud)
我希望此处插入文本的第一个值是红色,然后是绿色?
我是这样做的,但它适用于两者,知道如何解决这个问题吗?
.MyTitle {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
使用 :nth-child() 选择器。
下面的例子:
.MyTitle:nth-child(1) {
//This will style the first element
color: red;
}
.MyTitle:nth-child(2) {
//This will style the second element
color: green;
}
Run Code Online (Sandbox Code Playgroud)