Wanted in CSS: Shrinking when Overflowed and Scrolling.
A feature that would make the edge between almost-scrolling and scrolling a bit nicer.
Sometimes I want an element to have its height restricted, so that if it’s too long it will scroll. But, if it does scroll, I want its height to be more restricted. In other words, if it’s going to scroll, it should scroll a good bit, not just a single line. I’ll throw an example here:
Integer pellentesque turpis at tellus mattis faucibus. Praesent porta, ipsum quis mollis posuere, risus orci faucibus purus, ut dapibus risus urna vel sem. Nam id nulla ac mauris fermentum sodales id posuere justo. Donec dignissim risus in pulvinar convallis. Etiam pharetra arcu lacus, tempus tincidunt purus aliquam ac. Sed ligula sapien, egestas quis ornare ac, commodo ut lorem. Integer id tincidunt augue. Nunc consectetur orci eget velit tincidunt bibendum. Nulla a ante a velit varius eleifend vitae id lacus.
Nulla iaculis interdum quam, vel sodales augue faucibus ut. Aliquam egestas tellus vitae diam lobortis, vel lobortis risus vestibulum. Duis cursus enim a mauris hendrerit, in viverra urna lobortis. Donec pellentesque semper gravida. Vestibulum maximus convallis viverra. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris velit felis, iaculis pharetra nisi sed, laoreet ullamcorper nibh. Duis placerat sodales sodales. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec sagittis, lorem ac blandit condimentum, neque magna dignissim lacus, in hendrerit mauris ex ac ipsum. Nullam sagittis, tellus nec tincidunt eleifend, justo felis vulputate ligula, eu egestas mauris orci sit amet dui. Cras sed est eu lacus tincidunt congue.
Donec semper tempor dolor, eget sodales libero laoreet vitae. Curabitur aliquam libero id neque auctor, sit amet pulvinar leo interdum. Phasellus sed erat id augue placerat facilisis. Aenean ut tortor vel libero feugiat consequat a sit amet lacus. Pellentesque porttitor tincidunt gravida. Curabitur vitae pellentesque mauris. Proin sodales blandit leo. Morbi suscipit consectetur felis in hendrerit. Duis libero elit, posuere ac sagittis in, sodales id enim. Sed auctor sapien dui, a gravida nibh faucibus non.
Compare that behavior with the two containers here:
Integer pellentesque turpis at tellus mattis faucibus. Praesent porta, ipsum quis mollis posuere, risus orci faucibus purus, ut dapibus risus urna vel sem. Nam id nulla ac mauris fermentum sodales id posuere justo. Donec dignissim risus in pulvinar convallis. Etiam pharetra arcu lacus, tempus tincidunt purus aliquam ac. Sed ligula sapien, egestas quis ornare ac, commodo ut lorem. Integer id tincidunt augue. Nunc consectetur orci eget velit tincidunt bibendum. Nulla a ante a velit varius eleifend vitae id lacus.
Nulla iaculis interdum quam, vel sodales augue faucibus ut. Aliquam egestas tellus vitae diam lobortis, vel lobortis risus vestibulum. Duis cursus enim a mauris hendrerit, in viverra urna lobortis. Donec pellentesque semper gravida. Vestibulum maximus convallis viverra. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris velit felis, iaculis pharetra nisi sed, laoreet ullamcorper nibh. Duis placerat sodales sodales. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec sagittis, lorem ac blandit condimentum, neque magna dignissim lacus, in hendrerit mauris ex ac ipsum. Nullam sagittis, tellus nec tincidunt eleifend, justo felis vulputate ligula, eu egestas mauris orci sit amet dui. Cras sed est eu lacus tincidunt congue.
Donec semper tempor dolor, eget sodales libero laoreet vitae. Curabitur aliquam libero id neque auctor, sit amet pulvinar leo interdum. Phasellus sed erat id augue placerat facilisis. Aenean ut tortor vel libero feugiat consequat a sit amet lacus. Pellentesque porttitor tincidunt gravida. Curabitur vitae pellentesque mauris. Proin sodales blandit leo. Morbi suscipit consectetur felis in hendrerit. Duis libero elit, posuere ac sagittis in, sodales id enim. Sed auctor sapien dui, a gravida nibh faucibus non.
Integer pellentesque turpis at tellus mattis faucibus. Praesent porta, ipsum quis mollis posuere, risus orci faucibus purus, ut dapibus risus urna vel sem. Nam id nulla ac mauris fermentum sodales id posuere justo. Donec dignissim risus in pulvinar convallis. Etiam pharetra arcu lacus, tempus tincidunt purus aliquam ac. Sed ligula sapien, egestas quis ornare ac, commodo ut lorem. Integer id tincidunt augue. Nunc consectetur orci eget velit tincidunt bibendum. Nulla a ante a velit varius eleifend vitae id lacus.
Nulla iaculis interdum quam, vel sodales augue faucibus ut. Aliquam egestas tellus vitae diam lobortis, vel lobortis risus vestibulum. Duis cursus enim a mauris hendrerit, in viverra urna lobortis. Donec pellentesque semper gravida. Vestibulum maximus convallis viverra. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris velit felis, iaculis pharetra nisi sed, laoreet ullamcorper nibh. Duis placerat sodales sodales. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec sagittis, lorem ac blandit condimentum, neque magna dignissim lacus, in hendrerit mauris ex ac ipsum. Nullam sagittis, tellus nec tincidunt eleifend, justo felis vulputate ligula, eu egestas mauris orci sit amet dui. Cras sed est eu lacus tincidunt congue.
Donec semper tempor dolor, eget sodales libero laoreet vitae. Curabitur aliquam libero id neque auctor, sit amet pulvinar leo interdum. Phasellus sed erat id augue placerat facilisis. Aenean ut tortor vel libero feugiat consequat a sit amet lacus. Pellentesque porttitor tincidunt gravida. Curabitur vitae pellentesque mauris. Proin sodales blandit leo. Morbi suscipit consectetur felis in hendrerit. Duis libero elit, posuere ac sagittis in, sodales id enim. Sed auctor sapien dui, a gravida nibh faucibus non.
Cras nibh nulla, pulvinar vel lectus non, posuere scelerisque dolor. Quisque eget maximus sapien. Sed eu felis risus. Aliquam ac mauris porttitor, molestie odio sit amet, pellentesque erat. Pellentesque id lacus ornare, viverra massa at, mattis sapien. Nunc ullamcorper purus vitae risus viverra, sed volutpat turpis rhoncus. Cras at magna in sapien gravida blandit quis vel nulla. Duis a eros sed felis cursus tempus. Duis tincidunt elementum ex, vitae laoreet libero accumsan et. Sed id metus quis lacus sodales mollis et at lorem. Praesent sodales nisi ex, eget varius arcu facilisis ac. Nullam suscipit vestibulum tortor vel egestas. In pulvinar odio sit amet urna fermentum, gravida tristique ligula fermentum. Duis euismod dapibus fringilla.
I believe styling to automatically behave as above is currently impossible without Javascript. The closest feature I could find is the scroll-state feature of container queries. Container queries are restricted to avoid layout cycles (i.e., infinite loops), so there’s probably no good way to work around that and accomplish the above. Neither Firefox nor Safari implements them yet anyway. See MDN: CSS container-type (Browser compatibility).
The first alternative is to set a max-height cutoff and be happy with it. But if we need to scroll, we should need to scroll more than a little bit. If something only scrolls to show the last line, as in the first example? That’s a waste of everyone’s time! It’s ugly and unprofessional. If there’s room, we’d prefer the block be a little bit bigger in those cases, to avoid a short scroll.
The other alternative is to make the element’s max-height extra-short, so that it scrolls in most cases. Again, this is worse. If there’s enough space, it shouldn’t need to scroll, and we shouldn’t enforce scrolling to avoid this edge-case. Indeed, there’s got to be some sensible minimum ratio (I’d guess four-thirds) that a scrollable container should always hold compared to its height. The obvious case excepted: a full-height container, which couldn’t possibly not scroll once the content’s intrinsic height exceeds its own.
Assuming it were adopted as a new feature of CSS, it might look like:
div.maybe-overflow {
max-height: 600px; /* The longer case */
scroll-height: 400px; /* The scrolling case */
}This isn’t hard to implement. The container query limitations are their own challenge, but the browser can readily decide the layout height through its normal process, and if it’s more than height or max-height, it can choose to use scroll-height. Otherwise, it uses the calculated height, just like if there were no scroll-height. It shouldn’t need any kind of query-type declarations to do this. So I think it’s a missing feature. Whether it’s a missing feature enough designers would want, enough to merit inclusion in CSS and being added to browsers, is its own question.
For clarity:
- Calculate the height as normal (including
min-height,max-height, andheight). - If the calculated height would scroll and
scroll-heightis specified, use that. If the
scroll-heightconflicts with the maximum or minimum, consider it invalid and use the previously calculated height.
Should scroll-height ever expand the height (be bigger than what the calculated height would otherwise be)? I don’t think it would hurt anything, given the ability to add constraints with minimum and maximum. You would have to recognize it as scrolling even if it expanded to not need to, but it wouldn’t need to trigger a cycle. But I don’t see any obvious use for it, either. If a specification were to avoid it, it would probably be better to use the ratio instead of the explicit size? I do think there would be cases for a scroll-width equivalent, especially for containers that don’t directly hold text.
In all, it would be nice to have this available in CSS. There are design decisions that aren’t currently available for lack of tools to make them, at least without adding Javascript. While that’s fine for more complex behavior, simpler things that could be part of any designer’s kit can hopefully be added to stylesheets.