I have a basic flexbox like this..
.masonry_container {
display: flex;
}
.masonry_left_col {
border: 1px solid grey;
}
.masonry_right_col {
border: 1px solid grey;
}
<div class="masonry_container">
<div class="masonry_left_col">
Content
</div>
<div class="masonry_right_col">
Content
</div>
</div>
Why does it not extend to the full width?
I know this is probably really simple but i can't work it out, where am I going wrong?
Instead of display: flex on the container, use display: inline-flex . This switches the container from block-level (which takes the full width of its parent) to inline-level (which takes the width of its content).
Getting the child of a flex-item to fill height 100%Set position: absolute; on the child. You can then set width/height as required (100% in my sample).
You can make flex items take the content width instead of the width of the parent container with CSS properties. The problem is that a flex container’s initial setting is align-items: stretch; meaning that items expand to cover the container’s full length along the cross axis.
Note: Elements width depend on the other elements and screen of your window in this case. Example 1: Here you will see the two types of width flexbox is design by using CSS.
The Flexbox Layout (Flexible Box) module ( a W3C Candidate Recommendation as of October 2017) aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word "flex").
Approach 2: Using flex-grow class: We can also alter the width of the flexbox by using Bootstrap’s flex-grow-* class. .flex-grow-1 stretch the element to its maximum width by giving the other elements (if any) only the necessary amount of space.
The container actually is 100% wide, i.e. spans the full width of the window. But with the default flex settings, its children will simply align left and will be only as wide as their contents.
However, if you apply flex-grow: 1;
to the child elements to allow them to get wider, they will stretch and fill the full width of the container.
.masonry_container {
display: flex;
}
.masonry_left_col {
border: 1px solid grey;
flex-grow: 1;
}
.masonry_right_col {
border: 1px solid grey;
flex-grow: 1;
}
<div class="masonry_container">
<div class="masonry_left_col">
Content
</div>
<div class="masonry_right_col">
Content
</div>
</div>
Or, if you just want the flex items to go full left and right inside the container without stretching, add justify-content: space-between
to the container
.masonry_container {
display: flex;
justify-content: space-between;
}
.masonry_left_col {
border: 1px solid grey;
}
.masonry_right_col {
border: 1px solid grey;
}
<div class="masonry_container">
<div class="masonry_left_col">
Content
</div>
<div class="masonry_right_col">
Content
</div>
</div>
The flex container does extend the full width – it's a block level element.
.masonry_container {
display: flex;
border: 1px solid red;
}
.masonry_container > div {
border: 1px solid grey;
background-color: lightgreen;
}
<div class="masonry_container">
<div class="masonry_left_col">Content</div>
<div class="masonry_right_col">Content</div>
</div>
But the flex items have two default settings that prevent automatic expansion:
flex-basis: auto
flex-grow: 0
This means that items take the length of their content and do not consume free space.
For the items to expand you need to override the flex-grow
default value.
.masonry_container {
display: flex;
border: 1px solid red;
}
.masonry_container > div {
flex-grow: 1;
border: 1px solid grey;
background-color: lightgreen;
}
<div class="masonry_container">
<div class="masonry_left_col">Content</div>
<div class="masonry_right_col">Content</div>
</div>
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