Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Using flex-direction to layout content horizontally and vertically


    The Flexbox css spec allows for more adjustable layouts. The flex-direction property allows you to easily change the layout on the children of an element without making any changes to the dom, which is particularly useful when combined with media queries.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Flexbox is a fairly new layout mode in CSS, and it gives us a lot of new properties that can be adjusted to output a wide variety of layouts.

    By default, content on a page flows vertically from top to bottom. In this example, we have an unordered list of links, similar to something that could be used as a web page navigation. I'm going to go ahead and remove the bullets from the list, by setting the list style to none on the unordered list. Then I'll set display to flex.

    Flex will leave this unordered list as a block level container, and will affect the layout of its children. If I wanted the container to be an inline element, but also wanted the contents to be displayed using flexbox, I could change display to inline flex.

    Both flex and inline flex will lay out the children the same. But flex makes the container a block element, breaking the horizontal flow with its siblings, and inline flex, like an image tag, will not.

    Notice when I added display flex, that the links changed orientation to be laid out horizontally. That's because flexbox containers have a property called flex direction, which defaults to row, which, like the rows of a table, are laid out horizontally.

    I can change it back to a vertical layout by changing flex direction to column. Additionally, I have two alternative layouts which affect the order of the children. Column reverse will cause the children to flow from bottom to top, and row reverse will lay them out from left to right.

    Flex direction is most useful in the context of a responsive web design. When combined with media queries, it can adjust the layout direction of the children based on the size of the window.

    Currently, I'm working on a website that has a horizontal navigation. But when the window size is closer to a mobile screen, the navigation is laid out in a vertical list to make better use of the screen real estate.

    It is important to note that many flexbox properties, including flex direction, need prefixes for some browsers. Make sure to double check documentation, or better yet, use the auto prefixer post CSS tool to automatically add the needed prefixes.