min-width rendering differently in flex-direction: row and flex-direction: column - html

I have a container with display: flex and flex-direction: row.
In that container there is a sub-container also with display: flex but with flex-direction: column.
The problem is if I add an input in the sub-container, the min-width of that input will be ignored.
This is the code where I tried several cases of input in flexbox:
form {
margin: 100px;
}
div.flex_ctn {
display: flex;
}
input {
flex: 1;
min-width: 40px;
}
div.column {
flex-direction: column;
}
div.row {
flex-direction: row;
}
div.sub_ctn {
flex: 1;
/*min-width:40px;*/
}
<form>
<div class="flex_ctn row">
<input />
</div>
<div class="flex_ctn column">
<input />
</div>
<div class="flex_ctn row">
<div class="flex_ctn column sub_ctn">
<input />
</div>
</div>
<div class="flex_ctn column">
<div class="flex_ctn row sub_ctn">
<input />
</div>
</div>
</form>
https://fiddle.jshell.net/s3gu32ku/2/
If you reduce the screen size, the 3rd line doesn't react like the others.
In the css you will see that the last line is set as comment. When that part is enabled you just have to reload and the issue disappears. So, perfect ! I have got the solution!
But that bothers me to use something that I don't understand ^^.
This would be great if someone can explain to me why that error occurs, why that line fix it, and also if there a better way to avoid that issue.

Generally speaking, flex items, by default, cannot be smaller than the size of their content.
More specifically, these are initial settings of flex items:
min-width: auto (applies in flex-direction: row)
min-height: auto (applies in flex-direction: column)
Even more specifically, take a look at the spec language:
4.5. Implied Minimum Size of Flex
Items
To provide a more reasonable default minimum size for flex items, this
specification introduces a new auto value as the initial value of
the min-width and min-height properties defined in CSS 2.1.
auto
On a flex item whose overflow is visible in the main axis, when
specified on the flex item's main-axis min-size property, specifies an
automatic minimum size. It otherwise computes to 0.
In other words, the minimum sizing algorithm applies only on the main axis.
Your input elements in column-direction containers don't get min-width: auto – because the main axis is vertical in those cases – so they shrink and won't overflow the container. You can see this behavior play out on your second input element. Reduce the screen size while viewing this demo.
The same thing happens with the third input, which is a child of a nested flex container with flex-direction: column... EXCEPT, this column-direction container is also a flex item of larger container with flex-direction: row.
This means the main axis of the nested container is horizontal and min-width: auto applies. As a result, this flex item will not shrink below the intrinsic width of the input. For an illustration, see the same demo from above.
Therefore, you need to override this default with min-width: 0 or overflow: hidden (demo).
And for the reasons explained above, the fourth input, contained in a nested row-direction flex container, will also need to have min-width: auto overridden (demo).
Related: Why doesn't flex item shrink past content size?

Related

Why does flex-basis doesn't work with flex-direction: row? [duplicate]

There have been questions and articles about this, but nothing conclusive as far as I can tell. The best summary I could find is
flex-basis allows you to specify the initial/starting size of the element, before anything else is computed. It can either be a percentage or an absolute value.
...which in itself doesn't say much about the behavior of elements with flex-basis set. With my current knowledge of flexbox I don't see why that couldn't describe width also.
I'd like to know how exactly flex-basis is different from width in practice:
If I replace width with flex-basis(and vice versa), what will change visually?
What happens if I set both to a different value? What happens if they have the same value?
Are there some special cases where using either width or flex-basis would have a significant difference to using the other?
How do width and flex-basis differ when used in conjunction with other flexbox styles, such as flex-wrap, flex-grow and flex-shrink?
Any other significant differences?
Edit/clarification: This question has been asked in a different format in What exactly flex-basis property sets? but I felt a more direct comparison or summary of the differences of flex-basis and width (or height) would be nice.
Consider flex-direction
The first thing that comes to mind when reading your question is that flex-basis doesn't always apply to width.
When flex-direction is row, flex-basis controls width.
But when flex-direction is column, flex-basis controls height.
Key Differences
Here are some important differences between flex-basis and width / height:
flex-basis applies only to flex items. Flex containers (that aren't also flex items) will ignore flex-basis but can use width and height.
flex-basis works only on the main axis. For example, if you're in flex-direction: column, the width property would be needed for sizing flex items horizontally.
flex-basis has no effect on absolutely-positioned flex items. width and height properties would be necessary. Absolutely-positioned flex items do not participate in flex layout.
By using the flex property, three properties – flex-grow, flex-shrink and flex-basis – can be neatly combined into one declaration. Using width, the same rule would require multiple lines of code.
Browser Behavior
In terms of how they are rendered, there should be no difference between flex-basis and width, unless flex-basis is auto or content.
From the spec:
7.2.3. The flex-basis property
For all values other than auto and content, flex-basis is resolved the same way as width in horizontal writing modes.
But the impact of auto or content may be minimal or nothing at all. More from the spec:
auto
When specified on a flex item, the auto keyword retrieves the value
of the main size property as the used flex-basis. If that value is
itself auto, then the used value is content.
content
Indicates automatic sizing, based on the flex item’s content.
Note: This value was not present in the initial release of Flexible
Box Layout, and thus some older implementations will not support it.
The equivalent effect can be achieved by using auto together with a
main size (width or height) of auto.
So, according to the spec, flex-basis and width resolve identically, unless flex-basis is auto or content. In such cases, flex-basis may use content width (which, presumably, the width property would use, as well).
The flex-shrink factor
It's important to remember the initial settings of a flex container. Some of these settings include:
flex-direction: row - flex items will align horizontally
justify-content: flex-start - flex items will stack at the start of the line on the main axis
align-items: stretch - flex items will expand to cover the cross-size of the container
flex-wrap: nowrap - flex items are forced to stay in a single line
flex-shrink: 1 - a flex item is allowed to shrink
Note the last setting.
Because flex items are allowed to shrink by default (which prevents them from overflowing the container), the specified flex-basis / width / height may be overridden.
For example, flex-basis: 100px or width: 100px, coupled with flex-shrink: 1, will not necessarily be 100px.
To render the specified width – and keep it fixed – you will need to disable shrinking:
div {
width: 100px;
flex-shrink: 0;
}
OR
div {
flex-basis: 100px;
flex-shrink: 0;
}
OR, as recommended by the spec:
flex: 0 0 100px; /* don't grow, don't shrink, stay fixed at 100px */
7.2. Components of
Flexibility
Authors are encouraged to control flexibility using the flex shorthand
rather than with its longhand properties directly, as the shorthand
correctly resets any unspecified components to accommodate common
uses.
Browser Bugs
Some browsers have trouble sizing flex items in nested flex containers.
flex-basis ignored in a nested flex container. width works.
When using flex-basis, the container ignores the sizing of its children, and the children overflow the container. But with the width property, the container respects the sizing of its children and expands accordingly.
References:
Chrome does not expand flex parent according to children's content
Flex item overflowing when using flex-basis
Difference between width and flex-basis
Flex-basis is being ignored when sizing nested flex containers.
flex-basis:100px does something different from width:100px+flex-basis:auto
Examples:
https://jsfiddle.net/t419zhra/ (source: #Dremora)
https://codepen.io/anon/pen/NVxaoy (source #Daniel)
https://jsfiddle.net/voc9grx6/ (source: Chromium Bugs)
https://jsfiddle.net/qjpat9zk/ (source: Chromium Bugs)
flex items using flex-basis and white-space: nowrap overflow inline-flex container. width works.
It seems that a flex container set to inline-flex doesn't recognize flex-basis on a child when rendering a sibling with white-space: nowrap (although it could just be an item with undefined width). The container doesn't expand to accommodate the items.
But when the width property is used instead of flex-basis, the container respects the sizing of its children and expands accordingly. This is not a problem in IE11 and Edge.
References:
inline flex container width not growing
Inline flex container (display: inline-flex) is expanding the full width of parent container
Example:
https://jsfiddle.net/p18h0jxt/1/ (from first post above)
flex-basis (and flex-grow) not working on table element
References:
Why does flex-box work with a div, but not a table?
Why doesn't flex-grow: 1 work for a table in Safari? (and Edge)
flex-basis fails in Chrome and Firefox when the grandparent container is a shrink-to-fit element. The set-up works fine in Edge.
Absolutely positioned container not expanding width to fit flexbox content
Like in the example presented in the link above, involving position: absolute, the use of float and inline-block, will also render the same flawed output (jsfiddle demo).
Bugs affecting IE 10 and 11:
flex shorthand declarations with unitless flex-basis values are ignored
flex-basis doesn't account for box-sizing: border-box
flex-basis doesn't support calc()
Importance is ignored on flex-basis when using flex shorthand
In addition to Michael_B's excellent summary it's worth repeating this:
flex-basis allows you to specify the initial/starting size of the element, before anything else is computed. It can either be a percentage or an absolute value.
The important part here is initial.
By itself, this does resolve to width/height until the other flex grow/shrink properties come into play.
So. a child with
.child {
flex-basis:25%;
flex-grow:1;
}
will be 25% wide initially but then immediately expand as much as it can until the other elements are factored in. If there are none..it will be 100% wide/tall.
A quick demo:
.flex {
width: 80%;
margin: 1em auto;
height: 25px;
display: flex;
background: rebeccapurple;
}
.child {
flex-basis: auto;
/* default */
background: plum;
}
.value {
flex-basis: 25%;
}
.grow {
flex-grow: 1;
}
<div class="flex">
<div class="child auto">Some Content</div>
</div>
<div class="flex">
<div class="child value">Some Content</div>
</div>
<div class="flex">
<div class="child grow">Some Content</div>
</div>
Experimenting with the flex-grow, flex-shrink and flex-basis
(or the shorthand flex :fg fs fb)...can lead to some interesting results.
Nobody seems to mention that there is one key difference between flex-basis and width (or height, depending on the current writing mode), if we ignore the flexible sizing aspect (flex-grow: 0; flex-shrink: 0;).
It originates from the exception in Flex Layout, that the automatic minimum size for flex items defaults to min-content instead of zero, like usually. In other words, the default min-width: auto computes to min-content instead of 0.
The result is, that flex-basis is (by default) bound below by min-content. If you specify a value smaller than min-content, for example flex-basis: 0, it will compute to min-content. This essentially means that (by default) you can't make the box's content overflow, since the box has at least the size of the content.
This is a key difference to width, which can size the box arbitrarily small (by default), since min-width defaults to 0. If the value of width is smaller than min-content, the contents will overflow the box.
This behavior is mentioned in the spec, but only implicitly in the following comment at the wrong place at the end of 7.1.1. Basic Values of flex.
By default, flex items won’t shrink below their minimum content size (the length of the longest word or fixed-size element). To change this, set the min-width or min-height property. (See § 4.5 Automatic Minimum Size of Flex Items.)
As mentioned in the comment, setting a minimum size lowers the bound, and setting it to zero effectively disables it, making flex-basis behave again as expected.
But there are drawbacks. Firstly, there is no minimum size property for the main axis. You have to use the correct min-width/min-height or min-block-size/min-inline-size property for the current flex-direction. If you changed the flex-direction, you would need to again find the correct minimum size property.
Secondly, flex-basis can't be used anymore to distribute space towards proportionally sized boxes instead of simply adding to their initial size. For more details, see Figure 7 in the spec.
Here is a minimal example. Set min-width: 0 to make flex-basis behave as expected again.
.container {
display: flex;
}
.container div {
background-color: lightgrey;
border: 1px solid black;
margin: 0 10px;
/* disable any flexible sizing */
flex-grow: 0;
flex-shrink: 0;
/* TOGGLE ME */
/* min-width: 0; */
}
.mincontent {
width: min-content;
}
.smallerflexbasis {
flex-basis: 3ex;
}
.smallerwidth {
width: 3ex;
}
<div class="container">
<div class="mincontent">Lorem ipsum</div>
<div class="smallerflexbasis">Lorem ipsum</div>
<div class="smallerwidth">Lorem ipsum</div>
</div>
Possibly the most important point to add:
What if the browser doesn't support flex? In such a case, width/height take over and their values apply.
It is a very good idea - almost essential - to define width/height on elements, even if you then have a completely different value for flex-basis. Remember to test by disabling display:flex and seeing what you get.
It makes a difference if you're wrapping.
Say, you set a child to width:0 and expect it to wrap, well that's not going to happen. But with flex-basis:0 it will wrap. (provided overflow isn't hidden)
if you set a div's min-width:600px, if the window size goes under 600px, you will see a horizontal scrollbar which is not a good ux design.
If you set its flex-basis:600px, if the window size goes under 600px, that box will shrink and you will not see a horizontal bar.
Note that flex-basis applies only to the flex items.

Does justify-content of parent matter when all children have a flex property?

.parent {
background-color: yellow;
display: flex;
justify-content: space-evenly;
}
.parent > div {
background-color: lightblue;
flex: 1;
margin: 5px;
}
<div class="parent">
<div>Child #1</div>
<div>Child #2</div>
<div>Child #3</div>
</div>
In the above simple example, there is a parent div with 3 children divs. All the children have a flex: 1 property and so they are all distributed equally inside their parent. At this point, does the property justify-content: space-evenly of the parent actually count? No matter what value I insert, the result is always the same. Could it be deleted at all?
The justify-content property aligns flex items along the main axis of the current line of the flex container. This is done after any flexible lengths and any auto margins have been resolved. Typically it helps distribute extra free space leftover when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.ref
If you have no extra space then you can safely omit the property and nothing will change.
It can make a difference in two situations:
Your layout is dynamic and your item may have a reduced size and a free space is created
You are using justify-content:inherit inside a flex item to get the parent value (not a very common situation by the way)

How to create a flex item that shrinks before it wraps?

As can be seen in this JS-Fiddle, I basically try to use this CSS to create two divs that should fullfill these requirements:
If twice the space of the wider item is available, both should use 50% width (that works)
If not enough space for both items is available, they should wrap (that works)
If enough space is available for both items, but less than twice the width of the wider one, the narrower item should shrink (that does NOT work, it wraps)
I don't understand this behavior, because I have set flex-shrink for the flex items, so they should be able to shrink - but they don't: If the narrower item would be less than 50% wide, it wraps.
.m {
display: flex;
flex-wrap: wrap;
}
.l_1 {
background-color: red;
flex: 1 1 50%;
}
.r_1 {
background-color: yellow;
flex: 1 1 50%;
}
<div class=m>
<div class=l_1>
left_______________________________________________X
</div>
<div class=r_1>
right
</div>
</div>
(Tested on Firefox and Chrome)
The problem is not flex-shrink. The problem is flex-basis. You have it set to 50%.
This means that flex-grow will add free space to the flex-basis, and each item will wrap quickly, before it has an opportunity to shrink.
Switch from flex-basis: 50% to flex-basis: 0.
More specifically, instead of flex: 1 1 50% use flex: 1, which breaks down to this:
flex-grow: 1
flex-shrink: 1
flex-basis: 0
Now flex-grow distributes free space equally – not proportionally – to both items, and they can shrink before they wrap.
(Here's a more in-depth explanation: Make flex-grow expand items based on their original size)
.m {
display: flex;
flex-wrap: wrap;
}
.l_1 {
background-color: red;
flex: 1;
}
.r_1 {
background-color: yellow;
flex: 1;
}
<div class=m>
<div class=l_1>left_______________________________________________X</div>
<div class=r_1>right</div>
</div>
revised fiddle
When wrapping is enabled, it takes the place of shrinking, so where there is a condition that would trigger shrinking, it wraps instead - until there is only one item in the row, and if that's still bigger than the container, then it will shrink.
So, you need to set flex-basis for all boxes to the minimum size that block should be before wrapping. Note: a box will never shrink further than its minimum content width, meaning you can set flex-basis to 0 and it will go by the minimum content width of each box.
Then, if you want the boxes to expand to fill the available space, then use the flex-grow property (first value in flex) to control the relative amount by which each one should grow.
Flex-shrink will not apply with flex-wrap: wrap also applied. It will line wrap instead.
The only exception to this is when you only have one flex item in the row. Then it should allow for flex-shrink to apply.
Perhaps the appropriate fix is to only apply the flex-wrap within a media query, so it only happens in smaller viewports?

`flex-basis: auto` sizes parent as if child were `flex-basis: auto`, when child is `flex-basis:10px`

For example, you shouldn't be able to see the red of the parent here, but you do, because parent: 0 0 auto is sizing the parent to the auto width of its child content. You can see clearly though, the real width of its content is 10px, so shouldn't its auto sizing make the parent 10px as well?
body{
display:flex;
}
#parent {
flex: 0 0 auto;
background-color: red;
}
#child {
flex: 0 0 10px;
background-color: grey;
}
div{ display:flex; min-width:0; min-height:0; overflow:hidden; } /*There's some weirdness described here, http://stackoverflow.com/questions/36247140/why-doesnt-flex-item-shrink-past-content-size where flex elements default to min-width:auto, which could have caused problems here, but this doesn't change anything, so apparently this is not the issue*/
<div id="parent">
<div id="child">childcontents</div>
</div>
This occurs in firefox and chrome, so presumably this is going to turn out to be correct somehow. I'd like to know how, so that I can stop it.
According to my understanding of the spec, this is a bug.
What Michael_B said is correct, first #parent is sized, and once its width is know, #child can flex. Since flexing usually involves growing or shrinking, the size of the flex container must be known before flexing the flex item; and the final size of the flex item may not be the flex basis, so the flex container shouldn't be sized using that value.
The solution is easy: use width instead of flex-basis. width does not flex, so it doesn't depend on the width of the container. Thus the container can use the width of their contents when sized.
body {
display: flex;
}
#parent {
flex: none;
background-color: red;
}
#child {
width: 10px;
flex: none;
background-color: grey;
}
div {
display: flex;
min-width: 0;
min-height: 0;
overflow: hidden;
}
<div id="parent">
<div id="child">childcontents</div>
</div>
That said, in your case using flex-basis should work. That's because your flex item has both a zero flex grow factor and a zero flex shrink factor. It cannot grow nor shrink, it becomes directly frozen. Therefore it's possible to use consider the flex-basis when sizing the parent, and the spec says so:
9.9.3. Flex Item Intrinsic Size Contributions
The main-size min-content/max-content contribution of a
flex item is its outer min-content/max-content size,
clamped by its flex base size as a maximum (if it is not
growable) and/or as a minimum (if it is not shrinkable), and then
further clamped by its min/max main size properties.
The contribution of the ungrowable unshrinkable flex item is clamped by its flex base size both as a maximum and as a minimum. That is, the contribution is exactly the flex base size, which is defined as the flex basis when the flex basis is definite.
It looks like the flex layout algorithm calculates the width of the flex container before arriving at the width of the flex items.
Hence, it determines the auto size of #parent based on the full width of #child.
Then it sizes #child to flex-basis: 10px.
At his point, however, the auto width of the parent has already been determined and is inflexible.
Testing in Chrome, re-arranging the rules makes no difference, so it doesn't appear to be a cascading issue.
This is my view of the behavior without an official reference to back it up. You may find the precise answer here: W3C Spec: Flex Layout Algorithm

flex items ignoring width

I've reordered some elements in my html using flexbox in the responsive design of a website which works fine but the elements then won't resize properly.
At a breakpoint I have applied a class of flex to the home-promos div and reordered the elements. This works correctly.
The problem then arises when I try to resize the div's to percentage widths. They will only resize up to a certain point, such as 50% and then won't get any bigger.
Is anyone who is better with flexbox than myself able to tell me what the issue is?
.home-promos {
display: flex;
}
.home-promo-center {
order: 1;
}
.home-promo-left {
order: 2;
}
.home-promo-right {
order: 3;
}
<div class="home-promos">
<div class="home-promo-left">
<div class="promo-left-content">
*content*
</div>
</div>
<div class="home-promo-center">
<div class="promo-center-content">
*content*
</div>
</div>
<div class="home-promo-right">
<div class="promo-right-content">
*content*
</div>
</div>
</div>
When you create a flex container (display: flex or display: inline-flex), it comes with several default settings. Among them are:
flex-direction: row - flex items will align horizontally
justify-content: flex-start - flex items will stack at the start of the line
flex-wrap: nowrap - flex items are forced to stay in a single line
flex-shrink: 1 - flex items are allowed to shrink
Note the last two settings.
Your three divs are forced to remain in a single line. Hence, their combined width is limited to the width of the container.
Also, they are allowed to shrink, which prevents them from overflowing the container. This also limits their width.
To apply whatever width you want to each flex item you can override these initial settings with:
flex-wrap: wrap - now there's more space because flex items can break to new lines
flex-shrink: 0 - now there's more space because flex items will not shrink and can overflow their container if necessary