Reordering Content Responsively with Bootstrap 5’s Flexbox Grid
When a layout needs to shift a sidebar beneath the main article on phones but stay beside it on tablets, duplicating markup is a common workaround. Bootstrap 5’s flexbox grid lets you reorder columns visually without extra HTML.
11 Apr 2026, 09:22 UTC

The Problem: Changing Visual Order Without Duplicating Markup
When a layout needs to shift a sidebar beneath the main article on phones but stay beside it on tablets and up, developers often duplicate the markup or write custom media queries. Both approaches increase maintenance overhead and risk inconsistencies.
How Bootstrap 5’s Flexbox Grid Works
Bootstrap 5’s grid is built on CSS flexbox. The .row class creates a flex container (display:flex) with wrapping enabled (flex-wrap:wrap). Columns are defined with .col-* classes that allocate a fraction of the 12‑unit flex basis. When the total exceeds 12, flexbox automatically wraps the overflow to a new line, eliminating the need for legacy clearfix hacks.
Applying Responsive Column Widths and Wrapping
Responsive breakpoints are encoded as infixes: xs (default), sm, md, lg, xl, xxl. A class like col-md-6 means "six columns wide at medium viewports and larger; otherwise full width". Because the row wraps, three col-md-4 columns sit side‑by‑side on ≥md screens and stack vertically on smaller viewports.
Reordering Columns with Order Utilities
Flexbox also provides order‑control utilities: .order-first, .order-last, and .order-*-* (where * is a breakpoint and a number 1‑12). These adjust the order CSS property, changing visual placement without altering DOM order. For example, order-sm-2 order-md-1 makes an element appear second on small viewports but first on medium and up.
Worked Example: A Card Deck that Shifts on Mobile
The following snippet shows two cards that sit side‑by‑side on tablets and desktops, but stack with the second card appearing first on phones.
<!-- Include Bootstrap 5 CSS via CDN -->
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css' rel='stylesheet'>
<div class='container py-4'>
<div class='row g-3'>
<div class='col-12 col-md-6 order-md-2'>
<div class='card h-100'>
<div class='card-body'>
<h5 class='card-title'>Feature A</h5>
<p class='card-text'>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
</div>
<div class='col-12 col-md-6 order-md-1'>
<div class='card h-100'>
<div class='card-body'>
<h5 class='card-title'>Feature B</h5>
<p class='card-text'>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
</div>
</div>
</div>
</div>
</div>
To verify the behavior:
- Open the file in a browser (any modern Chrome, Firefox, Safari, or Edge).
- Resize the viewport width below 768 px (the
mdbreakpoint). The cards should stack, with Card B on top. - Widen the viewport to ≥768 px. The cards return to side‑by‑side, with Card A on the left.
- Open DevTools, select the
.rowelement, and confirm the computed styles showdisplay:flexandflex-wrap:wrap. The gutter classg-3applies negative margins to the row and padding to the columns.
Trade‑offs and Limitations
- Browser support: The grid relies on flexbox, which is unsupported in Internet Explorer 10 and earlier. Bootstrap 5 intentionally drops IE support, so the layout will break in those browsers.
- Accessibility considerations: Visual reordering via
order-*can disconnect the visual sequence from the DOM order, potentially confusing keyboard navigation and screen‑reader users if the source order does not follow a logical flow. Keep the DOM order meaningful (e.g., main content first) and use order utilities only for the visual shift.
Actionable Closing
When you need a sidebar to appear beneath the main article on phones but stay beside it on tablets and up, place both elements in a single .row, give them complementary col-* widths, and add the appropriate order-*-* classes. Keep the DOM order logical (main content first) and rely on the utilities solely for the visual change. Test each breakpoint, inspect the flex properties in DevTools, and you’ll have a responsive, maintainable layout without duplicated markup or custom media queries.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.