8

I'm using flex box to arrange some equally-sized divs, and I've set justify-content: space-around. This looks great for all the rows except the last one. I'd like any elements in the last row to be even with the left-most divs in the previous rows.

This is what it looks like:

enter image description here

and

enter image description here

And this is what I would like:

enter image description here

and

enter image description here

How can I achieve this?

Currently, I have this CSS:

#container {
  display: flex;
  flex-flow: row wrap;
  max-width: 400px;
  border: 1px solid red;
  justify-content: space-around
}

#container > div {
  border: 1px solid black;
  width: 110px;
}

With this HTML structure:

<div id="container">
  <div>Content</div>
  <div>Content</div>
  ...
  ...
</div>

Here's a fiddle: https://jsfiddle.net/chrisshroba/q2mm9ccj/

Community
  • 1
  • 1
Christopher Shroba
  • 7,006
  • 8
  • 40
  • 68
  • 2
    Maybe something like [**this**](https://jsfiddle.net/q2mm9ccj/1/) will work for you. – Michael Benjamin Apr 05 '17 at 17:27
  • Is the number of divs dynamic? Otherwise, [`align-self`](https://developer.mozilla.org/en-US/docs/Web/CSS/align-self) could be of use. – Just a student Apr 05 '17 at 17:27
  • you can use space-between and for and give margin to container – Ajay Apr 05 '17 at 17:34
  • @Michael_B Can you explain exactly why this is working? – kojow7 Nov 30 '19 at 05:09
  • @kojow7, Two things: (1) I'm not using `space-around`. I'm using `space-between`, with horizontal padding on the container. (2) Each value is designed to behave differently in the instance described above. See this post for an explanation: https://stackoverflow.com/q/36487476/3597276 – Michael Benjamin Nov 30 '19 at 18:59
  • @Michael_B Okay, thank you that makes sense. If you have time please see my latest question. I am wanting to use flexbox (rather than gridbox), need items to line up, but the items are dynamic in size and thus need to match to the largest width. – kojow7 Dec 01 '19 at 01:49

0 Answers0