html - <tr> 中第一个 <td> 的 CSS 样式

标签 html css css-selectors

我正在尝试将 的所有 元素设置为右对齐,但第一个元素除外,我希望将其左对齐。我试过这个:

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

这使得所有单元格都右对齐。如果我交换这两行的顺序,那么所有单元格都是左对齐的。非常困惑。根据 w3schools,

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

但这似乎并没有发生在我身上。

最佳答案

您的语法不正确。

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

应该是:

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

请注意分号 - 它们不应位于方括号外 {}

除此之外,您正确使用了 :first-child

jsFiddle demo

关于html - <tr> 中第一个 <td> 的 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192960/

相关文章:

css - "undefined"第 0 行

html - 匹配包含部分 URL 和特定类的链接

css - 溢出的 DIV :hidden lets children disappear

html - CSS 幻灯片菜单无法按预期工作

javascript - 我应该使用 PHP 将 CSS 和 JavaScript 文件导入到我的页面吗?

html - 在行中显示 div

PHP 相当于第 n 个 child

Css 选择器 SharePoint 2010

javascript - 整页水平 javascript Accordion

javascript - 使用 jQuery 检查 <li> 标签上是否已经存在一个值