Table with sticky header and resizable columns without using JQuery

后端 未结 4 2110
梦谈多话
梦谈多话 2021-02-13 10:44

All the solutions I\'ve seen to make a table with a sticky header and resizable columns seems to be using Jquery. I want a plain old solution using CSS only if possible. If CSS

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 11:24

    For the fixed header I used position: sticky and for the scroll I used a container to mimic the tbody scrolling (actually, it is the tbody that scrolls) and avoid a block display on the table or its tbody that would make it somewhat dysfunctional.

    The sticky position has more support than resize anyway so with the mandatory resize property it's relatively safe crossbrowser.

    https://caniuse.com/#search=sticky

    https://caniuse.com/#search=resize

    I tried several times editing this post to make the posted code run here on SO, but to no avail, so I made you a public JsFiddle to play with: https://jsfiddle.net/Erik_J/xzhpdqLo/

    (@Sølve Tornøe, I wasn't aware of your post before I submitted. Was away for a few hours and missed to refresh before posting. So you were the first to give a valid solution.)

    div {
        position: relative;
        margin: auto;
        outline: 1px solid #000;
        outline-offset: -1px;
        max-width: 900px;
        max-height: 300px;
        overflow-y: scroll;
    }
    table{ 
        width: 100%;
        border-collapse: collapse;
    }
    thead th { 
        position: -webkit-sticky;
        position: sticky;
        top: 0;
        resize: horizontal;
        overflow: auto;
        background: #cff;
    }
    th, td {
        padding: 5px 10px;
        border: 1px solid #000;
    }
    th {
        text-transform: capitalize;
    }
    
    
    header 1header 2header 3header 4
    header 11234
    header 2 22221234
    header 31234
    header 4123 3333333 3333333 33333334
    header 51234
    header 61234
    header 71234
    header 812 2222 2222 222234
    header 91234
    header 101234
    header 111234
    header 121234
    header 131234
    header 141234
    header 151234
    header 161234

提交回复
热议问题