Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox wrap - different alignment for last row

Tags:

html

css

flexbox

I'm using flex box to align two items to left and right of the container, while vertically centre-aligning them. Here's a very simple example of what I'm trying to achieve.

HTML:

<div class="container">
    <div class="first"></div>
    <div class="second"></div>
</div>

CSS:

.container {
    width:100%;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
}

.first {
    background-color: yellow;
    width: 200px;
    height: 100px;
}

.second {
    background-color: blue;
    width: 200px;
    height: 100px;
}

Here's the jsfiddle of the example.

This works perfectly well if the screen is wide enough to fit both internal divs on one row. However when the screen size is small (e.g. a mobile phone) and the divs wrap onto the second line, the second one also becomes aligned to the left side (i.e. flex-start). How can I force the second div to always be aligned against the right border, regardless of whether it's on the first row or wrapped onto the second one?

EDIT: In the example, I assigned fixed width to the two child elements - this is for simplicity only. In the real life application, all widths are dynamically changing based on the content read from the database at run-time. Hence, any solution that's based on fixed sizes will not work.

like image 582
Aleks G Avatar asked Nov 17 '14 23:11

Aleks G


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 align my last flex to the right?

You will see the free space at the right side of the last flex item. And if you apply margin-left: auto to the last item, the positive free space will be taken up to the left dimension and the last flex item will be pushed to the right.

How do you align Flex items in a row?

By default items start from the left if flex-direction is row , and from the top if flex-direction is column . You can change this behavior using justify-content to change the horizontal alignment, and align-items to change the vertical alignment.

How do you wrap the next line in Flex?

You can use flex-basis: 100% for item you want to wrap in new line.


1 Answers

You can try adding some left margin to push your .second element to the right:

.second {
    margin-left: auto;
}

.container {
  width:100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
}
.first {
  background-color: yellow;
  width: 200px;
  height: 100px;
}
.second {
  background-color: blue;
  width: 200px;
  height: 100px;
  margin-left: auto;
}
<div class="container">
  <div class="first"></div>
  <div class="second"></div>
</div>

Or, similarly, justify all elements to the right but push .first element to the left:

.container {
    justify-content: flex-end;
}
.first {
    margin-right: auto;
}

.container {
  width:100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
}
.first {
  background-color: yellow;
  width: 200px;
  height: 100px;
  margin-right: auto;
}
.second {
  background-color: blue;
  width: 200px;
  height: 100px;
}
<div class="container">
  <div class="first"></div>
  <div class="second"></div>
</div>
like image 173
Oriol Avatar answered Sep 21 '22 18:09

Oriol