Why can't flex items shrink below content size?
P粉038161873
P粉038161873 2023-10-15 22:06:39
0
2
550

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.

I tried usingword-break:break-wordand it helped, but when I resized the columns to a very small width, the letters in the text were broken into multiple lines (each line one letter), and the width of the column will not be less than the size of one letter.

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)

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

I can't useoverflow-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粉038161873
P粉038161873

reply all (2)
P粉884667022

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 usemin-width: autoormin-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粉557957970

    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

    Aboutautovalue...

    in other words:

    • min-width: autoandmin-height: autoThe default values only apply whenoverflowis visible.
    • If theoverflowvalue is notvisible, the value of the min-size attribute is0.
    • Therefore,overflow: hidecan replacemin-width: 0andmin-height: 0.

    besides...


    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 defaultmin-width: auto/min-height: auto code> located higher level items.

    Basically, a higher-level Flex project withmin-width: autoprevents projects nested below withmin-width: 0from shrinking.

    Example:


    Browser rendering instructions

    • Chrome vs. Firefox / Edge

      Since at least 2017, Chrome seems to have either (1) reverted to themin-width: 0/min-height: 0defaults, or (2) based on a mysterious algorithm The0default value is automatically applied in some cases. (This is probably what they callintervention.) 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, theautovalue for themin-widthandmin-heightproperties is "new". This means that some browsers may still render the0value by default because they implement Flex layout before updating the value, and0is the initial value ofminCSS 2.1.IE11 is such a browser.Other browsers have been updated to newerauto中定义的 autovalues defined in the flexbox specification.


    Modified 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!