html - 令人困惑的表格 CSS

标签 html css css-tables

我有一个描述表格的 CSS 元素,它包含这一行:

.pricing tr td:last-child { text-align: right; }

这使得最后一列中的文本右对齐。定价类是我要编辑的表的类。 我还没有真正能够弄清楚或在任何地方找到在 CSS 中列出表格标签的规则。如果我把:

table .pricing tr td:last-child { text-align: right; }

.pricing table tr td:last-child { text-align: right; }

那么这一行就不行了。但是,如果我输入:

table.pricing tr td:last-child { text-align: right; }

然后它就起作用了。

更令人困惑的是,我正在编辑主题的 CSS,它有以下几行:

table caption { font-weight: bold; text-align: left; padding: 4px 6px; border: 1px solid #ddd; border-bottom: 0; background: #ffd53c url(img/pattern.png); }

table th, table td { padding-left: 6px; padding-right: 6px; line-height: 21px; border: 1px solid #ddd; background-color: #fff; text-align: left; vertical-align: middle; }

table th, table tfoot td { font-weight: bold; background: #eee url(img/pattern.png); }

table tfoot a { text-decoration: none; }

table .alternate td { background-color: #fafafa; }

table.large th,table.large td { padding: 12px 8px; }

我只是想弄清楚一个规则,什么时候放table,什么时候不放table,是必须写tr td还是只写td,类选择器放在哪里(一切之前,中间,with或没有 table 等)。

我的 HTML 非常简单,就是这样:

<table class="pricing">
  <tr> <th>Codes </th> </tr>
  <tr> <td>The Constitution of the Russian Federation (adopted at National Voting on December 12, 1993)</td> <td>£34.00</td> </tr>

最佳答案

如果这个选择器有效

table.pricing tr td:last-child

这意味着你的表有一个pricing类,所以很明显

table .pricing tr td:last-child

无法工作(因为您正在寻找 tabletr 之间的 child (例如 tbody/thead/tfoot),类 定价和这条规则

.pricing table tr td:last-child 

查找包含在具有 parent 类的元素中的表格。这绝对是三个不同的规则

是否需要在您的选择器中指定一个元素/类,取决于您的规则必须有多具体,您是否必须覆盖以前的规则。作为经验法则,我建议您开始定义更简单(更短)的规则并增加它们的特异性,直到不应用该样式。

关于html - 令人困惑的表格 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18957878/

相关文章:

javascript - 使用 JavaScript 更改 HTML 元素

css - 如何根据屏幕分辨率移动图标?

javascript - onClick 方法适用于第二次单击。但它应该在第一次点击时起作用

javascript - Fancybox 不工作,如何让 Overlay 工作

javascript - 拖放布局设计器

css - 如何使用 Bootstrap 为所有元素制作全宽表单元素

html - 某些 Bootstrap 4 类在我的 Angular 8 元素中不起作用

html - 如何创建具有 div 和不同列宽的表格?

html - Firefox - 宽度 : 100% not working for list in a list (display: table;)

html - 如何继承div中的高度?