Module 11 : Order Classes & Responsive Utilities.
Bootstrap’s order classes (control visual order in flex/grid layouts) and responsive utilities (show/hide, display, flex-direction, alignment, spacing helpers) to build adaptive, accessible, and maintainable responsive UIs.
Concepts
1. Visual order vs DOM order
DOM order is the order elements appear in your HTML source. It impacts accessibility and keyboard navigation.
Visual order is how elements appear on screen. Bootstrap’s order utilities change visual order by applying CSS order (a Flexbox property) or grid order where applicable, without changing the DOM.
Why this matters:
Screen readers and keyboard navigation follow DOM order. Use order utilities only when visual reordering is needed and ensure the DOM order still makes sense for accessibility.
2. How Bootstrap order classes work
Classes are of the form order-{breakpoint}-{value} or order-{value} (depending on version). They set the CSS order property.
order values typically include 0, 1, 2, ..., and special values like first/last (which map to negative / large order values).
Responsive variants: applying order-sm-1 changes order at sm and up; below sm the element keeps its previous order.
Mechanics:
Applied to flex/grid items — parent must have display: flex (or .d-flex) or be a Bootstrap row/column that uses flexbox.
Browser computes layout using the order number: smaller numbers appear earlier.
3. Responsive display utilities
d-none, d-inline, d-block, d-flex, etc. hide or change display.
Responsive variants: d-sm-none hides on sm and up, d-md-block shows block at md and up.
Use these to show alternative UI elements at different breakpoints (e.g., simplified header on mobile).
Important interactions:
Hiding elements vs visually moving them: If you hide an element on mobile but still move it visually using order, the hidden element won't be visible — combine carefully.
Avoid having two interactive copies of the same control with different visibility — duplicate interactive elements can confuse assistive tech unless managed correctly.
4. Common responsive patterns
Mobile-first vertical stack → Desktop multi-column: Keep DOM order for mobile stack, then use order-md-* to create a desktop sequence.
Feature emphasis: Move a promotional card order-md-first on larger screens.
Swap sidebar & main: On small screens keep sidebar below main; on larger screens show sidebar to left using order classes.
Adaptive controls: Show compact icons on sm via d-sm-none / d-none d-sm-inline.
Pattern A — Swap two columns on desktop
HTML DOM (mobile-friendly order):
<div class="row"> <div class="col-12 col-md-4">Sidebar</div> <div class="col-12 col-md-8">Main content</div> </div>
If you want the sidebar to appear visually to the left on desktop but keep DOM order (for keyboard flow) you can:
<div class="row"> <div class="col-12 col-md-4 order-md-1">Sidebar</div> <div class="col-12 col-md-8 order-md-0">Main content</div> </div>
Explanation: On md and up, order-md-0 is shown first. On mobile they stay stacked in DOM order.
Pattern B — Make a CTA card appear first on large screens
<div class="d-flex flex-column flex-lg-row"> <div class="card order-lg-2">Regular content</div> <div class="card order-lg-1">CTA card — appears first on large screens</div> </div>
Explanation: On lg and up, the CTA uses order-lg-1 to be shown before the other card.
Pattern C — Show compact nav on mobile, full nav on desktop
<nav class="d-block d-md-none"> <!-- mobile simple nav --> </nav> <nav class="d-none d-md-block"> <!-- full nav for md+ --> </nav>
Note: Keep only one set of interactive elements if possible to avoid duplicate focusable controls.
Example
Create a responsive profile layout where avatar is above on mobile, left on tablet, and right on desktop — while keeping DOM order friendly for screen readers.
<!-- Parent: flex container --> <div class="d-flex flex-column flex-md-row align-items-start"> <!-- DOM order: header, avatar, details --> <header class="mb-3 w-100">Profile header (name, actions)</header> <aside class="order-md-2 col-md-4"> <!-- Avatar: on md show right (order-md-2), mobile it appears after header --> <img src="avatar.jpg" alt="Profile picture" class="img-fluid rounded-circle"> </aside> <main class="order-md-1 col-md-8"> <!-- Details: appear left on md via order-md-1 --> <p>Bio, stats, contact, etc.</p> </main> </div>
explanation:
flex-column (mobile): header → avatar → details stacked.
On md and up flex-md-row: children laid out horizontally.
order-md-1 puts main before aside on md+ so details are left and avatar is right.
DOM order keeps header first for screen readers; avatar and details remain in DOM order that makes sense.
Exercises
Exercise 1 — Swap content cards at lg
Build three cards in a horizontal row on lg+ and stacked on mobile.
Make the middle card appear first on lg using order utilities.
Starter HTML:
<div class="d-flex flex-column flex-lg-row"> <div class="card p-3 mb-2 mb-lg-0">Card A</div> <div class="card p-3 mb-2 mb-lg-0 order-lg-1">Card B (should appear first on lg)</div> <div class="card p-3 mb-2 mb-lg-0">Card C</div> </div>
Expected: On mobile A → B → C stacked; on lg+ the order visually is B → A → C.
Exercise 2 — Responsive show/hide
Create a toolbar with a text label shown on md+ and only an icon on sm.
Hints:
Use two elements or change display using d-* classes.
Expected: On mobile users see only icons, on desktops they see icon + label.
Exercise 3 — Accessibility check
Create a product list where a promo card is visually first on desktop but appears later in DOM. Test with keyboard only and screen reader (or use browser dev tools accessibility inspector). Confirm logical flow.
Expected: Keyboard navigation follows DOM. If the visual order breaks meaning, adjust DOM order or provide ARIA hints.
Build & test a "Responsive + CTA swap"
Build an area where the CTA button appears below content on mobile. but visually to the left of the copy on the desktop. Verify keyboard flow and responsive behavior.
Files needed: index.html, include Bootstrap CSS (local or CDN), optional small custom CSS file.
Setup HTML
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- link bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.x/dist/css/bootstrap.min.css" rel="stylesheet"> <title>Lab Hero Swap</title> </head> <body> <section class="container py-5"> <div class="d-flex flex-column flex-lg-row align-items-center"> <div class="hero-text order-lg-2 col-lg-8"> <h1>Product Name</h1> <p>Short pitch explaining the key benefit.</p> <a href="#" class="btn btn-primary d-inline d-lg-none">Get started</a> <!-- mobile CTA inside flow --> </div> <div class="hero-cta order-lg-1 col-lg-4 text-center"> <a href="#" class="btn btn-lg btn-success d-none d-lg-inline-block">Get started</a> <!-- desktop CTA --> </div> </div> </section> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.x/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
What this does
On mobile: .flex-column → hero-text first, hero-cta second. CTA in the hero text (d-lg-none) is visible only on mobile.
On desktop: .flex-lg-row changes to horizontal. order-lg-1 makes hero-cta appear left visually while hero-text has order-lg-2. Desktop CTA (d-none d-lg-inline-block) is visible only on lg+.
Testing checklist
Resize viewport — verify visual swap occurs at lg threshold.
Keyboard tabbing — ensure the CTA in DOM appears where intended (since mobile CTA exists in hero-text, keyboard order on mobile feels natural). On desktop the DOM CTA is still present in the DOM order (it may be second in DOM but visually left). Test that tab order still makes sense; if not, consider moving DOM CTA earlier or using skip-to links.
Screen reader check — ensure meaningful link text, aria-label if necessary.
Expected results
Mobile: CTA under copy; desktop: CTA to the left and prominent.
No duplicate accessible controls confusing users (each CTA is visible in only one breakpoint).
Keyboard and screen reader flows should remain predictable.
Exercises
a simple set of test steps for each exercise, e.g.:
Test
Load page on phone width: confirm A-B-C.
Load at 1200px width: confirm B-A-C visually.
Inspect with devtools: .order-lg-1 applied to the middle card.
Test
Tab through elements on mobile and desktop; ensure focus orders are reasonable.
Use browser Accessibility pane or NVDA/VoiceOver for visual vs DOM order check.
Research
“CSS Flexbox order property accessibility implications”
“Responsive design patterns with CSS utilities”
“Progressive enhancement and responsive utilities”
“responsive content”
“Performance cost of duplicate DOM elements for responsive toggles”
“Bootstrap order classes deep dive” (look for official Bootstrap docs and Flexbox spec)
“Mobile-first design and content ordering”
Suggested research directions:
the impact of reordering on cognitive load and screen reader users. Compare DOM-first vs visually-reordered implementations and perform user tests.
Measure page performance when duplicating elements for responsive display vs using single elements with CSS transformations.
Accessibility
Prefer keeping logical content order in the DOM. Only change visual order when it improves usability and doesn’t harm keyboard/screen reader flow.
If you must place a control visually first but keep it later in the DOM, add a skip link or ensure keyboard focus order is still intuitive.
Avoid duplicate interactive elements unless you ensure only one is focusable/visible at a time (use aria-hidden="true" for hidden duplicates where necessary).
Maintainability
Use semantic DOM; keep utilities for layout and presentation, not content semantics.
Keep the number of breakpoint-specific utilities low — prefer a single clear pattern instead of many scattered classes.
Performance
Duplicating large DOM subtrees for show/hide at breakpoints increases DOM size and memory. Use CSS-only when possible.
images use srcset/picture rather than loading both mobile and desktop images.
Debugging
Use browser devtools:
Inspect computed order and display.
Toggle device toolbar to test breakpoints.
Check the Accessibility pane for focus order and hidden elements.
If things don’t reorder:
Ensure parent is display:flex or a Bootstrap row/col using flex.
Ensure you applied a responsive breakpoint suffix correctly (e.g., order-md-1).
Confirm no custom CSS override sets order with !important.
Version differences
Class names and exact patterns differ between major Bootstrap versions (v3 → v4 → v5). Check the docs for your specific version before applying classes—prefer to test with the exact Bootstrap CSS you ship.
Common pitfalls
Relying on order for essential content sequencing (bad for accessibility).
Hiding content with display:none that’s required for keyboard users or screen readers.
Creating two separate interactive components and forgetting to remove one from the accessibility tree.
Create a responsive dashboard header that:
Place the search field above controls on mobile, but left of controls on desktop.
Shows a compact icon-only profile button on sm, and full name + avatar on md+.
Keeps keyboard navigation natural and uses accessible labels.
Deliverable: single HTML file with Bootstrap, a short README documenting choices and accessibility checks.






No comments:
Post a Comment