Reputation: 43794
How do I make my flex item (article
in this example), which has flex-grow: 1;
not to overflow its flex parent/container (main
)?
In this example article
is just text, though it might contains other elements (table
s, etc).
main, aside, article {
margin: 10px;
border: solid 1px #000;
border-bottom: 0;
height: 50px;
}
main {
display: flex;
}
aside {
flex: 0 0 200px;
}
article {
flex: 1 0 auto;
}
<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>
Upvotes: 359
Views: 360051
Reputation: 4066
For me the solution was to use max-width: 0rem;
or in tailwindCSS max-w-0
, the opposite of what the top-voted answer suggests.
<wrapper that is flex>
<div className="flex max-w-0">
overflowing content here
</div>
</wrapper>
Upvotes: 0
Reputation: 1
I am late to the party here but here it how it works. the "parent" which has flex property decides how content will flow by default its sub divs will flow horizontally. you have to specify to flow vertically and then go from there. hope it helps.
main, aside, article {
margin: 10px;
border: solid 1px #000;
border-bottom: 0;
height: 50px;
}
main {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
aside {
flex: 0 0 200px;
}
article {
flex: 1 0 auto;
}
<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>
Upvotes: 0
Reputation: 15605
For me simply adding min-width: 0;
to the overflowing div prevented the overflow:
article {
min-width: 0;
}
Explanation:
min-width
in a flex context: While the default min-width
value is 0
(zero), for flex items it is auto
. This can make block elements take up much more space than desired, resulting in overflow.
min-width
is defined to win against competing width
and max-width
, meaning as soon as the element's width would shrink below its implicit auto width, min-width:auto
will kick in and keep the element from shrinking.
The fix is obvious now: min-width: 0
It tells the browser that this element has no right to take up any minimum width, and now it will be rendered properly, taking up only as much width as is available.
A note about flex-shrink
: While flex-shrink sounds like it could help here, it does not. The described issue is about element-sizing based on the element's content, while flex-shrink defines shrinkage relative to other flex elements in the same context. Source
Upvotes: 676
Reputation: 287950
Your flex items have
flex: 0 0 200px; /* <aside> */
flex: 1 0 auto; /* <article> */
That means:
The <aside>
will start at 200px
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:
flex-basis: 0
and then let them grow with a positive flex-grow
.flex-shrink
to let them shrink if there isn't enough space.To prevent vertical overflow, you can
min-height
instead of height
to allow the flex items grow more if necessaryoverflow
different than visible on the flex itemsoverflow
different than visible on the flex containerFor 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>
Upvotes: 131
Reputation: 913
max-width
works for me.
aside {
flex: 0 1 200px;
max-width: 200px;
}
Variables of CSS pre-processors allows to avoid hard-coding.
aside {
$WIDTH: 200px;
flex: 0 1 $WIDTH;
max-width: $WIDTH;
}
overflow: hidden
also works, but I lately I try do not use it because it hides the elements as popups and dropdowns.
Upvotes: 1
Reputation: 9279
It's not suitable for every situation, because not all items can have a non-proportional maximum, but slapping a good ol' max-width
on the offending element/container can put it back in line.
Upvotes: 1
Reputation: 255
I know this is really late, but for me, I found that applying flex-basis: 0;
to the element prevented it from overflowing.
Upvotes: 12
Reputation: 11568
One easy solution is to use overflow
values other than visible
to make the text flex basis width reset as expected.
Here with value auto
the text wraps as expected and the article content does not overflow main container.
Also, the article flex
value must either have a auto
basis AND be able to shrink, OR, only grow AND explicit 0
basis
main, aside, article {
margin: 10px;
border: solid 1px #000;
border-bottom: 0;
height: 50px;
overflow: auto; /* 1. overflow not `visible` */
}
main {
display: flex;
}
aside {
flex: 0 0 200px;
}
article {
flex: 1 1 auto; /* 2. Allow auto width content to shrink */
/* flex: 1 0 0; /* Or, explicit 0 width basis that grows */
}
<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>
Upvotes: 15
Reputation: 122027
Instead of flex: 1 0 auto
just use flex: 1
main, aside, article {
margin: 10px;
border: solid 1px #000;
border-bottom: 0;
height: 50px;
}
main {
display: flex;
}
aside {
flex: 0 0 200px;
}
article {
flex: 1;
}
<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>
Upvotes: 10