Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox not full width

Tags:

html

css

flexbox

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?

like image 681
fightstarr20 Avatar asked May 21 '18 20:05

fightstarr20


People also ask

How do I make my Flex container not full width?

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).

How do you make flexbox children 100% width of their parents using CSS?

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).

How to make flex items take the content width instead of width?

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.

How many types of width Flexbox is design using CSS?

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.

What is Flexbox layout (flexible box)?

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").

How do I change the width of a bootstrap Flexbox?

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.


2 Answers

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>
like image 54
Johannes Avatar answered Oct 26 '22 13:10

Johannes


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>
like image 37
Michael Benjamin Avatar answered Oct 26 '22 14:10

Michael Benjamin