This question already has answers here:
:first-child not working as expected
                                
                                    (6个答案)
                                
                        
                                4年前关闭。
            
                    
:)

为什么我的第一段没有红色背景?

HTML:



    <div class="item">
        <h1><a href="http://localhost/test">Test</a></h1>
            <p>This is a new entry </p>
            <p>This is a new entry</p>
    </div>





CSS:

.item p:first-child {background:red}

演示:http://jsfiddle.net/tv90yrbz/

非常感谢您对此提供的任何帮助。

最佳答案

因为不是第一个孩子,所以h1是。

您可以使用first-of-type

.item p:first-of-type {background:red}


http://jsfiddle.net/tv90yrbz/2/



但是不支持旧版浏览器:

http://caniuse.com/#search=first-of-type

关于html - CSS中的第一个子问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31426745/

10-12 12:55
查看更多