Tillitsdone
down Scroll to discover

Understanding CSS flex-direction A Comprehensive Guide

Discover how to use the CSS flex-direction property to control layouts.

Learn about its use cases and the available options row, row-reverse, column, and column-reverse.
thumbnail

Introduction

The flex-direction property in CSS is a key part of the Flexbox layout system. It defines the main axis and the direction (normal or reversed) in which flex items are placed within a flex container. This property is widely supported, making it reliable for web developers and designers.

Specification

The flex-direction property is part of the CSS Flexible Box Layout Module Level 1 specification. This module defines a CSS box model optimized for layout design, making it easier to design flexible and responsive web pages. The flex-direction property specifically determines the direction in which flex items are placed within a flex container.

Description

The flex-direction property in CSS is used to define the main axis of a flex container and the direction in which the flex items are placed along this axis. This property is essential for controlling the layout of flex items within a container, offering both horizontal and vertical alignment options.

Syntax

The flex-direction property in CSS is straightforward to use. Here is the basic syntax:

flex-direction: row | row-reverse | column | column-reverse;

You can also use global values such as inherit, initial, revert, revert-layer, and unset. Here is a more detailed breakdown of the syntax:

/* The direction text is laid out in a line */
flex-direction: row;
/* Like <row>, but reversed */
flex-direction: row-reverse;
/* The direction in which lines of text are stacked */
flex-direction: column;
/* Like <column>, but reversed */
flex-direction: column-reverse;
/* Global values */
flex-direction: inherit;
flex-direction: initial;
flex-direction: revert;
flex-direction: revert-layer;
flex-direction: unset;

Values

The flex-direction property in CSS accepts several values that determine the direction in which flex items are placed within a flex container. Here are the primary values you can use:

  1. row:
    • Description: Places flex items horizontally, following the normal text direction (left to right).
    • Usage: flex-direction: row;
  2. row-reverse:
    • Description: Places flex items horizontally but in reverse order (right to left).
    • Usage: flex-direction: row-reverse;
  3. column:
    • Description: Places flex items vertically, from top to bottom.
    • Usage: flex-direction: column;
  4. column-reverse:
    • Description: Places flex items vertically but in reverse order (bottom to top).
    • Usage: flex-direction: column-reverse;

Global Values

In addition to the primary values, the flex-direction property also accepts several global values:

  • inherit: Inherits the value from its parent element.
    • Usage: flex-direction: inherit;
  • initial: Sets the value to its default value (row).
    • Usage: flex-direction: initial;
  • revert: Rolls back the value to the user agent’s default stylesheet value.
    • Usage: flex-direction: revert;
  • revert-layer: Rolls back the value to the user agent’s default stylesheet value for a specific layer.
    • Usage: flex-direction: revert-layer;
  • unset: Resets the value to its natural value, which means that if the property is naturally inherited it acts like inherit, otherwise it acts like initial.
    • Usage: flex-direction: unset;

Accessibility

Using the flex-direction property with values of row-reverse or column-reverse can create a disconnect between the visual presentation of content and the DOM order. This discrepancy can adversely affect users who rely on assistive technologies, such as screen readers, as the visual order may not match the reading order provided by these tools.

To mitigate this problem, carefully consider the use of row-reverse and column-reverse. If the visual order is important for the design and user experience, ensure that the DOM order also reflects this arrangement. This approach helps maintain consistency between the visual and logical structure of the content, providing a better experience for all users.

Examples

To better understand how the flex-direction property works, let’s explore some practical examples. These examples demonstrate how to use row, row-reverse, column, and column-reverse values to control the layout of flex items within a container.

HTML

<div id="main">
<div class="box red">1</div>
<div class="box lightblue">2</div>
<div class="box yellow">3</div>
</div>

CSS

#main {
display: flex;
flex-direction: row; /* or row-reverse, column, column-reverse */
}
.box {
width: 100px;
height: 50px;
margin: 5px;
text-align: center;
line-height: 50px;
}
.red {
background-color: red;
}
.lightblue {
background-color: lightblue;
}
.yellow {
background-color: yellow;
}

Example 1: row

The row value places flex items horizontally, following the normal text direction (left to right).

Example 2: row-reverse

The row-reverse value places flex items horizontally but in reverse order (right to left).

Example 3: column

The column value places flex items vertically, from top to bottom.

Example 4: column-reverse

The column-reverse value places flex items vertically but in reverse order (bottom to top).

Example 5: Combining Flex Direction with Other Properties

You can also combine flex-direction with other Flexbox properties to create more complex layouts. For example, you can use flex-wrap to control how items wrap within the container.

HTML

<div id="main">
<div class="box red">1</div>
<div class="box lightblue">2</div>
<div class="box yellow">3</div>
<div class="box green">4</div>
<div class="box purple">5</div>
</div>

CSS

#main {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 300px;
}
.box {
width: 100px;
height: 50px;
margin: 5px;
text-align: center;
line-height: 50px;
}
.red {
background-color: red;
}
.lightblue {
background-color: lightblue;
}
.yellow {
background-color: yellow;
}
.green {
background-color: green;
}
.purple {
background-color: purple;
}

FAQs

What does the flex-direction property do in CSS?

The flex-direction property in CSS defines the direction in which flex container items are placed. It determines whether the items are displayed in a row, column, or in reverse order. This property is essential for controlling the layout of flex items within a container, offering both horizontal and vertical alignment options.

What are the possible values for flex-direction?

The flex-direction property accepts four main values:

  • row: Places items horizontally, following the normal text direction (left to right).
  • row-reverse: Reverses the order of items, arranging them from right to left.
  • column: Stacks items vertically, starting from the top and moving down.
  • column-reverse: Flips the vertical order, displaying items from bottom to top.

How does flex-direction: column affect layout?

When flex-direction is set to column, the flex items are arranged vertically, stacking on top of each other. This layout is useful for vertical navigation menus or vertically aligned content sections. It makes the item follow the normal top-to-bottom direction.

What happens if I use flex-direction: row-reverse?

Using flex-direction: row-reverse arranges the flex items horizontally but in reverse order, from right to left. This can be useful for creating unique layouts, but it’s important to consider the impact on accessibility, as it may cause a disconnect between the visual and DOM order.

Can I Change flex-direction Responsively?

Yes! You can change flex-direction based on screen size using media queries. For example, you can set a row layout for larger screens and switch to a column layout for smaller screens. This makes your designs responsive.

Why Is Accessibility Important with flex-direction?

Using flex-direction with values like row-reverse or column-reverse can cause a disconnect between the visual presentation and the DOM order. This can affect users with low vision or those using screen readers. Ensuring the visual order matches the DOM order is crucial for maintaining accessibility.

How Can I Mitigate Accessibility Issues with flex-direction?

To mitigate accessibility issues, carefully consider the use of row-reverse and column-reverse. If the visual order is important, ensure that the DOM order also reflects this arrangement. This helps maintain consistency between the visual and logical structure of the content.

Are There Global Values for flex-direction?

Yes, flex-direction accepts several global values:

  • inherit: Inherits the value from its parent element.
  • initial: Sets the value to its default value (row).
  • revert: Rolls back the value to the user agent’s default stylesheet value.
  • revert-layer: Rolls back the value to the user agent’s default stylesheet value for a specific layer.
  • unset: Resets the value to its natural value, which means that if the property is naturally inherited it acts like inherit, otherwise it acts like initial.

Where Can I Find More Information About flex-direction?

For more information and examples, check out:

Browser Compatibility

The flex-direction property is well-supported across modern browsers, including:

  • Google Chrome: Supported since version 29.0 (August 2013).
  • Mozilla Firefox: Supported since version 28.0 (March 2014).
  • Internet Explorer/Edge: Supported since version 11.0 (October 2013).
  • Opera: Supported since version 17.0 (August 2013).
  • Safari: Supported since version 9.0 (September 2015).

Ensuring Compatibility

To ensure compatibility across different browsers:

  1. Use Feature Queries: Check if a browser supports the flex-direction property before applying it.
    @supports (flex-direction: row) {
    .container {
    display: flex;
    flex-direction: row;
    }
    }
  2. Fallback Styles: Provide fallback styles for older browsers.
    .container {
    display: block; /* Fallback for older browsers */
    }
    @supports (display: flex) {
    .container {
    display: flex;
    flex-direction: row;
    }
    }
  3. Testing: Regularly test your web pages on different browsers and devices.

Related Properties

The flex-direction property works with other Flexbox properties to create flexible and responsive designs. Here are some related properties:

  1. flex-wrap: Controls whether flex items wrap onto multiple lines.
    flex-wrap: wrap;
  2. flex-flow: Sets both flex-direction and flex-wrap simultaneously.
    flex-flow: row wrap;
  3. justify-content: Defines how space is distributed along the main axis.
    justify-content: space-between;
  4. align-items: Defines how space is distributed along the cross axis.
    align-items: center;
  5. align-content: Defines how space is distributed along the cross axis when there is extra space.
    align-content: space-between;
  6. order: Specifies the order of flex items.
    order: 1;
  7. flex-grow: Specifies the flex grow factor of a flex item.
    flex-grow: 1;
  8. flex-shrink: Specifies the flex shrink factor of a flex item.
    flex-shrink: 1;
  9. flex-basis: Specifies the initial main size of a flex item.
    flex-basis: 200px;
  10. flex: Sets flex-grow, flex-shrink, and flex-basis simultaneously.
    flex: 1 1 200px;

See Also

For further reading and resources on CSS Flexbox and related properties, check out:

These resources provide comprehensive guides, examples, and best practices for using Flexbox and related properties effectively in your web development projects.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.