Table with Rowspan Hover and Zebra effect

前端 未结 2 634
终归单人心
终归单人心 2020-12-30 11:19

I am trying to create a table that has a rowspan, zebra effect and highlights the row on hover. I kind of got it working but not quite.

It should be like this: http:

相关标签:
2条回答
  • 2020-12-30 11:36

    This is a job for tbody. Multiple tbody elements are allowed in a table at least as far back as HTML4, and they're designed for grouping related rows together. This way, you don't need JavaScript at all.

    body {
      padding: 1em;
    }
    
    table {
      width: 100%;
      border-collapse: collapse;
    }
    
    td,
    th {
      padding: .25em;
      border: 1px solid black;
    }
    
    tbody:nth-child(odd) {
      background: #CCC;
    }
    
    tbody:hover td[rowspan],
    tr:hover td {
      background: red;
    }
    <table>
      <tbody>
        <tr>
          <td rowspan="3"></td>
          <td>a</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>b</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>c</td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    
      <tbody>
        <tr>
          <td rowspan="3"></td>
          <td>a</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>b</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>c</td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    
      <tbody>
        <tr>
          <td rowspan="3"></td>
          <td>a</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>b</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>c</td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-12-30 11:39

    Something like:

    // stripe
    tr:nth-child(even) {
        background-color: #ccc;
    }
    // hover
    tr:hover {
         background-color: #c00;
    }
    

    should work. Post your code up.

    0 讨论(0)
提交回复
热议问题