CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Order Property


The order property controls the order in which flex items appear in the flex container. Items with a lower order value appear first. The default value is 0.

Example 1: Basic Reordering

.flex-container {
  display: flex;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightcyan;
  border: 1px solid #000;
}

.item-1 {
  order: 2; /* Will appear second */
}

.item-2 {
  order: 1; /* Will appear first */
}

Explanation

Despite .item-1 coming first in the HTML, .item-2 is displayed first because its order value of 1 is lower than .item-1's value of 2.


Example 2: Negative Order

.flex-container {
  display: flex;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightyellow;
  border: 1px solid #000;
}

.first-item {
  order: -1; /* Will appear before items with the default order of 0 */
}

Explanation

The order property accepts negative values. The .first-item is moved to the beginning of the container because -1 is less than the default order of 0 for the other items.


Example 3: Ordering a Mix of Items

.flex-container {
  display: flex;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lavender;
  border: 1px solid #000;
}

.a { order: 3; }
.b { order: 1; }
.c { order: 2; }

Explanation

The items are reordered based on their order values. The final display order will be B, C, A, demonstrating how to completely customize the visual order of elements.


Example 4: Using the Same Order

.flex-container {
  display: flex;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lemonchiffon;
  border: 1px solid #000;
}

.item-1 { order: 1; }
.item-2 { order: 1; }
.item-3 { order: 0; }

Explanation

If multiple items have the same order value, their relative order is determined by their source order in the HTML. Here, .item-3 comes first (order 0), followed by .item-1 and then .item-2.


Example 5: Promoting an Item

.flex-container {
  display: flex;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightblue;
  border: 1px solid #000;
}

.featured {
  order: -1; /* Moves this item to the front */
  font-weight: bold;
}

Explanation

This is a common use case for order. A "featured" item can be moved to the beginning of a list for emphasis, regardless of its position in the document structure.


Example 6: Responsive Ordering

.flex-container {
  display: flex;
  flex-direction: column; /* Items are stacked vertically */
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 50px;
  background-color: lightgreen;
  border: 1px solid #000;
}

@media (min-width: 600px) {
  .flex-container {
    flex-direction: row; /* On wider screens, items are in a row */
  }
  .main-content { order: 2; }
  .sidebar { order: 1; }
  .header { order: 0; }
}

Explanation

The order property is very powerful for responsive design. Here, the layout is a single column on mobile, but on wider screens, it becomes a row with a custom order for the header, sidebar, and main content.


Example 7: Ordering with Gaps

.flex-container {
  display: flex;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid #000;
}

.one { order: 10; }
.two { order: 20; }
.three { order: 5; }

Explanation

The actual numerical values of order don't matter as much as their sequence. Even with large gaps between the order numbers, the items will be arranged from the lowest value to the highest: .three, .one, then .two.