-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css-env-2] env(scrollbar-inline-size) #4691
Comments
This is related to #4329. Basically, whatever solutions we come up with in the vertical direction will also be applied in the horizontal direction. We discussed these issues today at the CSSWG face-to-face in A Coruña. Scrollbars are similar to keyboards. And yes, we were settling on env() variables for the dynamically changing measurements (and not another option). There are pros & cons, usecases & objections. |
The scrollbar size can be controlled by the user, so probably should be I presume this is not meant to be element-dependent, right? |
This was also discussed today at the CSS F2F as part of #4674 |
|
With [css-overflow-4] I mean, getting the size of the scrollbar is handy, but as we can achieve the same behavior with /* Keep content centered by manually offsetting the padding-inline-start */
.keep-content-centered-using-envvar {
padding-inline-start: env(scrollbar-inline-size);
}
/* Keep content centered using scrollbar-gutter */
.keep-content-centered-using-scrollbar-gutter {
scrollbar-gutter: stable both-edges;
} Or are there any other use-cases for Also note that manually taking /* This looks is a bad combination to do, as you'll end up with a double gap at the edge opposing the scrollbar */
.bad-combination {
padding-inline-start: env(scrollbar-inline-size);
scrollbar-gutter: stable both-edges;
} |
I still have a use-case and that is when making elements horizontally break out from their parent elements, extending them back to the full available viewable area + combined with programmatic scrolling. This is the pretty much standardized™ example code to make child elements extend back to full width, regardless of their parent's width (imagine a hero image in an article): :root {
overflow-x: hidden;
overflow-y: scroll; /* could also be done with scroll-gutter */
}
.outer {
width: 100%;
max-width: 40rem;
margin: 0 auto;
}
.inner {
width: 100vw;
margin-inline: calc((100vw - 100%) / -2);
} To my knowledge, this is still the way to go to do these types of things when you work with components and you don't know in what type of element the But Here is a demo of that behavior: https://codepen.io/Schepp/full/jOYdQbv Trying to contain scrolling with Having .inner {
--viewable-area: calc(100vw - env(scrollbar-inline-size));
width: var(--viewable-area, 100vw);
margin-inline: calc((var(--viewable-area, 100vw) - 100%) / -2);
} PS: In theory, one could also trigger page jumping by focusing something close to the right border of the page, and then again something close to the opposing border. |
Okay, so the so far unknown to me If you apply this to e.g. body {
width: 100%;
overflow: clip;
} |
Hello, another use case is to to have a position: fixed div stuck on the right of the screen cleanly. |
@Schepp Re If yes, then |
@simevidas yes, I would want the element to then take up the full space up to the right edge of the browser. I think what I mean with scrollbar size is how much in-flow space it takes up. A lot of scrollbars just overlay the underlying content, only appearing when triggered, and would then result in the Here is the code I currently have to use to find out myself and fix my problems via custom property // the following gets all executed right after opening <body> tag
const elem = document.createElement('div');
const referenceElem = document.createElement('div');
const measureElem = document.createElement('div');
Object.assign(elem.style, {
position: 'absolute',
width: '100%',
visibility: 'hidden',
});
Object.assign(referenceElem.style, {
overflow: 'scroll',
width: '50px',
});
const determineWidth = () => {
const scrollbarInlineSize = 50 - measureElem.offsetWidth;
window.sessionStorage.set('scrollbar-inline-size', scrollbarInlineSize);
document.documentElement.style.setProperty('--scrollbar-inline-size', `${scrollbarInlineSize}px`);
};
elem.appendChild(referenceElem);
referenceElem.appendChild(measureElem);
document.body.appendChild(elem);
const scrollbarInlineSize = window.sessionStorage.get('scrollbar-inline-size');
if (scrollbarInlineSize !== null) {
document.documentElement.style.setProperty('--scrollbar-inline-size', `${scrollbarInlineSize}px`);
} else {
window.requestAnimationFrame(() => determineWidth);
} (imagine a little more complexity to it and this being the gist) |
Would copy my comment from #6026, as it fits this issue more:
|
There are two separate problems:
@Schepp’s function answers the first question. The proposed That’s good, but I’m also interested in the second question. Being able to answer this question in CSS would allow developers to solve the
As you can see, developers cannot solve the It does not look like the second question will be answered in CSS anytime soon (see #6026 (comment)). I think the best bet to make |
Well, maybe it will, as people in the Chrome/ium team are experimenting with State Queries, which are meant to reflect exactly these types of things. |
Dropping this here, which was proposed at this year‘s F2F in Cupertino: https://gist.github.com/bramus/bcca5788d8ced82837180b7a15760c84 Essentially you need 3 things:
|
As a sidenote, with an But yes, this requires an additional wrapper around everything, and is more cumbersome. But can be a good viable workaround as soon as (posting as a way to demonstrate a future workaround, and as something people could use for testing how the potential APIs would work in CSS) |
Given I think the main one is that 100vw should just account for scrollbar width. That seems to be the primary case where people need to account for scrollbar width. Though I understand that could cause a cycle which seems to be the reason against it? |
Maybe a second env var that exposes the thin size – e.g. |
Would that rely on knowing the current value of the property? Style container queries feel a bit ott for that but perhaps it's fine? |
I really do think that adding another env like that is complicating, specially if you consider the interest in having the least edits every time to get a change is done. Or worse, to do it right, with feature detection, you must actually check if the browser supports it and have an alternative for it... What a complete mess that would be... |
I believe a proper solution needs to be context aware. So basically the opposite of what @emilio wrote earlier because use cases want to consider the actual scrollbar width in their calculations. Sebastian |
Scrollbars are a system provided, dynamic, and out-of-developer's control component that can "get in the way" by consuming unpredictable amounts of space. It'd be nice if the platform provided an environment variable that held the contextual system width value for scrollbars so that developer could use
calc()
to mitigate unpredictability.I've gently proposed using logical property syntax for the env variable name, since I feel this is the language used to talk about sizing on the web:
env(scrollbar-inline-size)
Here's a great bad example of how folks are working around this today:
(aka, negative margins)
All in all, having access to the scrollbar width would give folks the values they need to gracefully handle the dynamic aspects of scrollbars with just CSS.
I assume there's aspects to this work that overlap with custom scrollbars, so this env var would need to be a property that can update at runtime yeah? Help me think out the rest of the fringe edge cases, as well as use cases where scrollbar width would be super helpful in your layouts/ui. Thanks!
The text was updated successfully, but these errors were encountered: