CSS | Ordering Flex Items

The order property of CSS can be used for ordering flex items. It specifies the order of a flex item with respect to the other flex items. The element has to be a flexible item for the order property to work. The elements are displayed in ascending order of their order values. If two elements have the same order value then they are displayed on the basis of their occurrence in the source code.

Syntax:

order: integer | initial | inherit

Property Values:



  • Integer: It denotes the order of the flex items. The default value of a flex item is 0.
  • Initial: It sets the property to its default value.
  • Inherit: It means that the associated element takes the specified value of its parent element order property.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE>
<html>
  
<head>
    <title>
        CSS | Ordering Flex Items
    </title>
      
    <style>
        #GFG {
            width: 400px;
            height: 100px;
            border: 1px solid #d3d3d3;
            display: -webkit-flex; /* Safari */
            display: flex;
        }
        #GFG div {
            width: 70px;
            height: 70px;
        }
      
        /* Safari 6.1+ */
        div#second {-webkit-order: 2;}
        div#fourth {-webkit-order: 4;}
        div#third {-webkit-order: 3;}
        div#first {-webkit-order: 1;}
      
        /* Normal syntax */
        div#second {order: 2;}
        div#fourth {order: 4;}
        div#third {order: 3;}
        div#first {order: 1;}
    </style>
</head>
  
<body>
    <div id="GFG">
        <div style="background-color:yellow;" id="second"></div>
        <div style="background-color:blue;" id="fourth"></div>
        <div style="background-color:green;" id="third"></div>
        <div style="background-color:red;" id="first"></div>
    </div>
</body>
  
</html>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE>
<html>
      
<head>
    <title>
        CSS | Ordering Flex Items
    </title>
      
    <style>
        #GFG {
            width: 400px;
            height: 100px;
            border: 1px solid #d3d3d3;
            display: -webkit-flex; /* Safari */
            display: flex;
        }
        #GFG div {
            width: 70px;
            height: 70px;
        }
      
        /* Safari 6.1+ */
        div#second {-webkit-order: 2;}
        div#fourth {-webkit-order: 4;}
        div#third {-webkit-order: 3;}
        div#first {-webkit-order: 1;}
      
        /* Normal syntax */
        div#second {order: 2;}
        div#fourth {order: 4;}
        div#third {order: 3;}
        div#first {order: 1;}
    </style>
</head>
  
<body>
    <div id="GFG">
        <div style="background-color:green;" id="second"></div>
        <div style="background-color:pink;" id="fourth"></div>
        <div style="background-color:black;" id="third"></div>
        <div style="background-color:violet;" id="first"></div>
    </div>
</body>
  
</html>

chevron_right


Output:

Supported Browsers: The browsers supported by Ordering Flex Items are listed below:

  • Google Chrome 29.0, 21.0 -webkit-
  • Mozilla Firefox 28.0, 18.0 -moz-
  • Internet Explorer 11.0
  • Safari 9.0, 6.1 -webkit-
  • Opera 17.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.