gpt4 book ai didi

html - css first-of-type 在其他类下

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:32 24 4
gpt4 key购买 nike

我有一个看起来像这样的 html 文件:

<div class="my_class">
<h1>gets styled and that's good</h1>
<h1>doesn't get styled and this is intended</h1>
<div class="my_other_class">
<h1>but this also get styled, but shouldn't</h1>
</div>
</div>

<div class="my_class">
<h1>do style</h1>
<h1>don't style</h1>
<div class="my_other_class">
<h1>shouldn't but does</h1>
</div>
</div>

现在我使用 .my_class h1:first-of-type {...} 来设置第一个 h1 的样式。然而,第三个也得到了风格,不应该。

有没有办法只选择第一个h1

最佳答案

使用子组合器 >

.my_class > h1:first-of-type {
color: red;
}
<div class="my_class">
<h1>gets styled and that's good</h1>
<h1>doesn't get styled and this is intended</h1>
<div class="my_other_class">
<h1>but this also get styled, but shouldn't</h1>
</div>
</div>

关于html - css first-of-type 在其他类下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27535151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com