Suppose I have this:
.flex-container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
<div class="flex-container">
<div><a href="#">1</a></div>
<div><a href="#">2</a></div>
<div><a href="#">3</a></div>
</div>
How can I use flexbox to order the items, so the layout would be like this?
I cannot change the order of the divs in the HTML, since it would jeopardize the layout of a different view.
Reverse the row order by using
row-reverse
instead ofrow
for the flex-direction.You can also order the children manually by giving them each an
order
property that takes an integer.Its easy if you can switch to
CSS Grid layout
:create an grid container on
flex-cotainer
elementplace the second element to first column (using
grid-column: 1
) spanning 2 rows (usinggrid-row: 1 / 3
).See demo below:
A flexbox solution is hacky at best using negative margins and known heights - see below:
You shouldn't use Flexbox for this because it's not designed for it. Consider using CSS Grid instead. Using
grid-template-areas
, assigning the children where we want them in the layout becomes a trivial task. TheDOM
order of the grid children becomes irrelevant.