CSS中的第一个孩子问题

mic*_*urk -1 html css css-selectors

:)

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

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>
Run Code Online (Sandbox Code Playgroud)

CSS:

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

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

非常感谢您对此的任何帮助.

Cur*_*urt 5

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

你可以使用first-of-type:

.item p:first-of-type {background:red}
Run Code Online (Sandbox Code Playgroud)

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


但不支持较旧的浏览器:

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