<div class="d-flex"> <nav class="flex-shrink-0 p-3 bg-light" style="width: 250px;" aria-label="Sidebar"> <ul class="nav nav-pills flex-column"> <li class="nav-item"><a href="#" class="nav-link active" aria-current="page">Home</a></li> <li class="nav-item"><a href="#" class="nav-link">Profile</a></li> <li class="nav-item"><a href="#" class="nav-link">Messages</a></li> </ul> </nav> <main class="flex-grow-1 p-3"> <h1>Welcome</h1> <p>This is the main content area.</p> </main> </div>
The d-flex class creates a flex container with horizontal layout by default. The sidebar uses flex-shrink-0 and a fixed width of 250px, so it stays on the left. The main content uses flex-grow-1 to fill the remaining space on the right. This results in a vertical sidebar on the left and content area on the right.
<nav class="flex-shrink-0 p-3 bg-light" style="width: 250px;" aria-label="Sidebar"> <ul class="nav nav-pills flex-column"> <li class="nav-item"><a href="#" class="nav-link active">Home</a></li> <li class="nav-item"><a href="#" class="nav-link">Profile</a></li> <li class="nav-item"><a href="#" class="nav-link">Messages</a></li> </ul> </nav>
The aria-label attribute gives a descriptive name to the nav landmark, helping screen readers announce it as a sidebar navigation region. This improves accessibility by clarifying the purpose of the navigation area.
nav-link and the active link also has active, which selector matches only the active links?The selector .nav-link.active matches elements that have both nav-link and active classes. Option C looks for a child with class active inside nav-link, which is incorrect. Option C looks for nav-link inside an active element, also incorrect. Option C is invalid because it lacks a dot before nav-link.
flex-shrink-0 from the sidebar?<div class="d-flex"> <nav class="p-3 bg-light" style="width: 250px;" aria-label="Sidebar"> <ul class="nav nav-pills flex-column"> <li class="nav-item"><a href="#" class="nav-link active">Home</a></li> <li class="nav-item"><a href="#" class="nav-link">Profile</a></li> <li class="nav-item"><a href="#" class="nav-link">Messages</a></li> </ul> </nav> <main class="flex-grow-1 p-3"> <h1>Welcome</h1> <p>This is the main content area.</p> </main> </div>
flex-shrink-0 class prevents shrinking in flexbox.Removing flex-shrink-0 allows the sidebar to shrink when the container is too small. This means the sidebar width can become less than 250px, making it narrower. With flex-shrink-0, the sidebar keeps its width and the main content shrinks instead.
Bootstrap 5's collapse component uses data-bs-toggle="collapse" on a button to toggle visibility of a target element with class collapse. Adding/removing the show class controls the visibility with smooth animation. This is the recommended way to create a collapsible sidebar.
Options B, C, and D either lack Bootstrap integration or rely on less accessible or less flexible methods.
