Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a minimum amount of space between flexbox items?

Tags:

html

css

flexbox

Given this CSS:

div.container {   display: flex;   flex-flow: row wrap;   justify-content: space-between;   border: 1px solid blue; } div.container div {   width: 200px;   border: 1px solid gray;   display: inline-block;   text-align: center; } 

This layout has the first item in each row aligned to the left, and the last item aligned to the right, as required.

As the browser window is made narrower, the distributed div elements will move closer together until they touch, at which point they are re-arranged over an additional row. Again, the first div on each row is aligned left, and the last aligned right with space between.

Is there any way of setting a minimum spacing so that the inner div elements always have a gap between them.

padding and margin will probably not work, as the alignment

<-- 1st left in row and last right in row --> will not hold.

like image 421
Jim Avatar asked Oct 07 '15 04:10

Jim


People also ask

Can I set width of flex 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.

Can I use gap with Flex?

IMPORTANT the gap property only modifies the distance between cell items, and it does not affect the distance of those cell items to the edges of the grid container. It turns out that the gap property can also be used with flex layout and it solves so many problems!


2 Answers

Bit late the the party but I ran into the same issue. The way I solved it probably wont work for everyone but here it is for those who can use it.

The basic idea is that you have a min gap of x. You set the left and right margins of each item to x/2 so that the distance between the items will be x (margin + margin). Then you wrap all of the items in a container with a left and right margin of -x/2. This will hide the margin on the items at the edges of each row.

Here is a working example:

.box {    border: 1px solid black;    overflow-x: hidden;  }    .container {    display: flex;    flex-wrap: wrap;    justify-content: space-between;    margin: 0 -1em;  }    .item {    display: flex;    border: 1px solid grey;    padding: 1em;    width: 20%;    margin: 0 1em;  }
<div class="box">    <div class="container">      <div class="item">1</div>      <div class="item">2</div>      <div class="item">3</div>      <div class="item">4</div>      <div class="item">5</div>    </div>  </div>

The overflow-x: hidden; on .box is to prevent the horizontal scrollbar that shows up in some browsers because of the margin overflowing.

If you want the gap to always be consistent and for rows with only one item to have that item span the whole row then you can add flex-grow: 1 to .item.

like image 69
bygrace Avatar answered Sep 21 '22 08:09

bygrace


You can add another div with flex style for holding the needed gap between inner divs. and for the minimum width for that gap use this property (as mentioned in W3Schools.com):

flex: flex-grow flex-shrink flex-basis|auto|initial|inherit;

which flex-shrink is :

flex-shrink: A number specifying how much the item will shrink relative to the rest of the flexible items

so, for example you set this css code for the gap div :

flex: 1 0 10px; 

that tells gap div will have 10px width, and will grow relative to the rest of the flexible items, but WON'T SHRINK. so the minimum width will be 10px at the narrowest width of the screen.

like image 22
PiML Avatar answered Sep 21 '22 08:09

PiML