【问题标题】:Apply style to the first element of a dynamic list [duplicate]将样式应用于动态列表的第一个元素[重复]
【发布时间】:2014-10-28 17:13:19
【问题描述】:

我有一个从数据库生成的列表,所以我没有特定的长度,也没有每一行的 html 代码。 但是我需要为列表的第一行设置一个特定的样式。 所以我问是否可以告诉 css 仅将样式应用于第一个元素,如果列表是动态的也是如此。

<tr>
   <td class="align-center"><? echo $myvariable; ?></td>
</tr>

如果此 TD 生成例如 40 次,则只有第一个必须具有特定样式。这是可能的? 感谢您的建议。

【问题讨论】:

标签: html css


【解决方案1】:

如果你想先定位 tr 然后使用

table tr:first-child {
    /* Styles goes here */
}

如果您想首先定位 td 或第一个 tr,请使用

table tr:first-child td:first-child {
    /* Styles goes here */
}

如果您想首先定位每个td 中的tr

table tr td:first-child {
    /* Styles goes here */
}

为了确保您定位到正确的table,至少为该table 提供classid 并在您的选择器中使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多