Why can't flex items shrink to less than the content size?
P粉432906880
P粉432906880 2023-08-21 22:36:03
0
2
434

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.

I tried using word-break: break-word 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.

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)

I know that setting the font size and column padding to smaller values will help... but is there any other solution?

I cannot use overflow-x: hidden.

JSFiddle


.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 }
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor
Lorem ipsum dolor


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 usemin-width: autoormin-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 exceptvisible)

    Flexbox specification

    Aboutautovalue...

    in other words:

    • min-width: autoandmin-height: autoThe default is only applicable whenoverflowisvisible.
    • If the value ofoverflowis notvisible, the value of the min-size attribute is0.
    • Therefore,overflow: hiddencan replacemin-width: 0andmin-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 defaultmin-width: auto/min-height: auto## on higher level items #.

    Basically, a higher level flex item with

    min-width: autocan prevent the nested item below withmin-width: 0from 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 }
    Lorem ipsum dolor
    Lorem ipsum dolor
    Lorem ipsum dolor
    Lorem ipsum dolor
      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!