How to prevent flex-items from overflowing flex parent with no wrap?
Set display: inline-flex
on the .parent
class to change it to an inline element. This will also force the .parent
to expand to contain its children. Then by setting min-width: 100%
on the .parent
class, it will force it to expand to 100% of the containing element.
.parent {
display: inline-flex;
flex-direction: row;
background-color: red;
min-width: 100%;
}
.child {
min-width: 100px;
flex-basis: 0px;
flex-grow: 1;
margin: 5px;
height: 100px;
background-color: blue;
}
Prevent flex items from overflowing a container
Your flex items have
flex: 0 0 200px; /* <aside> */
flex: 1 0 auto; /* <article> */
That means:
The
<aside>
will start at200px
wide.Then it won't grow nor shrink.
The
<article>
will start at the width given by the content.Then, if there is available space, it will grow to cover it.
Otherwise it won't shrink.
To prevent horizontal overflow, you can:
- Use
flex-basis: 0
and then let them grow with a positiveflex-grow
. - Use a positive
flex-shrink
to let them shrink if there isn't enough space.
To prevent vertical overflow, you can
- Use
min-height
instead ofheight
to allow the flex items grow more if necessary - Use
overflow
different than visible on the flex items - Use
overflow
different than visible on the flex container
For example,
main, aside, article {
margin: 10px;
border: solid 1px #000;
border-bottom: 0;
min-height: 50px; /* min-height instead of height */
}
main {
display: flex;
}
aside {
flex: 0 1 200px; /* Positive flex-shrink */
}
article {
flex: 1 1 auto; /* Positive flex-shrink */
}
<main>
<aside>x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x </aside>
<article>don't let flex item overflow container.... y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y </article>
</main>
Prevent flex item from overflowing flex container
Much of the CSS in your code can be removed.
It's not necessary or conflicts with useful flex settings.
Just use flex properties to achieve the layout.
.outer { height: 100vh; display: flex; flex-flow: column;}
.inner { display: flex; flex: 1; min-height: 0; /* https://stackoverflow.com/q/36247140/3597276 */}
.column { overflow-y: auto; margin: 0 10px; border: 1px solid #000;}
.column-left { flex: 0 0 25%;}
.column-right { flex: 1; display: flex; flex-flow: column;}
body, div { margin: 0; padding: 0;}
<div class="outer"> <h1>Heading of different height</h1> <div class="inner"> <div class="column column-left"> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> </div> <div class="column column-right"> <div class="content"> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> <h2>Row</h2> </div> <div class="controls"> Variable height <br> 1 </div> </div> </div></div>
Prevent flex PARENT from growing without overflow: hidden
You can remove "overflow-y: hidden" and add "height: 100%" to .splitView and -section.
.splitView{
display: flex
flex: 1 1 auto
background: rgba(#008080, 0.2)
height: 100%
&-section{
display: flex
flex-direction: column
flex: 1 1 50%
height: 100%
}
}
Since the height of the parent div is set, the children divs will inherit that height.
https://codepen.io/salixdubois/pen/JQLaRE
Prevent a child element from overflowing its parent in flexbox
An initial setting on flex items is min-width: auto
. This means that a flex item, by default, cannot be smaller than the size of its content.
Therefore, text-overflow: ellipsis
cannot work because a flex item will simply expand, rather than permit an overflow. (Scroll bars will not render either, for the same reason.)
To override this behavior, use min-width: 0
or overflow: hidden
. More details.
#container { display: flex; flex-wrap: wrap; border: thin solid gray;}
.card-wrapper { width: 33.33%; display: flex; background: #e0e0ff;}
.card { flex-grow: 1; margin: 7px; display: flex; flex-direction: column; border: thin solid gray; background: #e0ffff; overflow: hidden; /* NEW */}
.card div { border: thin solid gray;}
.card div:nth-child(1) { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; /* NEW */}
.card div:nth-child(2) { flex-grow: 2;}
<div id="container"> <div class="card-wrapper"> <div class="card"> <div>Title</div> <div>Multiline<br/>Body</div> <div>Footer</div> </div> </div> <div class="card-wrapper"> <div class="card"> <div>Really long rambling title that pushes beyond the bounds of the container, unless your screen is really, really wide</div> <div>Body</div> <div>Footer</div> </div> </div> <div class="card-wrapper"> <div class="card"> <div>Title</div> <div>Body</div> <div>Footer</div> </div> </div> <div class="card-wrapper"> <div class="card"> <div>Title</div> <div>Body</div> <div>Footer</div> </div> </div> <div class="card-wrapper"> <div class="card"> <div>Title</div> <div>Body</div> <div>Footer</div> </div> </div></div>
Prevent flex item from exceeding parent height and make scroll bar work
Short Answer
Instead of flex: 1
, use flex: 1 1 1px
.
Make these two adjustments in your code:
#messagelist {
/* flex:1; */
flex: 1 1 1px; /* new */
}
#messagecontents {
/* flex:1; */
flex: 1 1 1px; /* new */
}
revised codepen
Explanation
In most cases, as you have noted, adding min-height: 0
to flex items in a column-direction container is enough to correct the problem.
In this case, however, there's an additional obstacle: flex-basis
.
You're applying the following rule to flex items #messagelist
and #messagecontents
: flex: 1
.
This is a shorthand rule that breaks down to:
flex-grow: 1
flex-shrink: 1
flex-basis: 0
(source: https://www.w3.org/TR/css-flexbox-1/#flex-common)
2019 UPDATE: Since the posting of this answer in 2018, it appears that Chrome's behavior has changed and is now uniform with Firefox and Edge. Please keep that in mind as you read the rest of this answer.
In Chrome, flex-basis: 0
is enough to trigger an overflow, which generates the scrollbars. (2019 update: This may no longer be the case.)
In Firefox and Edge, however, a zero flex-basis
is insufficient. This is probably the more correct behavior in terms of standards compliance as MDN states:
In order for
overflow
to have an effect, the block-level container must have either a set height (height
ormax-height
) orwhite-space
set tonowrap
.
Well, flex-basis: 0
meets none of those conditions, so an overflow condition should not occur. Chrome has probably engaged in an intervention (as they often do).
An intervention is when a user agent decides to deviate slightly from a standardized behavior in order to provide a greatly enhanced user experience.
To meet the "standardized behavior", which would enable an overflow to occur in Firefox and Edge, give flex-basis
a fixed height (even if it's just 1px).
Flex child is overflowing the parent container even after setting min-width:0?
There are a couple of issues with what you posted, but fear not we can sort you out.
What is flex, what is not
First let's look at your markup:
.parent
is an element with display: flex
. From your naming we might incorrectly assume that its children are:
.child1
,.child2
, and.child3
.
…but this is not the case.
The children of .parent
are actually:
.child1
, and- a classless
div
.
The classless div
has no styles set for it, so .child2
and .child3
are not positioned in a flexbox context. For this reason, your min-width: 0
on .child3
doesn't solve your problem, as that solution only applies for flex children.
Applying min-width
in the correct context
To start, let's give that child div
a class: .foo
.
.foo
itself has a block display, but currently it is allowing content (in .child3
) to overflow. It is this element on which we want to prevent overflow:
.foo {
min-width: 0;
}
That should be all you need. It seems you're already familiar with why we use min-width
to help with this, but just in case you can read about it in CSS Tricks: Flexbox and Truncated Text.
Solution
Below is all of it put together.
.parent {
display: flex;
border: 2px solid red;
}
.foo {
min-width: 0;
outline: 1px solid rebeccapurple;
}
.child1 {
background: cyan;
height: 40px;
width: 40px;
flex-shrink: 0;
}
.child2 {
background: pink;
height: 20px;
width: 20px;
}
.child3 {
background: yellow;
height: 20px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%;
}
<div class='parent'>
<div class='child1'>1</div>
<div class="foo">
<div class='child2'>2</div>
<div class='child3'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed luctus sagittis odio, ac pulvinar tortor sagittis et. In hac habitasse platea dictumst. Phasellus ut velit dolor. Vestibulum pulvinar orci libero, in aliquet arcu auctor non. Morbi volutpat elit id lacus cursus, at imperdiet tellus eleifend. Morbi euismod vehicula urna, sed pretium felis ullamcorper vitae. Nunc at ligula a odio eleifend convallis eget sed orci. Praesent fermentum, sem in congue tempus, ex diam suscipit neque, in ullamcorper orci erat eu orci.
</div>
</div>
</div>
Related Topics
Limit Number of Digits After Decimals in Text Input
Material Design - Stepper How to Remove/Disable Steps
How to Avoid Element from Expanding When Its Content Text Font-Size Gets Bigger
Inserting an Image from Local Directory in Thymeleaf Spring Framework (With Maven)
Text Cutting Inside Large Select Option Dropdown
How to Divide a Page in Three Vertical Sections
Position: Sticky Buttons Not Working in Ie 11
How to Create an HTML Button That Acts Like a Link
Post Unchecked HTML Checkboxes
Left Column and Stacked Right Column Using Flexbox Css
Show Placeholder Text for Input Type Date
Force Element to Display Outside of Overflow:Hidden
How to Manually Trigger Click Event in Reactjs
How to Remove the Default Arrow Icon from a Dropdown List (Select Element)
Responsive Fullscreen Youtube Video With No Black Bars
Angular 2: Get Position of HTML Element