I have a table with 10 columns. I want the header row, first 3 columns and last column sticky.
I got the header row and last column sticky but can’t get the first 3 to stick. here is the CSS
That was me, and it is. I searched codepen earlier but couldn’t find it, thought maybe it had been deleted, that’s why I asked. I don’t see/understand the need for the JavaScript, surely the non-sticky column width’s would be set to fit-contents. That was my intention at least.
Thanks again.
If I remember correctly the problem with the sticky left and right columns apart from the first ones that are left:0 or right:0 is that you have to know the width of the column.
If the columns are fixed width at say 200px wide you can then say left:200px and left:400px to make them sticky. If on the other hand the columns are fluid then you have no measurement on which to apply the left position.
My example will work without js but the table columns will be fixed width.
I’ll have another look later today and see if there are any other ways that i missed.
I doubt you missed anything. Visually it looks great and works perfectly. Only change would be to add CSS fixed width and fit-content to the th’s and td’s, and disregard the JavaScript, no need for it.
Yes you could just remove the js as I said which is only there to make the table more fluid. Here’s a version with the js removed and a few tweaks for comfort.
The scrolling columns will fit the content if it forces the width otherwise text just wraps evenly. The sticky columns do need a fixed width though as mentioned so you can offset the sticky
That’s basically the same as my example except you got the z-index wrong on the header and it overlaps the other headers.
Using the version emmVXXQ on my laptop, col2 scrolls over col1, then col3 scrolls over col2. So only col1 is actually sticky. With version bNNLZNp, col2 and col3 scroll over and past col1, again only col1 is sticky. In codepen, col2 and col3 both move and partly overlap col1 and col2. What causes this to happen?
Ah ok. Without the js working out the width we need to include the padding and borders in the manual left offset. I’ve added a variable and added it to this demo.
You’ll have to let me know what browsers you see a problem with but I have only checked chrome at the moment but will look at others shortly.
The vertical borders between cols 1, 2 and 3 are solid, half of the horizontal borders are not. When scrolling horizontal, I can see the horizontal borders on the non-sticky columns as they scroll behind the sticky cols. Is this to do with borders or padding? That is the only issue. Otherwise,
Is that happening in my demos. I can’t see it or I’m looking at the wrong thing
You shouldn’t be able to see anything under the coloured background of the sticky columns unless you have the sticky left position out by a px or 2 which might happen if you reduced the borders or padding but didn’t change the offset etc. The non sticky columns slide behind the sticky columns so as long as all things add up they should remain invisible.
It happens in the last demo you posted bNNLZNp Between rows 2 and 3, rows 4 and 5, rows 6 and 7, and continuing this pattern, only for the first three columns.
It is hard to see it on sitepoint because of its height, with only one row visible. But its evident in the demo if you scroll vertically, then horizontally. Even more evident full screen. It wouldn’t be affected by sticky left, as its a vertical gap. So padding top and/or bottom likely the issue.
This is what I am seeing after a scroll vertical, then horizontal. Not all the horizontal borders are solid. It’s between rows 1 and 2, 3 and 4, 5 and 6 etc. Same applies between cols 1 and 2.
The issue with transparent border, if that is the issue, presents in sticky: V6 in codepen; not in the live version you just posted. I only changed the root --cell-width.