我有以下 HTML 代码:
<div class="foo">
...
<span> ---> can be the first
<h1> ---> can be the first
<h2> ---> can be the first
and so on...
...
</div>
我想向第一个元素添加一些 CSS 样式,但没有声明 HTML 元素的类型。
例如,这段代码对我没有帮助:
.foo span:first-child
我希望 CSS 能够在第一个元素上运行,即使开发人员选择在该 div 中进行更改也是如此。
有什么办法吗?
最佳答案
您可以只添加此 CSS,它将以 .foo
的第一个子元素为目标。:
.foo > :first-child {
/* styling here */
}
关于html - 在不知道第一个 child 的类型的情况下选择第一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29920169/