如何在表单中包含的表中设置第一个元素的样式

说我有这个表格:

Sample text

我怎么能添加一个新的样式添加到这个表单的第一个,因为我没有它的id,这是我到目前为止:

 form table tbody tr td{ font-family : Arial; font-weight: bold; } 

我会按照其他答案使用第一个孩子,但是你不一定需要对css规则的其余部分如此具体。 例如,下面的规则就足够了:

 form td:first-child { font-family: Arial; font-weight: bold; } 

您的css不仅更容易理解和调试,而且如果您需要覆盖此规则将更加简单,而无需使用其他规则。

如果您希望规则仅应用于其父级的第一个子级,请使用first-child

 form table tbody tr td:first-child { font-family : Arial; font-weight: bold; } 

来自MDN的参考

尝试,

 form table tbody tr td:first-child{ font-family : Arial; font-weight: bold; } 

在你的CSS中你必须使用第一个孩子

 form table tbody tr td:first-child{ font-family : Arial; font-weight: bold; } 

您可以使用CSS first-child选择器,如下所示:(注意直接后代选择器,这将只选择表单中第一个表中每个tr中的第一个td

CSS:

 form > table > tbody > tr > td:first-child { font-family: Arial; font-weight: bold; }​ 

演示: http : //jsfiddle.net/SO_AMK/hVChC/

您可以将class分配给第一个td ,然后将CSS规则部署到class

 
Sample text
form table tbody tr td.firstChild{ font-family : Arial; font-weight: bold; }

伪选择器:first-child是不错的选择,但并不是所有浏览器版本都支持。

见下表