P粉4404536892023-08-22 12:47:17
I've found this has caused me trouble many times over the past few years with both flex and grid, so I suggest the following:
* { min-width: 0; min-height: 0; }
If you need this behavior, just use min-width: auto
or min-height: auto
.
In fact, you can also add box-sizing to make all layouts more reasonable:
* { box-sizing: border-box; min-width: 0; min-height: 0; }
Does anyone know if there are any strange consequences? In several years of using the above method, I haven't had any problems. In fact, I can't think of any situation where I would want to layout from content outwards into flex/grid, rather than from flex/grid inward to content --- and if such a situation exists, it's certainly rare. So this feels like a bad default. But maybe I'm missing something?
P粉7524794672023-08-22 10:57:07
You have encountered the default settings of flexbox.
Flex items cannot be smaller than the size of their content on the main axis.
The default setting is...
min-width: auto
min-height: auto
...applies to flex items in row and column directions respectively.
You can set the flex item to:
min-width: 0
min-height: 0
overflow: hidden
(or any other value except visible
)Aboutauto
value...
in other words:
min-width: auto
and min-height: auto
The default is only applicable when overflow
is visible
. overflow
is not visible
, the value of the min-size attribute is 0
. overflow: hidden
can replace min-width: 0
and min-height: 0
. as well as...
min-height: auto
by default. Nested Flex Container
If you are working with flex items on multiple levels of your HTML structure, you may need to override the default min-width: auto
/ min-height: auto## on higher level items #.
min-width: auto can prevent the nested item below with
min-width: 0 from shrinking.
Chrome vs. Firefox / Edge
Since 2017, Chrome appears to either (1) revert to the default values ofmin-width: 0 /
min-height: 0 or (2) A mysterious algorithm automatically applies the default value of
0 in certain situations. (This is probably what they call
intervention.) So many people see their layouts (especially the expected scrollbars) working as expected in Chrome, but not in Firefox/ Doesn't work in Edge. For more detailed information, see here: flex-shrink differences between Firefox and Chrome
IE11
As noted in the specification, the auto
values for the min-width
and min-height
properties are "new". This means that some browsers may still render the 0
value by default because they implemented flex layout before the value updated, and because 0
is in CSS 2.1 The initial values of min-width
and min-height
. IE11 is one of them. Other browsers have been updated to the newer auto values defined in the flexbox specification
.
.container { display: flex; } .col { min-height: 200px; padding: 30px; word-break: break-word } .col1 { flex: 1; background: orange; font-size: 80px; min-width: 0; /* NEW */ } .col2 { flex: 3; background: yellow } .col3 { flex: 4; background: skyblue } .col4 { flex: 4; background: red }
<div class="container"> <div class="col col1">Lorem ipsum dolor</div> <div class="col col2">Lorem ipsum dolor</div> <div class="col col3">Lorem ipsum dolor</div> <div class="col col4">Lorem ipsum dolor</div> </div>