Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change flex direction on wrap

I am trying to use a nested flexbox layout in order to achieve the following layout when there is a certain amount of space:

Example

Then, when there isn't enough space, the right-hand side should wrap to a row layout instead of a column layout, so that the boxes are side-by-side when they wrap, example:

Small Layout

The idea is to have the boxes on the right 50% of the left content so that when they wrap underneath, they are the same combined size as the left content.

However, I can't figure out how to do this via flexbox only. This is what I have so far, but if you resize the window you'll see that the boxes stay in the same direction:

http://jsfiddle.net/usLxshro/

div {
    min-height: 50px;
    background: #E7E7E7;
    margin: 10px;
}
#wrapper {
    display: flex;
    flex-wrap: wrap;
}
#left-content {
    flex: 2;
    min-width: 200px;
}
#right-content {
    flex: 1;
    min-width: 200px;
}
section {
    border: 1px solid #333;
    margin: 10px;
}
like image 436
CodingIntrigue Avatar asked May 11 '15 07:05

CodingIntrigue


People also ask

How do I change the direction on my flex?

CSS Demo: flex-directionIf its dir attribute is ltr , row represents the horizontal axis oriented from the left to the right, and row-reverse from the right to the left; if the dir attribute is rtl , row represents the axis oriented from the right to the left, and row-reverse from the left to the right.

How do you reverse a flex item?

default flex-direction: row; The flexbox items are ordered the same way as the text direction, along the main axis. flex-direction: row-reverse; The flexbox items are ordered the opposite way as the text direction, along the main axis.

What is flex-wrap-reverse?

wrap-reverse. Specifies that the flexible items will wrap, if necessary, in reverse order. Demo ❯ initial. Sets this property to its default value.


1 Answers

You can use media-queries to change behavior

@media (max-width: 500px) {
    #wrapper {
        flex-direction: column;    
    }
    #right-content {
        display: flex;
    }

    section {
        flex: 1;   
    }
}

See updated fiddle — http://jsfiddle.net/shurshilin/usLxshro/1/

Hope it'll help you.

like image 72
Dmitry Shurshilin Avatar answered Sep 29 '22 17:09

Dmitry Shurshilin