首页 > 表格如何定义列样式的文本属性

表格如何定义列样式的文本属性

如代码所示,可以对列的背景色进行改变,但是修改文本对齐方式和文本颜色就不起作用了,能解决吗?
html

<table>
    <colgroup>
        <col />
        <col />
        <col class="right" />
    </colgroup>
    <tr>
        <td>aaa</td>
        <td>bbb</td>
        <td>ccc</td>
    </tr>
    <tr>
        <td>aaa</td>
        <td>bbb</td>
        <td>ccc</td>
    </tr>
    <tr>
        <td>aaa</td>
        <td>bbb</td>
        <td>ccc</td>
    </tr>
</table>​

css

table {
    width: 100%;
}
td {
    border: 1px solid #999;
}
.right {
    background: green;
    color: #fff;
    text-align: right;
}

http://jsfiddle.net/G9Nna/


IE的IE7和Quirks模式下,文本颜色和对齐方向都可以正常设置
IE8(含)以上以及其他浏览器均不支持。

CSS具体样式对col和colgroup支持的细节,我找到如下说法:

The style attribute (or induced style from id, class, etc.) only takes into account properties that sensibly apply to the column itself. That is, while each <td /> can contain text content and thus can have attributes like text-align set, the <col /> element does not contain text and thus none of the text-level styles apply. (Block-level stuff like background-color still works.)

意思就是 col 元素不包含文本,所以被指定的有关于文本级别的样式是不会被应用的。

From:Is html <COL align> deprecated?

【热门文章】
【热门文章】