I am playing around with Flexbox in an attempt to learn it on a fundamental level. I am confused by inconsistent results from independent usage of flex-grow
and width
on flex items.
Suppose I have the following HTML:
<div class="flex-container">
<div id="flex-item-1">1</div>
<div id="flex-item-2">2</div>
</div>
and the following CSS:
.flex-container {
display: flex;
}
/*
Within #flex-item-1 and #flex-item-2, toggle between
flex-grow and width to view the different resultant widths
*/
#flex-item-1 {
flex-grow: 3;
background-color: tomato;
width: 75%;
}
#flex-item-2 {
flex-grow: 1;
background-color: pink;
width: 25%;
}
When the width
CSS properties for #flex-item-1
and #flex-item-2
are active but the flex-grow
properties aren't, you can see that the grid lines (made by the HTML and CSS code below) line up with #flex-item-1
and #flex-item-2
perfectly.
<!-- 12-column grid lines -->
<div class="flex-container">
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
<div class="col">5</div>
<div class="col">6</div>
<div class="col">7</div>
<div class="col">8</div>
<div class="col">9</div>
<div class="col">10</div>
<div class="col">11</div>
<div class="col">12</div>
</div>
.col {
box-sizing: border-box;
border-left: 1px solid green;
width: 8.333333%;
height: 100px;
}
However, when the flex-grow
properties are active and the width
properties aren't, the flex items don't line up with the grid exactly:
Why is this?
Feel free to play around with this example on this Codepen.