I have a flexbox with flex-direction: row
. The child items have a min-width
set. When the window is shrunk past the point where the min-width
is reached, the items begin to overflow the flex container.
.parent {
display: flex;
flex-direction: row;
background-color: red;
}
.child {
min-width: 100px;
flex-basis: 0px;
flex-grow: 1;
margin: 5px;
height: 100px;
background-color: blue;
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
https://jsfiddle.net/4t8029q8/
Is there any way to force the container to stretch to contain the items without setting an explicit min-width
on the parent? Doing so provides the behavior I am trying to achieve, but is too rigid to accomodate a variable number of items.
.parent {
display: flex;
flex-direction: row;
background-color: red;
min-width: 330px
}
.child {
min-width: 100px;
flex-basis: 0px;
flex-grow: 1;
margin: 5px;
height: 100px;
background-color: blue;
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
https://jsfiddle.net/4t8029q8/1/
This is what I want, for the flex-items to never overflow the flex container even if it causes the page to need a horizontal scroll.
NOTE: I have other more complicated logic requiring flex-basis: 0px
and flex-grow: 1
, so those lines cannot be removed.
The best solution I've come up with is to set the services images to overflow: hidden and the staff images to nowrap, this prevents images from either gallery from overlapping with any other elements.
The initial value of the flex-wrap property is nowrap . This means that if you have a set of flex items that are too wide for their container, they will overflow it.
Use the flex-grow property to make a flex item consume free space on the main axis. This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.
When using CSS Flexbox, wrapping your flex items is recommended. But if we don’t and view the page on a smaller screen, the flex items may not fit the screen’s viewport, therefore causing the horizontal overflow issue: To solve this, you should always wrap your flex items.
Under certain circumstances overflow needs a little extra love. It turns out that there was a feature in the flexbox specification that added an implied minimum size for flex items. This feature was removed and then re-added back into the spec at some point.
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.)
Once your screen reaches said size, force your parent to have wrapping items by adding flex-wrap: wrap; to your parent. When you're outside the said width, be sure to set your parent's CSS back to not allow wrapping, flex-wrap: nowrap; . This will allow your child items to wrap, and your parent will create a horizontal to the page.
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;
}
You're forcing your child elements to have a specific width, much like @Michael_B mentioned, this essentially creates a static environment where they will remain the set width regardless of the parent.
Essentially at this point, since you know the min-width of your children elements, I would create a media query, at the specific width requirement, which in this case is 100px. Once your screen reaches said size, force your parent to have wrapping items by adding flex-wrap: wrap;
to your parent. When you're outside the said width, be sure to set your parent's CSS back to not allow wrapping, flex-wrap: nowrap;
. This will allow your child items to wrap, and your parent will create a horizontal to the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With