html - 如何在特定 div 内的表内选择第一个 TD

标签 html css css-selectors

我想使用 CSS 选择器选择表格的第一个 td。 这是我的 Html 代码。

<div class="mydiv">
    <table class="mytable">
        <tbody>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

这是我的 CSS 代码 第一次尝试。

 div.mydiv > table > tbody > tr > td 
    {
        width:25px ;
    }

第二次尝试

div.mydiv > table  > tr > td 
{
    width:25px ;
}

似乎他们都没有工作。

问候。

最佳答案

因为您只有一个元素 .mydiv 表,只需使用 :first-of-typenth-child(1):第一个 child :

div.mydiv td:first-of-type {
    width:25px
}
<div class="mydiv">
    <table class="mytable">
        <tbody>
            <tr>
                <td>a</td>
                <td>b</td>
                <td>c</td>
                <td>d</td>
            </tr>
        </tbody>
    </table>
</div>

这里不需要使用 !important,这是不好的做法。

enter image description here

关于html - 如何在特定 div 内的表内选择第一个 TD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27834872/

相关文章:

html - 覆盖 “a” 标签应用的 CSS

javascript - 如何让按钮对应于不同的下拉 div?

css - 是否有用于包含特定文本的元素的 CSS 选择器?

javascript - 是否可以在 iframe 内容中加载混淆的 html?

javascript - Handlebars js : H1 tag won't display when submit button is clicked

css - 如何为 CSS webkit 滚动条添加边距?

javascript - 过渡保持结构上的光滑 slider

html - 如何定位直接文本而不是标签内的文本?

css - 这是什么 CSS 选择器? [ui-view].ng-enter-active

css - 100%的div宽度并没有占据页面的100%?