Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS order Property

Posted in CSS Tutorial
Updated on Oct 07, 2024
By Mari Selvan
👁️ 15 - Views
⏳ 4 mins
💬 1 Comment
CSS order Property

Photo Credit to CodeToFun

🙋 Introduction

The order property in CSS is a part of the Flexible Box Layout Module, commonly known as Flexbox. It allows developers to control the order in which flex items appear within a flex container.

By default, flex items are displayed in the order they appear in the source code. However, the order property enables you to rearrange these items without altering the HTML structure, providing greater flexibility in layout design.

💡 Syntax

The syntax for the order property is simple. It accepts a single integer value, which determines the item's position among its siblings within the flex container.

Syntax
Copied
Copy To Clipboard
.item {
  order: integer;
}
  • integer: The integer value can be positive, negative, or zero. The default value is 0.

🎛️ Default Value

The default value of the order property is 0. All items have the same order value by default, so they are displayed in the order they appear in the HTML source.

🏠 Property Values

ValueDescription
Positive IntegerItems with higher positive values will appear later in the order.
Negative IntegerItems with negative values will appear earlier in the order.
ZeroThis is the default value, indicating that the item should appear in its original order in the source code.

📄 Example

In this example, we'll rearrange the order of three items within a flex container using the order property.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS order Property Example</title>
  <style>
    .container {
      display: flex;
    }
    .item {
      padding: 10px;
      margin: 5px;
      background-color: lightblue;
      border: 1px solid #000;
    }
    .item1 {
      order: 2;
    }
    .item2 {
      order: -1;
    }
    .item3 {
      order: 0;
    }
  </style>
</head>
<body>
  <h1>Flexbox Order Property Example</h1>
  <div class="container">
    <div class="item item1">Item 1</div>
    <div class="item item2">Item 2</div>
    <div class="item item3">Item 3</div>
  </div>
</body>
</html>

In this example:

  • Item 1 has an order value of 2, so it appears last.
  • Item 2 has an order value of -1, so it appears first.
  • Item 3 has an order value of 0, so it appears in the middle.

🖥️ Browser Compatibility

The order property is widely supported in modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. As always, it's recommended to test your layout across different browsers to ensure consistent behavior.

🎉 Conclusion

The order property is a powerful feature of Flexbox, enabling developers to control the visual arrangement of elements without modifying the underlying HTML structure.

This property is particularly useful for responsive designs, where the layout needs to adapt to different screen sizes and orientations. By using the order property, you can create more flexible and dynamic layouts for your web projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy