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

后端 未结 5 1058
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  不知归路
    2020-12-14 17:32

    Here's my solution, based on sibling combinators.
    The main part is:

    .datacell:hover ~ .datarow {
        background-color: rgba(255,255,0,0.5);
    }
    .datacell:hover ~ .datarow + .datacell ~ .datarow{
        background-color: transparent;
    }
    

    Snippet:

    .datatable{
        display:grid;
        grid-gap: 0;
        grid-template-columns: auto 1fr auto;
        position: relative;
    }
    
    .datarow{
        grid-column: 1 / 4;
        z-index: 0;
    }
    
    .datacell{
        z-index: 1;
        padding: 8px;
        border-bottom: 1px solid #c0c0c0;
    }
    
    .datacell:hover ~ .datarow {
        background-color: rgba(255,255,0,0.5);
    }
    .datacell:hover ~ .datarow + .datacell ~ .datarow{
        background-color: transparent;
    }
    
    .row-1{ grid-row: 1;}
    .row-2{ grid-row: 2;}
    .row-3{ grid-row: 3;}
    
    .col-1{ grid-column: 1;}
    .col-2{ grid-column: 2;}
    .col-3{ grid-column: 3;}
    Row 1 Column 1
    Row 1 Column 2
    Row 1 Column 3
    Row 2 Column 1
    Row 2 Column 2
    Row 2 Column 3
    Row 3 Column 1
    Row 3 Column 2
    Row 3 Column 3

    The html must be structured so that a .datarow element closes the virtual grid row and spans all the preceding cells. Explicitation of the positioning inside the grid is needed in order to let cells and rows overlap.

提交回复
热议问题