我有一个 HTML 结构:
<div class="home-view">
<div class="view-header">Header</div>
<div class="view-content">Content</div>
</div>
我想设置home-view
的第一项的样式。在本例中,它是 view-header
,但有时没有标题,view-content
将是 home-view
中的第一项.
home-view
中的第一项应该有一些样式。
我一直在尝试使用.home-view:first-child
,但没有运气,因为它的 child 有不同的类名(我认为)。有什么建议吗?
最佳答案
.home-view > *:first-child { background-color:red; }
...将选择任何类型的第一个子元素,即第一个子元素。
关于css - :first-child with different class names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17718475/