Post pobrano z: Holy Albatross with Widths
Heydon’s Holy Albatross is a technique to have a row of elements break into a column of elements at a specific width. A specified parent width, not a screen width like a media query would have. So, like a container query (ya know, those things that don’t exist yet that we all want).
I’ve used it before, although it was pointed out to me that using it only on two elements isn’t really necessary, and that the Holy Albatross is most useful when working with three or more elements.
The original article kind didn’t get into setting the widths on the horizontal row of elements (say one of them needs to be larger than the others), but the follow-up article has a demo in it showing that
flex-grow can be used to do exactly that. But Xiao Zhuo Jia notes that it’s not exactly a great system:
Problem is, it’s very difficult to set width to your non-stacked column, because the width is taken over by the hack.
One suggestion by Heydon is to use flex-grow, the problems are:
1. It is a very unintuitive way to set width – for a 3 column layout, you want 1 column to be 50% wide, you have to set flex-grow to 2.333
2. You have to know the number of total columns and set other columns’ flex-grow value accordingly
The other method is to use min-width and max-width, as shown by this codepen. I don’t believe
max-width: 100%is needed as anything larger than 100% will be changed to 100% due to flex-shrink, so really we’re dealing with min-width.
The problem with this method is that we have to set min-width for all columns, or else flex-grow will take over and expand the column beyond the min-width we’ve set.
None of this is fun.
I poked around a bit and I found that you can have your cake and eat it too…
Xiao Zhuo Jia calls the Unholy Albatross. Check out the How does it work? part of the article to see the great CSS trickery. It has to do with using the
max() function and CSS custom properties with fullbacks. It still feels very in the spirit of the Holy Albatross and allows you to set the width (via
--width) on any given element with a pixel value or ratio. Plus, it supports gaps.
You can support CSS-Tricks by being an MVP Supporter.