Open In App

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:

Example 1:




<!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>

Output:



Example 2:




<!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>

Output:

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


Article Tags :