This question already has answers here:
What CSS selector can be used to select the first div within another div
                                
                                    (4个答案)
                                
                        
                                5年前关闭。
            
                    
我有一个从数据库生成的列表,所以我没有特定的长度,也没有每一行的html代码。
但是我需要为列表的第一行设置特定的样式。
所以我问是否有可能告诉CSS仅将样式应用于第一个元素,以及列表是否是动态的。

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


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

最佳答案

如果要首先定位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,请至少为该class提供一个idtable并在选择器中使用它。

10-05 20:57
查看更多