Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make flex div take only the needed space?

Tags:

How do I make a flex div to take only the space needed? It is taking all available space.

I want to have lined up divs, wrapping when it needs. Problem is that the flex container takes more space than it actually needs.

Here’s an example http://cssdeck.com/labs/wvhe64ot

#outer {      border: 5px solid green;      width: 400px;  }  #container {      display: flex;      border: 5px dashed black;      flex-wrap: wrap;  }  #container div {      padding: 10px;      border: 2px solid black;  }
Dashed border should be close to the small divs  <div id="outer">    <div id="container">      <div>Lipsum</div>      <div>Lipsum</div>      <div>Lorem ipsum dolor sit amet</div>      <div>Lorem ipsum dolor sit amet</div>     </div>  <div>
like image 229
rodorgas Avatar asked Feb 26 '15 01:02

rodorgas


People also ask

How do you make a flex item occupy remaining space?

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.

How do you control the flex item width?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.


2 Answers

You might be looking for the display: inline-flex property. It treats its children just like the display: flex property, except it itself does not automatically grow.

like image 115
Tormod Haugene Avatar answered Sep 19 '22 12:09

Tormod Haugene


set flex-shrink: 0 on div that you what to take at least required space

like image 34
Bhavesh Avatar answered Sep 20 '22 12:09

Bhavesh