I have a bit of HTML:
<div class="flex">
<div class="red">
<div class="example">
<div class="wide">
</div>
</div>
</div>
<div class="orange"></div>
<div class="yellow"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="indigo"></div>
</div>
<div class="flex">
<div class="red"></div>
<div class="orange"></div>
<div class="yellow"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="indigo"></div>
</div>
And my CSS:
.flex {
display: -webkit-box;
display: flex;
height: 100px;
width: 100%;
}
.flex > div {
flex-grow: 1;
flex-shrink: 1;
flex-basis: auto;
-webkit-flex-grow: 1;
-webkit-flex-shrink: 1;
margin: 15px;
}
.example {
overflow: hidden;
width: 100%;
border: 1px solid black;
}
.example .wide {
width: 400px;
}
Now, the problem here is, if I have a div
inside an element that is a flexbox and that div
has overflow: hidden;
I would expect the parent flexbox to just take up the default amount of space (i.e. the same as the other elements, but it doesn't.
It is like it ignores the overflow
and just expands the div
anyway.
I have made a CodePen so you can see the issue.
I would like all the coloured div
s to be the same width.
Can anyone help?
Your problem is caused by setting the
flex-basis
of.flex > div
toauto
. This causes the div to expand to accommodate its content as it has no set dimension along the flex axis. Give it a set value like20px
and the space will be evenly divided becauseflex-grow
is set to1
.This article should help you get started with the flex layout.
Here is a modified version of your code
only set position of parent element
this work for me