2017-03-17 39 views
0

我可以基于列索引为表中的每列设置css中的宽度属性吗?即基于索引将宽度属性分配给表中的列

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> 

等.. ?

回答

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实际上无法计算索引,它只能指定您指定的内容。