I have a 2×2 CSS grid that fills the browser viewport using fixed positioning. It looks like this:
----------------- | |xxxxxxxxxxxxx| ----------------- |x| | |x| | |x| | |x| | -----------------
The top row and left column each fit their content and are sized auto
. The bottom row and right column are used to occupy any remaining space and have size 1fr
respectively.
The northeast and southwest cells (filled with Xs in the image) are both scrollable Flex parent cells. They contain any number of child elements, and that number may change dynamically. I set the overflow
property of each property to auto
and style the scrollbar using these rulesets:
.scrollable::-webkit-scrollbar { height: 10px; width: 10px; } .scrollable::-webkit-scrollbar-thumb { background: darkgray; } .scrollable::-webkit-scrollbar-track { background: #666666; }
When I load the page in Chrome on macOS, the scrollbar overlays the contents of the southwest cell. Then when I resize the browser window, the left column expands to accommodate the scrollbar (which already appears as an overlay) and it moves to the right of the content.
I want the scrollbar to not cover the content at all, and I'd really like to eliminate layout shifting on irrelevant resize events. Do you know how I can achieve these goals?
This codepen is a minimal reproducible example. However, it doesn't look consistent to me. When I reload the page, sometimes the scrollbar is covered and sometimes the column is wide enough for the scrollbar to show up to the right of the content. I'm seeing the wrong coverage and sizing being more consistent on this standalone page.
To prevent this, the grid needs to update its dimensions and take scrollbars into account. Another question is how to do this using only css. I thought of applying
animation
forgrid-template-columns
and it seems to work:If you want to use JavaScript: