P粉1565327062023-08-28 13:19:46
I find that Flex and grid have been bugging me for years, so I propose the following:
* { min-width: 0; min-height: 0; }
Then if you need this behavior just use min-width: auto
or min-height: auto
.
In fact, you can also add box size to make all layouts more reasonable:
* { box-sizing: border-box; min-width: 0; min-height: 0; }
Does anyone know if there are any weird consequences? I haven't had any problems in a few years of using a mix of the above methods. In fact, I can't think of any cases where I would want to layout from content outwards into the Flex/Grid, rather than layout from the Flex/Grid inward into the content --- of course if they exist, they are rare. So this feels like a bad default. But maybe I'm missing something?
P粉0432953372023-08-28 12:43:23
You have encountered Flexbox default settings.
Flexible items cannot be smaller than the size of their content along the main axis.
The default value is...
Minimum Width: Automatic
Minimum Height: Automatic
...for elastic items in row and column directions respectively.
You can override these defaults by setting the flex item to:
Minimum width: 0
Minimum height: 0
Overflow: hidden
(or any other value, except visible
) Aboutauto
value...
in other words:
min-width: auto
and min-height: auto
The default values only apply when overflow
is visible
.overflow
value is not visible
, the value of the min-size attribute is 0
. overflow: hide
can replace min-width: 0
and min-height: 0
. besides...
min-height: auto
by default. Nested Flex Containers
If you are working with a Flex project on multiple levels of the HTML structure, you may want to override the default min-width: auto
/ min-height: auto code> located higher level items.
Basically, a higher-level Flex project with min-width: auto
prevents projects nested below with min-width: 0
from shrinking.
Example:
Chrome vs. Firefox / Edge
Since at least 2017, Chrome seems to have either (1) reverted to the min-width: 0
/ min-height: 0
defaults, or (2) based on a mysterious algorithm The 0
default value is automatically applied in some cases. (This is probably what they call intervention.) So many people are seeing their layouts (especially the required scrollbars) working as expected in Chrome, but Not so in Firefox/Edge. This issue is covered in more detail here: flex-shrink differences between Firefox and Chrome
IE11
As noted in the specification, the auto
value for the min-width
and min-height
properties is "new". This means that some browsers may still render the 0
value by default because they implement Flex layout before updating the value, and 0
is the initial value of min CSS 2.1. IE11 is such a browser. Other browsers have been updated to newer
auto中定义的 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>