当您不知道第一个元素是什么时,是否可以解决 div 中的第一个元素。
例如,我有两个不同的 div
<div class="templateOne">
<h1>Header 1</h1>
</div>
<div class="templateOne">
<h2>Header 2</h2>
</div>
然后我可以说
.templateOne > * {
margin-top: 0em;
}
或类似的东西。
如果要使用地址第一个子元素,可以使用:first-child
或:nth-child(1)
伪选择器。
.templateOne :first-child {
color: red;
}
<div class="templateOne">
<h1>Header 1</h1>
<p>Content 1</p>
</div>
<div class="templateOne">
<h2>Header 2</h2>
<p>Content 2</p>
</div>
如果您只想处理具有特定类名的第一个元素,您可以使用:first-of-type
或nth-of-type(1)
:
.templateOne:first-of-type {
color: red;
}
<div class="templateOne">
<h1>Header 1</h1>
<p>Content 1</p>
</div>
<div class="templateOne">
<h2>Header 2</h2>
<p>Content 2</p>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句