How to Rearrange Layouts for Mobile and Desktop Using Bootstrap 5 Responsive Utilities
Creating a navbar with a centered logo flanked by navigation links on desktop is a classic, visually balanced design choice. However, once that layout collapses on mobile devices, things can easily fall apart—often leaving the brand logo awkwardly trapped in the center or below a collapsed menu toggle button.
If you are using Bootstrap 5.3, changing your layout depending on screen size doesn't require clunky JavaScript or duplicate HTML blocks. You can achieve seamless responsive layouts using Bootstrap Flexbox Order Utilities and CSS Media Queries. Let's look at why this happens and how to fix it cleanly.
The Core Problems in the Layout
Before applying the fix, there are two primary issues in the original markup:
- Duplicate IDs: Both collapsible
<div>wrappers share the identicalid="navbarCollapse". HTML IDs must be globally unique on the page. Having duplicates breaks accessibility and causes erratic collapse behavior with Bootstrap JavaScript. - Source Order & Visual Flow: By default, HTML renders elements in DOM source order. On small screens, the hamburger toggler renders first, followed by the left menu, then the logo, and finally the right menu.
Solution 1: Bootstrap 5 Responsive Flex Order Classes (Recommended)
The easiest, most idiomatic way to alter visual layouts across breakpoints in Bootstrap 5 is with responsive ordering classes like order-{breakpoint}-{number} (e.g., order-first, order-sm-2).
Because the container element (<div class="container-fluid">) is already a flex container, we can tell Bootstrap:
- On mobile (default): Render the logo first (
order-0), toggler second (order-1), and collapsible links last (order-2). - On desktop (
smor larger): Reorder them naturally so the left links sit on the left, the brand sits in the middle, and the right links sit on the right.
Corrected HTML Markup
<nav class="navbar navbar-expand-sm">
<div class="container-fluid">
<!-- 1. Brand Logo: First on mobile (order-0), center on desktop (order-sm-2) -->
<a class="navbar-brand order-0 order-sm-2 mx-auto mx-sm-0" href="#">
<img src="/img/flamingtext_com-236403447.gif" alt="Brand Logo">
</a>
<!-- 2. Mobile Toggler: Second on mobile (order-1), hidden on desktop -->
<button class="navbar-toggler order-1" type="button" data-bs-toggle="collapse" data-bs-target="#mainNavbarCollapse" aria-controls="mainNavbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- 3. Collapsible Menu Wrapper: Third on mobile (order-2) -->
<div class="collapse navbar-collapse order-2 order-sm-1" id="mainNavbarCollapse">
<!-- Left Navigation Links -->
<ul class="navbar-nav me-auto mb-2 mb-sm-0">
<li class="nav-item fs-1 mx-2">
<a class="nav-link" href="#">Link 1</a>
</li>
<li class="nav-item fs-1 mx-2">
<a class="nav-link" href="#">Link 2</a>
</li>
</ul>
<!-- Right Navigation Links (can live in the same collapse wrapper) -->
<ul class="navbar-nav ms-auto mb-2 mb-sm-0 order-sm-3">
<li class="nav-item fs-1 mx-2">
<a class="nav-link" href="#">Link 3</a>
</li>
<li class="nav-item fs-1 mx-2">
<a class="nav-link" href="#">Link 4</a>
</li>
</ul>
</div>
</div>
</nav>Solution 2: Using Custom CSS Media Queries
If you prefer fine-grained control or want to adjust custom properties that utility classes don't cover, CSS media queries are the standard method for adapting layouts across different screen widths.
Bootstrap 5.3 uses standard breakpoints:
- xs: < 576px (Default / Mobile)
- sm: ≥ 576px
- md: ≥ 768px
- lg: ≥ 992px
- xl: ≥ 1200px
You can target mobile devices specifically using a max-width query:
/* Styles specifically for mobile devices below Bootstrap's sm breakpoint */
@media (max-width: 575.98px) {
.navbar > .container-fluid {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
}
.navbar-brand {
order: 1;
margin: 0 auto 10px auto; /* Centers brand on mobile and pushes it to top */
width: 100%;
text-align: center;
}
.navbar-toggler {
order: 2;
}
.navbar-collapse {
order: 3;
width: 100%;
}
}Key Takeaways for Clean Responsive Layouts
- Consolidate Collapsible Containers: Avoid splitting collapsed menus into multiple separate wrappers unless you explicitly want multiple dropdown triggers. Keep all links within a single collapse container so mobile users see one uniform menu.
- Leverage Bootstrap Grid & Flex Utilities: Use
d-none d-md-blockto toggle visibility, andorder-*classes to shuffle element positions without altering your HTML structure. - Always Check Viewport Meta Tag: Ensure your HTML
<head>contains the essential responsive viewport tag, without which mobile browsers will simulate desktop resolutions:
<meta name="viewport" content="width=device-width, initial-scale=1">