在 colgroup 中使用文本对齐中心 [英] using text-align center in colgroup
问题描述
我的页面中有一个表格,我使用 colgroups 以相同的方式格式化此列中的所有单元格,适用于背景颜色和所有内容.但似乎无法弄清楚为什么 text-align center 不起作用.它不会将文本居中对齐.
i have a table in my page, i use colgroups to format all cells in this column the same way, works good for background color and all. but cannot seem to figure out why text-align center does not work. it does not align the text centered.
示例:
<table id="myTable" cellspacing="5">
<colgroup id="names"></colgroup>
<colgroup id="col20" class="datacol"></colgroup>
<colgroup id="col19" class="datacol"></colgroup>
<colgroup id="col18" class="datacol"></colgroup>
<thead>
<th> </th>
<th>20</th>
<th>19</th>
<th>18</th>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
CSS:
#names {
width: 200px;
}
#myTable .datacol {
text-align: center;
background-color: red;
}
推荐答案
只有 a一组有限的 CSS 属性适用于列,text-align
不是其中之一.
Only a limited set of CSS properties applies to columns, and text-align
isn't one of them.
参见为什么只有四个属性适用于表列的奥秘" 说明为什么会出现这种情况.
See "The mystery of why only four properties apply to table columns" for a description of why this is the case.
在您的简单示例中,最简单的修复方法是添加以下规则:
In your simple example, the easiest way to fix it would be to add these rules:
#myTable tbody td { text-align: center }
#myTable tbody td:first-child { text-align: left }
这将使所有表格单元格居中,第一列除外.这在 IE6 中不起作用,但在 IE6 中 text-align
does 实际上(错误地)在列上工作.我不知道 IE6 是否支持所有属性,或者只是一个更大的子集.
That would center all table cells, except the first column. This doesn't work in IE6, but in IE6 the text-align
does actually (wrongly) work on the column. I don't know if IE6 supports all properties, or just a larger subset.
哦,您的 HTML 无效. Oh, and your HTML is invalid. 这篇关于在 colgroup 中使用文本对齐中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 遗漏了一个
.
<thead>
misses a <tr>
.
登录
关闭