CSS flex-direction Property

You are Here:

CSS flex-direction Property

CSS flex-direction property specifies the direction of the flexible items (either row wise or column wise).

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> h2+div{ display: flex; width: 100%; height: 250px; border: 1px solid #8c8c8c; } h2+div > div{ width: 50px; height: 50px; border: 1px solid #8c8c8c; margin: 10px; } #parent{ flex-direction: row; } #parent2{ flex-direction: column; } </style> </head> <body> <h1>CSS flex-direction Property</h1> <h2>flex-direction: row; (default)</h2> <div id="parent"> <div>Div 1</div> <div>Div 2</div> <div>Div 3</div> </div> <h2>flex-direction: column;</h2> <div id="parent2"> <div>Div 1</div> <div>Div 2</div> <div>Div 3</div> </div> </body> </html>

Syntax

Using CSS

element{ flex-direction: column; }

Using Javascript

object.style.flexDirection="column";

Animatable

No, flex-direction property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS flex-direction property is row.

Property Value

The following table provides a list of values for CSS flex-direction property.

ValueExplanation
rowThe flex items are displayed horizontally, as a row in same order. Here the default value for justify-content is 'flex-start'.
row-reverseThe flex items are displayed horizontally, as a row in reverse order. Here the default value for justify-content is 'flex-end'.
columnThe flex items are displayed vertically, as a column in same order. Here the default value for justify-content is 'flex-start'.
column-reverseThe flex items are displayed vertically, as a column in reverse order. Here the default value for justify-content is 'flex-end'.

Using JavaScript

In the following example, we will demonstrate how to change the CSS flex-direction property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> #parent{ display: flex; width: 100%; height: 250px; border: 1px solid #8c8c8c; } #parent > div{ width: 50px; height: 50px; border: 1px solid #8c8c8c; margin: 10px; } </style> </head> <body> <h1>CSS flex-direction Property</h1> <div id="parent"> <div>Div 1</div> <div>Div 2</div> <div>Div 3</div> </div> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementById("parent"); function myFunction(){ x.style.flexDirection = "row-reverse"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author