Why can't flex items shrink to less than the content size?
P粉432906880
P粉432906880 2023-08-21 22:36:03
0
2
459
<p>I have 4 flexbox columns and everything works fine, but when I add some text to one 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 resize the columns to a very small width, the letters in the text are broken into multiple lines (one letter per row), but the width of the column will not be smaller than the size of a letter. </p> <p> Watch this video (at the beginning, the first column is the smallest, but when I resize the window, it becomes the widest column. I just want to always respect the flex settings; the flex size is 1:3:4 :4)</p> <p>I know that setting the font size and column padding to smaller values ​​will 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>
P粉432906880
P粉432906880

reply all(2)
P粉440453689

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粉752479467

Automatic minimum size for Flex projects

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)

Flexbox specification

Aboutautovalue...

in other words:

  • min-width: auto and min-height: autoThe default is only applicable when overflow is visible.
  • If the value of overflow is not visible, the value of the min-size attribute is 0.
  • Therefore, overflow: hidden can replace min-width: 0 and min-height: 0.

as well as...


You have applied min-width: 0 but the project still won't shrink?

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 #.

Basically, a higher level flex item with

min-width: auto can prevent the nested item below with min-width: 0 from shrinking.

Example:

  • Flex items cannot be shrunk smaller than their contents
  • Adapt child elements to parent elements
  • white-space CSS property conflicts with flex layout

Browser rendering considerations


Revised Demo

.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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!