Bootstrap, ordering grid items


In Bootstrap, ordering grid items allows you to control the sequence in which columns appear within a row. This feature is particularly useful for creating responsive layouts where the order of elements needs to change based on screen size.

Bootstrap uses the Flexbox layout model to enable flexible and customizable ordering of grid items. Here’s a detailed explanation of how ordering works in Bootstrap:

Ordering Grid Items

Bootstrap provides classes that allow you to change the order of columns within a row without altering the HTML structure. These classes use the Flexbox order property to rearrange items.

Basic Ordering Classes

  • .order-first: Moves the item to the first position in the row.
  • .order-last: Moves the item to the last position in the row.
  • .order-0, .order-1, .order-2, etc.: Sets the order of items based on numerical values, where order-0 is the default.

Example:

<div class="row"> <div class="col-4 order-2 bg-primary text-white">Column 1</div> <div class="col-4 order-1 bg-secondary text-white">Column 2</div> <div class="col-4 order-3 bg-success text-white">Column 3</div> </div>

In this example:

  • Column 2 will appear first.
  • Column 1 will appear second.
  • Column 3 will appear third.

Responsive Ordering

Bootstrap allows you to apply different ordering rules based on screen sizes, making your layout more adaptable to various devices.

Responsive Ordering Classes:

  • .order-sm-*: Applies ordering on small screens and up.
  • .order-md-*: Applies ordering on medium screens and up.
  • .order-lg-*: Applies ordering on large screens and up.
  • .order-xl-*: Applies ordering on extra-large screens and up.
  • .order-xxl-*: Applies ordering on extra-extra-large screens and up (Bootstrap 5).

Example:

<div class="row"> <div class="col-4 order-2 order-sm-1 bg-primary text-white">Column 1</div> <div class="col-4 order-1 order-sm-2 bg-secondary text-white">Column 2</div> <div class="col-4 order-3 order-sm-3 bg-success text-white">Column 3</div> </div>

In this example:

  • On extra small screens, Column 2 appears first, Column 1 appears second, and Column 3 appears third.
  • On small screens and up, the order of columns will be as specified by the responsive classes.

Key Points About Ordering

  1. Flexbox-Based: Ordering is based on Flexbox, which allows for flexible arrangement of items in a container.
  2. Non-destructive: Ordering classes rearrange items visually without changing the underlying HTML structure, ensuring that screen readers and other assistive technologies read the content in the intended order.
  3. Responsive Design: Using responsive ordering classes, you can customize the order of elements for different screen sizes, which is useful for creating mobile-friendly designs.
  4. Flexibility: You can use ordering classes in combination with alignment and justification classes to achieve complex layouts.

Summary

  • Basic Ordering: Use .order-first, .order-last, and .order-* classes to control the visual order of grid items.
  • Responsive Ordering: Apply .order-sm-*, .order-md-*, .order-lg-*, .order-xl-*, and .order-xxl-* classes to adjust item order based on different screen sizes.
  • Flexbox: Bootstrap’s ordering functionality leverages Flexbox, allowing for flexible and responsive layouts without altering HTML.