A sidebar with a content area helps organize a webpage into two parts: a menu or navigation on the side and the main content next to it. This makes it easy for users to find and read information.
Sidebar with content area in Bootsrap
<div class="d-flex"> <nav class="sidebar bg-light p-3"> <!-- Sidebar content here --> </nav> <main class="content flex-grow-1 p-3"> <!-- Main content here --> </main> </div>
Use d-flex on a container to create a flexible horizontal layout.
The sidebar uses fixed width or padding, and the content area uses flex-grow-1 to fill remaining space.
<div class="d-flex"> <nav class="sidebar bg-light p-3" style="width: 250px;"> <ul class="nav flex-column"> <li class="nav-item"><a href="#" class="nav-link">Home</a></li> <li class="nav-item"><a href="#" class="nav-link">Profile</a></li> <li class="nav-item"><a href="#" class="nav-link">Settings</a></li> </ul> </nav> <main class="content flex-grow-1 p-3"> <h1>Welcome</h1> <p>This is the main content area.</p> </main> </div>
flex-column flex-md-row to stack sidebar on small screens and place side by side on medium and larger screens.<div class="d-flex flex-column flex-md-row"> <nav class="sidebar bg-light p-3" style="width: 200px;"> <p><strong>Menu</strong></p> <ul class="nav flex-column"> <li class="nav-item"><a href="#" class="nav-link">Dashboard</a></li> <li class="nav-item"><a href="#" class="nav-link">Reports</a></li> </ul> </nav> <main class="content flex-grow-1 p-3"> <h2>Dashboard Content</h2> <p>Details and charts go here.</p> </main> </div>
This complete example creates a sidebar on the left with a vertical menu and a main content area on the right. The sidebar stays fixed width and full height. The main content area fills the rest of the screen. The layout uses Bootstrap's flex utilities for a clean, responsive design.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Sidebar with Content Area</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> <style> .sidebar { width: 220px; min-height: 100vh; } </style> </head> <body> <div class="d-flex"> <nav class="sidebar bg-light p-3"> <h5>Menu</h5> <ul class="nav flex-column" role="navigation" aria-label="Main menu"> <li class="nav-item"><a href="#home" class="nav-link">Home</a></li> <li class="nav-item"><a href="#services" class="nav-link">Services</a></li> <li class="nav-item"><a href="#about" class="nav-link">About</a></li> <li class="nav-item"><a href="#contact" class="nav-link">Contact</a></li> </ul> </nav> <main class="content flex-grow-1 p-4" tabindex="0"> <h1 id="home">Welcome to Our Website</h1> <p>This is the main content area next to the sidebar menu.</p> <section id="services"> <h2>Services</h2> <p>We offer web design, development, and consulting.</p> </section> <section id="about"> <h2>About Us</h2> <p>Learn more about our team and mission.</p> </section> <section id="contact"> <h2>Contact</h2> <p>Get in touch via email or phone.</p> </section> </main> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Use semantic HTML elements like <nav> for the sidebar and <main> for the content to improve accessibility.
Adding tabindex="0" to the main content allows keyboard users to focus on it easily.
Use responsive classes like flex-column flex-md-row if you want the sidebar to stack on small screens.
A sidebar with content area divides the page into navigation and main content sections.
Bootstrap's flex utilities make it easy to create side-by-side layouts.
Use semantic tags and accessibility features for better user experience.
Practice
Solution
Step 1: Understand sidebar role
A sidebar is typically used to hold navigation or extra information separate from main content.Step 2: Identify layout purpose
Separating navigation from content helps users find links easily and keeps content clear.Final Answer:
To separate navigation links from the main content for better organization -> Option BQuick Check:
Sidebar = Navigation separation [OK]
- Thinking sidebar is only for decoration
- Believing sidebar speeds up page load
- Assuming sidebar hides content on small screens
Solution
Step 1: Identify flex container class
Bootstrap usesd-flexto enable flexbox layout on a container.Step 2: Choose direction for sidebar and content
To place sidebar and content side by side, useflex-rowfor horizontal layout.Final Answer:
class='d-flex flex-row' on a container with sidebar and content divs -> Option AQuick Check:
Flex row = side by side layout [OK]
- Using flex-column which stacks vertically
- Using d-block which disables flexbox
- Using d-inline which is not a flex container
<div class='d-flex'> <nav class='bg-light p-3' style='width: 200px;'>Sidebar</nav> <main class='flex-grow-1 p-3'>Content area</main> </div>
What will be the visual layout in the browser?
Solution
Step 1: Analyze flex container behavior
Thed-flexclass creates a flex container with default row direction, placing children side by side.Step 2: Understand child sizing
Thenavhas fixed width 200px, andmainusesflex-grow-1to fill remaining space.Final Answer:
Sidebar on the left with fixed width, content area fills remaining space on the right -> Option DQuick Check:
Fixed width sidebar + flex-grow content = side by side [OK]
- Assuming vertical stacking without flex-row
- Thinking sidebar disappears without display:none
- Believing elements overlap without position styles
<div class='d-flex flex-column'> <nav class='sidebar'>Menu</nav> <main class='content flex-grow-1'>Main content</main> </div>
Why does the sidebar appear above the content instead of side by side?
Solution
Step 1: Check flex direction
The container usesflex-column, which stacks children vertically.Step 2: Understand layout effect
Vertical stacking places sidebar above content, not side by side.Final Answer:
Because flex-column stacks items vertically, not horizontally -> Option AQuick Check:
flex-column = vertical stack [OK]
- Confusing flex-grow with layout direction
- Forgetting d-flex disables flexbox
- Assuming custom class affects flex direction
Solution
Step 1: Set desktop layout with flex
Usingd-flex flex-rowplaces sidebar and content side by side on larger screens.Step 2: Add responsive toggle with offcanvas
Bootstrap'soffcanvascomponent provides a sidebar that can toggle visibility on small screens.Final Answer:
Use d-flex flex-row for desktop layout and Bootstrap's offcanvas component for small screens -> Option CQuick Check:
Flex row + offcanvas = responsive sidebar [OK]
- Using flex-column which stacks vertically always
- Hiding sidebar with CSS only loses accessibility
- Removing sidebar element breaks navigation
Ready to prove it?
Pass the test and this pattern moves unknown
