[css-values] Add the `fr` (flexible length) unit for general use as a length unit
The fr unit provided by CSS Grid is phenomenally useful. With it, you can specify in essence the portion of the "leftover space" that a grid track should occupy. This way, if you have a track that's 5em tall and another that's 60px tall, you can add a track that uses all of the remaining vertical space by specifying a height of 1fr. If you want to have an element that uses 20% of the leftover space, you can specify a height of 0.2fr. Then another track of 0.8fr would use whatever space is still unused at that point, resulting in a container that's completely full vertically.
This would be amazing if it could be generally applied to lengths in CSS. You could then have an element of a fixed height of like 10em and then throw down an element of height 1fr that would magically fill the remaining space. And of course fractional heights thereof, just like in the case of grids.
Currently, section 7.2.3 of the Grid Layout spec says that fr isn't a length and isn't compatible with lengths, and so forth, but it seems to me that this is something that should change.
While you can theoretically do this using calc() and percentage units, the results are mixed at best, and require more maintenance as you apply changes to your styles or add and remove elements in the container.
This would potentially solve a vast array of frustrating and tedious layout issues, would make producing well-structured and reliable stylesheets easier, and would introduce consistency, since having one set of CSS properties support a length unit that the rest of CSS doesn't offer is potentially confusing, and seems like an unnecessary distinction.
In general, if you let elements change their size according to the leftover space, this will affect the leftover space, creating a circularity!
In Grid this is avoided because fr units can only be used for sizing tracks, not items. And track sizes can depend on the intrinsic contributions of the items, but not on their final sizes.
I'm not convinced that this would work well in all layout models. Specifically, in flexbox, how would this play with flex-grow and flex-shrink?
Also, if you turn <flex> into a <length>, then you can use things like calc(1fr - 1px), which are not clear.
@a2sheppy We did (very) briefly consider doing that, but fr units hook into the layout algorithms in very particular ways, and it was clear that just treating it as a <length> would not work.
If there are specific cases where you think an fr unit would be useful, though, we can look into adding that functionality for that case. Would need more specific examples, though, of where you're running into problems (that wouldn't be better solved by switching into grid/flex layout modes). :)
Obviously, it would only be usable in contexts in which the parent had a fixed size that couldn't be affected by the size of its children. If I have a container element whose width is set so as not to be affected by its children (which should be the case if you set it explicitly to a particular value or to a percentage of its parent container's size, generally), then it should be possible to use fr units.
If the engine finds that the fr unit can't be safely used, then perhaps falling back to treating it as a percentage would work (that is, multiply the fr unit by 100 to convert it to a percentage). It would be roughly equivalent, though not precise and may not be quite what the developer is hoping for, but it would be better than failing entirely or the renderer going into some kind of infinite loop.
In my case, I had a grid that included cells that contained a heading and a <div> that needed to fill the remainder of the space. So I wanted to simply specify the height of the <div> as 1fr in order to let the browser know to just use all remaining space below the heading. This would have allowed it to adapt if the heading were long enough to wrap lines, or to adapt to slightly different line heights as the styles change over time. Setting the height to 100% did not present the desired results (I don't recall what happened but it certainly wasn't what I was shooting for).
In the end I wound up having to put the headings in separate grid cells, which was an annoying solution for various reasons, and IMO shouldn't have been necessary.
In another case, elsewhere in the same example, I had a block in which I wanted to have a heading, a couple of paragraphs of introductory text, and then below that an <iframe> occupying the remainder of the block. Being able to use fr units would have gotten me the result I wanted more quickly and easily than any other approach I played with, since I could simply have let the heading and intro content take whatever space it needed then used height: 1fr on the <iframe>.
Usefulness becomes more pronounced if you need to split the available space among, say, three objects. Anyway, that's the gist of it.
Along similar lines, being able to use fr units in calc() would be enormously helpful.
I could be misunderstanding your examples, but they both sound to me like they are solvable with Flexbox.
You can use flex-grow, flex-shrink, and flex-basis to control how the items fill the space. By setting display: flex on your grid item, you can make the heading take only as much space as it needs and make the <div> to fill the rest of the space.
Splitting the space precisely between multiple objects is a bit more complicated. I've never tried to use Flexbox in that manner.
Unfortunately, as others have stated, this is generally not possible. It's adding entirely new complex and invasive capabilities to layout modes that are not prepared for them. Flexbox is already pretty complex internally, and it doesn't have to deal with margin collapsing, or floats, or table row/column balancing. ^_^
In both your examples, it does seem like using a vertical flexbox would have done what you wanted.
I'm going to go ahead and close this issue, as I don't see it as being reasonably possible. Flexibility is something that requires very careful design, as I and the other editors have discovered over the last several years of writing the spec for Flexbox and Grid.