A sidebar helps organize your page by putting navigation or extra info on the side. It keeps the main content clear and easy to read.
Sidebar with main content in Tailwind
<div class="flex"> <aside class="w-64 bg-gray-200 p-4">Sidebar content</aside> <main class="flex-1 p-4">Main content</main> </div>
flex creates a horizontal layout.
w-64 sets the sidebar width, flex-1 makes main content fill the rest.
<div class="flex"> <aside class="w-48 bg-blue-100 p-3">Menu</aside> <main class="flex-1 p-6">Content area</main> </div>
<div class="flex flex-col md:flex-row"> <aside class="w-full md:w-56 bg-gray-300 p-4">Sidebar</aside> <main class="flex-1 p-4">Main content</main> </div>
This page has a sidebar with navigation links on the left and main content on the right. The layout uses Tailwind's flex utilities for a clean side-by-side look. The sidebar has a fixed width, and the main content fills the rest of the screen. The main area is keyboard focusable for accessibility.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Sidebar with Main Content</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="min-h-screen bg-white text-gray-900"> <div class="flex min-h-screen"> <aside class="w-64 bg-gray-100 p-6" aria-label="Sidebar"> <nav> <ul class="space-y-4"> <li><a href="#" class="block text-blue-600 hover:underline">Home</a></li> <li><a href="#" class="block text-gray-700 hover:text-blue-600">About</a></li> <li><a href="#" class="block text-gray-700 hover:text-blue-600">Services</a></li> <li><a href="#" class="block text-gray-700 hover:text-blue-600">Contact</a></li> </ul> </nav> </aside> <main class="flex-1 p-8" tabindex="0"> <h1 class="text-3xl font-bold mb-4">Welcome to the Main Content</h1> <p>This area shows the main information. The sidebar on the left helps you navigate.</p> </main> </div> </body> </html>
Use aria-label on the sidebar for screen readers.
Make main content focusable with tabindex="0" for keyboard users.
Use responsive classes like md:flex-row to adjust layout on different screen sizes.
A sidebar organizes navigation or extra info beside main content.
Use Tailwind's flex and width utilities to create side-by-side layouts.
Always consider accessibility and responsive design for a better user experience.
Practice
Solution
Step 1: Understand sidebar role
A sidebar is typically used to hold navigation links or extra info next to main content.Step 2: Compare options
Only To organize navigation or extra information beside the main content correctly describes this purpose; others are incorrect uses of sidebars.Final Answer:
To organize navigation or extra information beside the main content -> Option CQuick Check:
Sidebar purpose = organize navigation/info [OK]
- Thinking sidebar replaces main content
- Using sidebar only for decoration
- Assuming sidebar hides content always
Solution
Step 1: Identify layout direction classes
Tailwind'sflexwithflex-rowarranges children horizontally side by side.Step 2: Evaluate other options
block flex-colstacks vertically,grid-cols-1creates one column, andinline-blockdoes not create a flex layout.Final Answer:
<code>flex flex-row</code> -> Option DQuick Check:
Horizontal layout = flex flex-row [OK]
- Using flex-col for horizontal layout
- Using grid-cols-1 for two columns
- Using inline-block for layout
<div class="flex"> <aside class="w-1/4 bg-gray-200">Sidebar</aside> <main class="w-3/4 p-4">Main Content</main> </div>
Solution
Step 1: Understand width classes
w-1/4means the element takes one quarter (25%) of the container width.Step 2: Confirm layout with flex
Since the parent usesflex, the sidebar and main content sit side by side with widths 25% and 75% respectively.Final Answer:
25% of the container width -> Option AQuick Check:
w-1/4 = 25% width [OK]
- Confusing w-1/4 with 1/2 or 75%
- Ignoring flex layout effect
- Assuming fixed pixel width
<div class="flex flex-col"> <aside class="w-1/4 bg-blue-100">Sidebar</aside> <main class="w-3/4 p-4">Main Content</main> </div>
Solution
Step 1: Analyze flex direction
flex-colstacks children vertically, so sidebar and main appear one above the other.Step 2: Check width classes and layout goal
Widthsw-1/4andw-3/4are valid but ignored in vertical stacking; to place side by side,flex-rowis needed.Final Answer:
Using flex-col stacks sidebar and main vertically, not side by side -> Option BQuick Check:
flex-col stacks vertically, not side by side [OK]
- Confusing flex-col with flex-row
- Thinking widths apply in vertical flex
- Ignoring layout direction effect
<div class="flex"> <aside class="??? bg-gray-300">Sidebar</aside> <main class="??? p-4">Main Content</main> </div>
Solution
Step 1: Hide sidebar on small, show on medium+
Usehidden md:blockon aside to hide it on small screens and show from medium screens up.Step 2: Set widths for responsive layout
Sidebar getsw-1/4on medium+ screens; main usesw-fullon small andmd:w-3/4on medium+ to fill remaining space.Final Answer:
hidden md:block w-1/4 for aside and w-full md:w-3/4 for main -> Option AQuick Check:
Responsive show/hide + widths =hidden md:block w-1/4for aside andw-full md:w-3/4for main [OK]
- Not hiding sidebar on small screens
- Using md:hidden instead of hidden md:block
- Not adjusting main content width responsively
Ready to prove it?
Pass the test and this pattern moves unknown
