简体   繁体   English

首先是CSS风格 <td> 在一个 <tr>

[英]CSS style for first <td> in a <tr>

I'm trying to set all the <td> elements of my <tr> to be right-aligned except the first one, which I want to be left-aligned. 我试图将<tr>的所有<td>元素设置为右对齐,除了第一个,我想要左对齐。 I tried this: 我试过这个:

<style>
td { text-align: right };
td:first-child { text-align: left };
</style>

That makes all cells be right-aligned. 这使得所有细胞都是右对齐的。 If I swap the order of those two rows, then all cells are left-aligned. 如果我交换这两行的顺序,那么所有单元格都是左对齐的。 Highly confused. 高度困惑。 According to w3schools, 根据w3schools,

The :first-child selector is used to select the specified selector, only if it is the first child of its parent. :first-child选择器用于选择指定的选择器,前提是它是父节点的第一个子节点。

But that doesn't seem to be happening for me. 但这似乎并没有发生在我身上。

Your syntax was incorrect. 你的语法不正确。

td { text-align: right };                    /* This was being applied */
td:first-child { text-align: left };         /* This wasn't.. */

Should be: 应该:

td { text-align: right; }
td:first-child { text-align: left; }

Note the semi-colons - they shouldn't be outside of the brackets {} 注意分号 - 它们不应该在括号{}

Other than that, you were using :first-child properly. 除此之外,您正在使用:first-child正确:first-child

jsFiddle demo jsFiddle演示

Another option, depending on what you want to do: 另一种选择,取决于你想做什么:

table tr td {
    background-color:red;}
table tr td:nth-child(1) {
    background-color:green;}
td:nth-child(2) { text-align: right; }

你可以在一行中做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM