Why don't flex items shrink beyond content size?
P粉242126786
2023-08-27 16:35:08
<p>I have 4 Flexbox columns and everything works fine, but when I add some text to the column and set it to a large font size, it makes the column wider than it should be due to the Flex property. </p>
<p>I tried using <code>word-break:break-word</code> and it helped, but when I resized the column to a very small width, the letters in the text were broken into multiple rows (one letter per row), but the column width will not be less than the size of a letter. </p>
<p>Watch this video
(Initially, the first column is the smallest, but when I resize the window, it is the widest column. I just want to always respect the Flex settings; Flex size 1:3:4:4) </p>
<p>I know, setting the font size and column padding to smaller would help...but is there any other solution? </p>
<p>I cannot use <code>overflow-x:hidden</code>. </p>
<p>JSFiddle</p>
<p><br /></p>
<pre class="brush:css;toolbar:false;">.container {
display: flex;
width: 100%
}
.col {
min-height: 200px;
padding: 30px;
word-break: break-word
}
.col1 {
flex: 1;
background: orange;
font size: 80px
}
.col2 {
flex: 3;
background: yellow
}
.col3 {
flex: 4;
background: skyblue
}
.col4 {
flex: 4;
background: red
}</pre>
<pre class="brush:html;toolbar:false;"><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></pre>
<p><br /></p>
I find that Flex and grid have been bugging me for years, so I propose the following:
Then if you need this behavior just use
min-width: auto
ormin-height: auto
.In fact, you can also add box size to make all layouts more reasonable:
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?
Automatic minimum size for Flex projects
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, exceptvisible
)Flexbox Specification
About
auto
value...in other words:
min-width: auto
andmin-height: auto
The default values only apply whenoverflow
is visible
.overflow
value is notvisible
, the value of the min-size attribute is0
.overflow: hide
can replacemin-width: 0
andmin-height: 0
.besides...
min-height: auto
by default.You've applied min-width: 0 but the project still doesn't shrink?
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 withmin-width: 0
from shrinking.Example:
Browser rendering instructions
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 The0
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 ChromeIE11
As noted in the specification, the
auto
value for themin-width
andmin-height
properties is "new". This means that some browsers may still render the0
value by default because they implement Flex layout before updating the value, and0
is the initial value ofmin CSS 2.1. IE11 is such a browser. Other browsers have been updated to newer
auto中定义的 auto
values defined in the flexbox specification.Modified demo