The only difference here is parent, styles of subtree are completely the same.
Grid calculates the width of first element (section
with green outline) correctly, but flex sets it to 0 and allows overflow (blue circle div
). Playing with overflow
or flex-shrink
on section
doesn't help.
Image may be NSFW.
Clik here to view.
Here is the code:
main { margin-top: .5em;}.flex { display: flex;}.grid { display: grid; grid-template-columns: auto auto 1fr;}section { outline: 1px solid green;}div { background: blue; border-radius: 50%; aspect-ratio: 1;}#height:checked ~ main div { height: 100%;}aside { height: 75px; background: #F00A;}
<input type="checkbox" id="height" checked="checked"><main class="flex"><section><div></div></section><aside>aside</aside></main><main class="grid"><section><div></div></section><aside>aside</aside></main>
What I expect
- Height of
aside
is taken as a height ofmain
(it works) - Height of
main
is used as height ofsection
because of flex/grid (it works) - Height of
section
is used as height ofdiv
because ofheight: 100%
(it works: you can check by unchecking checkbox - size ofdiv
changes to 0 and circle disappears if height is not set) - Height of
div
is used as width ofdiv
because ofaspect-ratio: 1
(it works: div is visible as a circle) - Width of
div
bounces back as width ofsection
becausediv
is in normal flow inside of thesection
(works withgrid
in Chrome but not in FF; never works withflex
: green frame is collapsed to 0 width insted of surrounding blue circle like in grid) - Width of
section
recalculates flex/grid layout and aside shifts to the right of it (works ingrid
)
Questions
Should flex
and grid
behave the same way in this example?
Do I misunderstand how it should work with flex
?
Is there a way to make flex
behave like grid
here?
I found out that in FF grid
works the same way as flex
- that means either FF or Chrome is wrong in calculating grid
- who of them and why?
About "100% of what?"
It's ok to specify height in percents for subchildren of horizontal flex: https://jsfiddle.net/w3m7Ldrk/
main { display: flex; gap: 1em; outline: 1px dotted red;}div { background: blue; width: 3em;}
<main><section><div style="height:25%"></div></section><section><div style="height:50%"></div></section><section><div style="height:75%"></div></section><section><div style="height:100%"></div></section><aside>1<br>2<br>3<br>4</aside><section><div style="height:150%"></div></section></main>
Also in original example blue circle does definetely have height of section
(green outline) only when checkbox is checked, so height: 100%
works fine. That means, regardless specifying height
on section
solves the problem, lack of specific height on it can't be an answer about why it behaves like that.