【问题标题】:Style table depending on number of rows样式表取决于行数
【发布时间】:2020-01-03 16:17:07
【问题描述】:

仅使用 CSS,是否可以根据行数(或列数)以某种方式设置表格样式

场景,我有一个这样定义的表:

<table class='my-table-css'>
<tr> <td></td></tr> 
........
<tr> <td></td></tr> 
</table>

如果表格中的行数超过三行,我需要将偶数行的颜色与奇数行不同

如果表格中的行少于四行,我需要以相同的方式为偶数行和奇数行着色

表一,偶数行有背景色

       --------------
Row 1  |     |      |
       --------------
Row 2  | BG  | BG   |
       --------------
Row 3  |     |      |
       -------------- 
Row 4  | BG  | BG   |
       --------------

表2,连行都没有背景色

       --------------
Row 1  |     |      |
       --------------
Row 2  |     |      |
       --------------
Row 3  |     |      | 

我只需要使用 CSS 来完成此任务,有来自 CSS 大师的任何想法吗?

【问题讨论】:

标签: css


【解决方案1】:

这是一个基于this previous answer 的想法,其中唯一的要求是知道每行的高度(我知道在大多数情况下它不是固定的)。您可以轻松将此解决方案扩展到任意数量的行:

table {
 --h:45px; /* Each row will be have a height equal to 45px*/
 --n:3;   /* The condition of number of rows*/
 border-spacing:0;
 background:
    repeating-linear-gradient(to bottom, red 0 var(--h),blue var(--h) calc(2*var(--h))) 0 0/100% calc(100% - var(--h)*var(--n)),
    orange;
  
  margin:10px;
}
table td {
  border:1px solid;
  padding:10px;
  font-size:20px;
}
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>

【讨论】:

  • 那纯粹是邪恶的,恭喜 :D 我会根据行数在 JS 中添加一个类以避免过多思考^^
  • 这真的很聪明 :) 但不幸的是我无法知道行高
【解决方案2】:

你可以试试tr:nth-last-child(2):nth-child(2) {}

只有在有 3 行时才有效。

对于 2 行,将 nth-last-child 的值更新为 1。

table { 
 margin:10px;
}
table tr:nth-child(odd) {
 background:red;
}

table tr:nth-child(even) {
 background:blue;
}

table td {
  border:1px solid;
  padding:10px;
  font-size:20px;
}

table tr:nth-child(2):nth-last-child(2){
  background:red;
}
table tr:nth-child(2):nth-last-child(1) {
  background:red;
}
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>
<table class='my-table-css'>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
  <tr>
    <td>some content here</td><td>some content here</td>
  </tr>
</table>

【讨论】:

  • 该规则必须适用于具有不同行数的表,从 1 到数百......
  • 我猜这样就可以了。最好添加一个例子来演示;)
  • @mikniller 试试看,如果它坏了告诉我们
  • @MikNiller 检查更新...在使用疯狂的解决方案之前,我必须考虑简单:p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2020-03-03
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
相关资源
最近更新 更多