This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 5 years ago.
I'm using max-width: 100% to contain textarea. But it doesn't seem to work inside a flex item. Is there a property I can specify on textarea to ensure that the max will be respected?
textarea {
display: block;
margin-bottom: 1em;
max-width: 100%;
box-sizing: border-box;
overflow: auto;
}
See codepen. Flex classes from from flexboxes.
You specify that the text area should be 4,000 columns wide in the codepen, which is what's causing the issue.
Since the parent is a flex container, it will grow to accommodate the children elements.
Easiest fixes are to:
set a max width in px, vw, or another appropriate unit for your textarea.
use width instead of max-width, and specify a max-width on the parent container, instead
override the default min-width: auto of the parent element, setting it to any other value, i.e. 0, or 250
reduce the columns to something more reasonable, like 250 - or omit them and use width instead
Related
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.
This question already has answers here:
When flexbox items wrap in column mode, container does not grow its width
(9 answers)
Closed 4 years ago.
I'm using a column wrap layout, and the page is going to load new content and append them as new children elements to the parent box element, so I hope the box width auto fit to the children elements.
Codes here: https://codepen.io/mashirozx/pen/exgRqV
What it looks now:
My hope (Notice the overflow-x bar):
Problem solved:
Adding overflow: auto to the flex box.
Try to change:
flex-container-content > div {
background-color: #EB213C;
width: auto;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;`
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.
This question already has an answer here:
CSS Height working but min-height doesn't work
(1 answer)
Closed 5 years ago.
I would like a flex-element with a min-height of 100%.
There is a child element always at the bottom.
If the content in the first flex child is longer than the screen the site should be normal scrollable.
Here is a pen I made.
Why is it working with height but not min-height?
height: 100%; // works
min-height: 100%; // doesn't works
As answered here: CSS Height working but min-height doesn't work
Height can be inherited from positioned parents but not when these have a min-height property.
Which means that .flexbox doesn't inherit height from wrapper because .wrapper has min-height set. When you set height to .wrapper, .flebox and with it .grower can figure out what 100% actually means, and then they grow to fit the screen.
Note: I would say position part of the linked answer is not correct, but the quoted part is.
min-height : Height of the element or css class in question should not be less than specified value, even if height is less that specified value of min-height. It does not specify what the height is going to be, it specifies that it cannot be smaller than its value.
So, if height is 200px, max-height is 230px, min-height is 250px, height of the page component will be 250px. Between height and max-height, max-height can override height.
So, if I change your wrapper class to as shown below, it's height will remain 80%.
.wrapper {
background-color: pink;
min-height: 80%;
height:50px;
}
This question already has answers here:
How to set the margin or padding as percentage of height of parent container?
(9 answers)
Closed 8 years ago.
I am working on a project in which the client wants the navigation <div> to align according to the screen height, similar to how margin-left as a percentage works when screen width is decreased.
So, I gave margin-top: 20% and navigation <div> displays that margin, but when I decrease the height of the window it does not adjust according the screen height although it works when I decrease the screen width.
My question is not how can I achieve that, but why does the percentage work horizontally and not vertically?
Here is an example: http://jsfiddle.net/sandeep/5VReY/
The percentage works on the width of the container block, according to the css specifications
The percentage is calculated with respect to the width of the
generated box's containing block. Note that this is true for
'margin-top' and 'margin-bottom' as well.
See w3.org for more information.
You can play width top/bottom properties having margin prop in "auto";
If you have a block like this:
<div class="centered"></div>
It may be centered verticaly like this:
.centered {
width: 100px; height: 100px; /* must be present. No matter the value */
position: absolute; /* to props top/bottom take effect */
margin: auto; /* here's the magic. */
bottom: 0px; top: 0px; /* This is how you center a block verticaly */
}
The same can be achieved for horizontal alignment width left/right properties. You can also have an offset in order to ubicate other point than the center of the block.
Here I leave you some examples of what and how can be done combining these properties.
http://jsfiddle.net/SQDJ6/