根据索引

时间:2017-03-17 15:12:08

标签: html css

我可以根据列索引为表中的每列设置css的width属性吗?即。

table column_with_index_[0] {
    width: 100px;
}
table column_with_index_[1] {
    width: 200px;    
}
...

在我的.css中?

只有一个简单的表结构

<table>
    <tbody>
        <tr>
            <th></th>
            <th></th>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>

...等 ?

1 个答案:

答案 0 :(得分:2)

我经常这样做......

tr th:nth-child(1),
tr td:nth-child(1) {
    width: 100px;}

tr th:nth-child(2),
tr td:nth-child(2) {
    width: 200px;}

但是如果你在HTML中包含了这样的内容......

<tr>
    <th data-index="1"></th>
    <th data-index="2"></th>
</tr>

然后你可以这样定位......

tr th[data-index="1"] {
    width: 100px;}

CSS实际上无法统计索引,它只能定位您指定的内容。