Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make first two columns sticky in a table

I need to make the first two columns sticky in a table having n number of columns each of dynamic width.

I had tried the below CSS

td:nth-child(1), td:nth-child(2){
   position:sticky;
   left:0px;
}

And then I had set the left position of second column in JS by calculating the width of first column

var width = $("table tr > td:nth-child(1)").outerWidth();
$("table.matrix_class tr > td:nth-child(2)").css('left', width);

Now I need to do all the stuff in CSS not in JS. How do I do that in pure CSS?

Additionally, how do you do this when the first column width is dynamic?

like image 848
Vicky Avatar asked Nov 28 '17 12:11

Vicky


People also ask

What are sticky columns?

Sticky columns enable you to display specific columns at all times while the user scrolls the Grid horizontally. This specific column will be scrollable as well, however, it will fix its position to the left/right when it reaches left/right Grid border.

How do you fix a first column in a table?

You can use the position property set to “absolute” for the first column and specify its width. Then use the overflow-x property set to “scroll” for the entire table.


1 Answers

You can do sticky header with using this css.

live working demo

<div class="zui-wrapper">
<div class="zui-scroller">
    <table class="zui-table">
        <thead>
            <tr>
                <th class="zui-sticky-col">Name</th>
                <th class="zui-sticky-col2">Number</th>
                <th>Position</th>
                <th>Height</th>
                <th>Born</th>
                <th>Salary</th>
                <th>Prior to NBA/Country</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="zui-sticky-col">DeMarcus Cousins</td>
                <td class="zui-sticky-col2">15</td>
                <td>C</td>
                <td>6'11"</td>
                <td>08-13-1990</td>
                <td>$4,917,000</td>
                <td>Kentucky/USA</td>
            </tr>
            <tr>
                <td class="zui-sticky-col">Isaiah Thomas</td>
                <td class="zui-sticky-col2">22</td>
                <td>PG</td>
                <td>5'9"</td>
                <td>02-07-1989</td>
                <td>$473,604</td>
                <td>Washington/USA</td>
            </tr>
            <tr>
                <td class="zui-sticky-col">Ben McLemore</td>
                <td class="zui-sticky-col2">16</td>
                <td>SG</td>
                <td>6'5"</td>
                <td>02-11-1993</td>
                <td>$2,895,960</td>
                <td>Kansas/USA</td>
            </tr>
            <tr>
                <td class="zui-sticky-col">Marcus Thornton</td>
                <td class="zui-sticky-col2">23</td>
                <td>SG</td>
                <td>6'4"</td>
                <td>05-05-1987</td>
                <td>$7,000,000</td>
                <td>Louisiana State/USA</td>
            </tr>
            <tr>
                <td class="zui-sticky-col">Jason Thompson</td>
                <td class="zui-sticky-col2">34</td>
                <td>PF</td>
                <td>6'11"</td>
                <td>06-21-1986</td>
                <td>$3,001,000</td>
                <td>Rider/USA</td>
            </tr>
        </tbody>
    </table>
</div>
like image 50
Akash Borana Avatar answered Oct 17 '22 08:10

Akash Borana