我試圖選擇一個名為的類中的第h1一個。如果它是其中的第一個元素,它會起作用,但是如果在它之后出現,它將不起作用。divdetail_containerh1divul<style type="text/css">.detail_container h1:first-child{color:blue;} </style></head><body><div class="detail_container"> <ul> <li></li> <li></li> </ul> <h1>First H1</h1> <h1>Second H1</h1></div></body></html>我的印象是,h1無論它在哪里,我擁有的CSS都會選擇第一個div。我該如何運作?
3 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
該h1:first-child選擇裝置
當且僅當它是一個h1元素時,才選擇其父項的第一個孩子。
所述:first-child容器的這里是ul,這樣不能滿足h1:first-child。
:first-of-type您的情況有CSS3 :
.detail_container h1:first-of-type
{
color: blue;
}
但是由于瀏覽器兼容性問題而已,那么最好還是給第h1一個類一個類,然后再定位該類:
.detail_container h1.first
{
color: blue;
}

人到中年有點甜
TA貢獻1895條經驗 獲得超7個贊
對于這種特定情況,您可以使用:
.detail_container > ul + h1{
color: blue;
}
但是,如果在許多情況下都需要相同的選擇器,則應該為這些選擇器提供一個類,如BoltClock所說。
- 3 回答
- 0 關注
- 574 瀏覽
相關問題推薦
添加回答
舉報
0/150
提交
取消