CSS Properties
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.
.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
Value | Description |
---|---|
Positive Integer | Items with higher positive values will appear later in the order. |
Negative Integer | Items with negative values will appear earlier in the order. |
Zero | This 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.
<!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:
Author
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
If you have any doubts regarding this article (CSS order Property), please comment here. I will help you immediately.