Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left-align last row of flexbox using space-between and margins [duplicate]

Tags:

I've got an unknown number of elements in a container that need to wrap with no margins on the outside, but minimum margins between them.

I also need these to be justified with space-between and the last row left aligned.

I'm trying to do this with flexbox like so:

.outside {
  border: 1px solid black;
}
.container {
  margin: -5px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.container:after {
  content: '';
  flex: auto;
}
.box {
  background: red;
  width: 50px;
  height: 50px;
  margin: 5px;
}
<div class="outside">
  <div class="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
</div>

View JSFiddle

This works correctly, except that the spacing on the last row is off, as you can see in the screenshot:

enter image description here

Is there any way to get this to work if we don't know how many columns there will be (using flexbox or something else other than javascript)?

like image 757
James Simpson Avatar asked Dec 02 '16 16:12

James Simpson


People also ask

How do I align my last flex to the left?

The simplest way to do this is with a grid instead of flex and grid template columns with repeat and auto fills , where you have to set the number of pixels that you have given to each element, 100px from your snippet code. This is the proper solution to get last row items left aligned.

How do I give space between flexbox?

To set space between the flexbox you can use the flexbox property justify-content you can also visit all the property in that link. We can use the justify-content property of a flex container to set space between the flexbox.

Can I use margin with flexbox?

If you apply auto margins to a flex item, that item will automatically extend its specified margin to occupy the extra space in the flex container, depending on the direction in which the auto-margin is applied.

How do I get rid of the gap on my flexbox?

To override this behavior, apply align-content: flex-start to the container. When you're working in a single-line flex container (i.e., flex-wrap: nowrap ), the properties to use to distribute space along the cross axis are align-items and align-self .


2 Answers

Last row alignment is a common problem with flexbox.

One method to consider is using invisible flex items after the last visible item. For short, I just call them "trailing phantom items".

.outside {
  border: 1px solid black;
}
.container {
  margin: -5px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.container:after {
  content: '';
  flex: auto;
}
.box {
  background: red;
  width: 50px;
  height: 50px;
  margin: 5px;
}

.hidden {
  visibility: hidden;
  margin: 0 5px;
  height: 0;
}
<div class="outside">
  <div class="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
    <div class="box hidden"></div>
  </div>
</div>
like image 164
Michael Benjamin Avatar answered Oct 05 '22 16:10

Michael Benjamin


Michael is right that the problem is you can't do space-between and then select the last row to be flex-start. The part that sticks out to me though is the specified width. Is that important?

If not, the usual way to do this would be to use media queries to control how many items are displayed per row. You can set a lots of media query steps to make sure the items don't stretch too much, but that way the space between more closely lines up with your normal grid gutters and it makes the "last row problem" go away. Think that would work?

Example: http://codepen.io/anon/pen/JbpPKa

like image 42
Joshua Ogle Avatar answered Oct 05 '22 16:10

Joshua Ogle