开发者

Selecting first td in the first table with CSS 3

Say for example I have:

<div id="container">
    <table>
        <tr>
            <td></td>
            <td></td>
        </tr>

        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>

    <table>
        <tr>
            <td></td>
            <td></td>
        </tr>

        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>
</div>

And

td { width:10px; height:10px; }

What I want to do is apply a style to the first td, of the first table.

开发者_如何转开发

I would have expected:

#container table:first-child td:first-child {
    background: red;
}

However it does not work? Please advise!


#container table:first-child tr:first-child td:first-child { background: red; }

Seems to work.
Note that under IE, you will need to have a !DOCTYPE declaration in your html to get support for :first-child. http://www.w3schools.com/cssref/sel_firstchild.asp


To select only the first table within the div:

div#container table:nth-child(1) tr td {
    color: #888;
}

To select only the first td of the first table within the div:

div#container table:nth-child(1) tr td:nth-child(1) {
    color: #444;
}

For me this works easier if you want to change it to a second table or second tablecell

div#container table:nth-child(2) tr:nth-child(4) td:nth-child(1) {
    color: #888;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜