Kombai Logo

Tailwind CSS Order

The order property in CSS plays a crucial role in defining how flex and grid items are laid out within their container. By default, items are arranged in the order they appear in the markup. However, the order property provides a way to rearrange items visually without changing the source HTML structure. Tailwind CSS simplifies this with a range of utility classes for controlling the order property, catering to both simple and advanced layouts.

In this document, we'll dive into the mechanics of order utilities in Tailwind CSS, examining their usage with flex and grid containers, applying conditional logic based on states, and customizing the order for specific needs.

ClassPropertiesExample
order-1order: 1;<div className="order-1"></div>
order-2order: 2;<div className="order-2"></div>
order-3order: 3;<div className="order-3"></div>
order-4order: 4;<div className="order-4"></div>
order-5order: 5;<div className="order-5"></div>
order-6order: 6;<div className="order-6"></div>
order-7order: 7;<div className="order-7"></div>
order-8order: 8;<div className="order-8"></div>
order-9order: 9;<div className="order-9"></div>
order-10order: 10;<div className="order-10"></div>
order-11order: 11;<div className="order-11"></div>
order-12order: 12;<div className="order-12"></div>
order-firstorder: -9999;<div className="order-first"></div>
order-lastorder: 9999;<div className="order-last"></div>
order-noneorder: 0;<div className="order-none"></div>

Overview of Order

Tailwind CSS offers utility classes to control the order property seamlessly, making it straightforward to reorganize content dynamically. Here's how to get started with adding order adjustments:

Adding the Order

Reordering items in flex and grid layouts is simple using Tailwind's utilities. By assigning order values to items, you can dictate their rendered position, overriding the natural flow of the document.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Adding the Negative Order

Sometimes, negative ordering is essential when you need certain elements to appear before others. Tailwind simplifies this with negative utility classes.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

States and Responsiveness

Tailwind CSS order utilities integrate seamlessly with responsive design principles and dynamic states.

Hover and Focus States

Dynamic states like hover or focus enable reordering items based on user interactions. This technique is useful for creating engaging and interactive layouts.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Breakpoint Modifiers

Tailwind enables applying order preferences based on viewport size with responsive classes. This is achieved via breakpoint modifiers such as sm:, md:, and lg:.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Custom Order

For more flexibility, Tailwind's configuration allows extending or specifying custom order values in your project.

Extending the Theme

The order utility set can be extended or customized by modifying the Tailwind theme configuration. This is particularly useful for integrating project-specific naming conventions or values.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Using Arbitrary Values

For scenarios requiring precise control, arbitrary values can be powerful. These values override the predefined set, ensuring ultimate customization flexibility.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Real World Examples

A dynamic product grid layout where featured items appear first in the order using Tailwind's order utilities.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Priority Navigation Menu

A responsive navigation menu that prioritizes important items using order classes.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Social Media Feed Layout

A social media feed that orders posts based on engagement levels.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Task Priority Board

A kanban-style board that orders tasks based on priority levels.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

News Article Layout

A news feed that orders articles based on their importance and publish time.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Customization Examples

Responsive Grid Order Layout

This example demonstrates how to create a responsive grid layout with custom order values for different breakpoints.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Priority-Based Card Ordering

This example shows how to implement a priority-based card system using custom order values.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Dynamic Content Reordering

This example demonstrates how to create a dynamic content layout with custom ordering for different content types.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Best Practices

Maintain Design Consistency

When leveraging the order utilities in Tailwind CSS, it is essential to maintain visual and structural consistency throughout your UI design. Consistency simplifies both development and user experience, ensuring layouts are predictable.

For instance, when managing complex flex or grid structures, clearly define an ordering standard that aligns with your design system. Utilize Tailwind's predefined order classes like order-1, order-2, etc., to establish a logical hierarchy across components.

Design consistency can also be supported by customizing the order values in the Tailwind configuration to match project conventions. For instance, setting named values such as 'header', 'footer', and 'aside' can clarify the purpose of each element in your design structure. Always test across different UI components to ensure that applying order does not disrupt overall layout guidelines.

Optimize for Reusability

When you’re building components that rely on order utilities, it’s wise to plan for reuse and scalability. Often, you’ll find that a particular card layout or grid arrangement needs to be reused in multiple contexts across a large application.

For such cases, consider using a custom utility. For instance, you might define a utility group like .promo-card that consistently enforces the same spacing, alignment, and ordering rules across product promotions. By abstracting these classes into a single utility, you make it much easier to manage changes in the future.

Accessibility Considerations

Enhance Readability and Navigability

Reordering elements visually can have unintended consequences for users who rely on screen readers or other assistive technologies. Although the CSS order property changes how content is displayed, the underlying DOM order remains the same. Screen readers will still read content in its original sequence, which may confuse users if the visual presentation differs significantly from the logical order.

Hence, the best practice is to rely on semantic HTML structure and only use reordering sparingly. If you must reorder an element that is critical to user understanding—such as moving a form field or a CTA higher in the visible layout—consider whether it would be more appropriate to re-architect the HTML to reflect that new order. Conversely, if reordering is strictly a stylistic choice (e.g., reordering purely decorative images), it’s less likely to impact the user’s comprehension.

Ensure Keyboard Accessibility

Whenever you reorder items, confirm that it does not disrupt the intuitive tab flow or visually misalign crucial elements from their focus states. In a typical page flow, pressing the Tab key will move through elements in the order they appear in the DOM, not the order in which they appear on screen.

A best practice is to re-check the tab sequence after applying order-* utilities. Ensure that the visually topmost (or leftmost) focusable elements receive keyboard focus before those positioned lower (or to the right). If you find discrepancies, you might need to revert to a more logical DOM order or find alternative layout strategies.