使用 CSS 3 在第一个表中选择第一个 td

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3729720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:46:53  来源:igfitidea点击:

Selecting first td in the first table with CSS 3

css

提问by pondpad

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.

我想要做的是将样式应用于第一个表的第一个 td。

I would have expected:

我会期望:

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

However it does not work? Please advise!

然而它不起作用?请指教!

回答by RickF

#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

似乎工作。
请注意,在 IE 下,您需要在 html 中有一个 !DOCTYPE 声明才能获得对 :first-child 的支持。 http://www.w3schools.com/cssref/sel_firstchild.asp