Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex-box: Align last row to grid

I have a simple flex-box layout with a container like:

.grid {   display: flex;   flex-flow: row wrap;   justify-content: space-between; } 

Now I want the items in the last row to be aligned with the other. justify-content: space-between; should be used because the width and height of the grid can be adjusted.

Currently it looks like

The item in the bottom right should be in the middle

Here, I want the item in the bottom right to be in the "middle column". What is the simplest way to accomplish that? Here is a small jsfiddle that shows this behaviour.

.exposegrid {    display: flex;    flex-flow: row wrap;    justify-content: space-between;  }    .exposetab {    width: 100px;    height: 66px;    background-color: rgba(255, 255, 255, 0.2);    border: 1px solid rgba(0, 0, 0, 0.4);    border-radius: 5px;    box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);    margin-bottom: 10px;  }
<div class="exposegrid">    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>    <div class="exposetab"></div>  </div>
like image 303
Thorben Croisé Avatar asked Sep 11 '13 14:09

Thorben Croisé


People also ask

How do you align rows in a flexbox?

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

To align one flex child to the right set it with margin-left: auto; From the flex spec: One use of auto margins in the main axis is to separate flex items into distinct "groups".

Can you mix flexbox and grid together?

You can use them together but not necessarily one the same element. For instance I can place a div in a CSS-Grid parent container (which has display:grid ) but the child div could have display:flex to lay out it's children.


1 Answers

Add a ::after which autofills the space. No need to pollute your HTML. Here is a codepen showing it: http://codepen.io/DanAndreasson/pen/ZQXLXj

.grid {   display: flex;   flex-flow: row wrap;   justify-content: space-between; }  .grid::after {   content: "";   flex: auto; } 
like image 199
Dan Andreasson Avatar answered Oct 05 '22 14:10

Dan Andreasson