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>
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.
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.
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.
set flex-shrink: 0 on div that you what to take at least required space
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