How to make hover state on row table with CSS grid layout

后端 未结 5 1052
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 16:52

This a table that created with CSS Grid Layout, but I have a problem with it, I can\'t make hover state on each row.

I only want use CSS for this.

Can anyone

5条回答
  •  Happy的楠姐
    2020-12-14 17:46

    Here is a trick using pseudo-element. The idea is to use the element as a background and make it overflow on the left and right so that it will cover all the row. Like that if you hover on any element inside a row you change the color and it's like you changed the color of the whole row.

    This trick involve few changes on the markup and also more CSS.

    .table {
      display: grid;
      grid-template-columns: [col-start] auto [col-end];
      grid-template-rows: [header-start] 50px [header-end row-start] auto [row-end];
      grid-auto-rows: auto;
      grid-auto-columns: auto;
      grid-gap: 1px;
      overflow: hidden;
      background: gray;
    }
    
    .table>* {
      padding: 10px;
      position: relative;
    }
    
    .heading {
      background: navy;
      color: #fff;
      grid-row: header;
    }
    
    .row span {
      position: relative;
      z-index: 2;
    }
    
    .row:before {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      right: -1000%;
      left: -1000%;
      z-index: 1;
    }
    
    .row:after {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      right: -1px;
      width: 1px;
      z-index: 2;
      background-color: #fff;
    }
    
    .row:nth-child(5n+5)::after {
      bottom: -1px;
      right: 0;
      left: -1000%;
      height: 1px;
      z-index: 3;
      width: auto;
      top: auto;
      background-color: #fff;
    }
    
    .row:hover::before {
      background-color: red;
    }
    Title 1
    Title 2
    Title 3
    Title 4
    Title 5
    Row 1
    Row 1
    Row 1
    Row 1
    Row 1
    Row 2
    Row 2
    Row 2
    Row 2
    Row 2
    Row 3
    Row 3
    Row 3
    Row 3
    Row 3

提交回复
热议问题