CSS

Flexbox: Dynamic Sidebar with Fluid Main Content Area

Learn how to create a common web layout using Flexbox, featuring a fixed-width sidebar and a main content area that fluidly expands to fill the remaining space.

.container {
    display: flex;
    min-height: 100vh; /* Ensure container takes full viewport height */
}

.sidebar {
    flex: 0 0 250px; /* Do not grow, do not shrink, fixed basis of 250px */
    background-color: #f0f0f0;
    padding: 20px;
    box-sizing: border-box;
}

.main-content {
    flex: 1; /* Grow to fill available space, can shrink if necessary */
    background-color: #fff;
    padding: 20px;
    box-sizing: border-box;
    overflow-y: auto; /* Allow scrolling if content overflows */
}

/* Basic styling for demonstration */
body { margin: 0; font-family: sans-serif; }
h1 { margin-top: 0; }
/* HTML Structure */
/*
<div class="container">
    <aside class="sidebar">
        <h1>Sidebar</h1>
        <nav>
            <ul>
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
            </ul>
        </nav>
    </aside>
    <main class="main-content">
        <h1>Main Content</h1>
        <p>This is the main content area. It will automatically take up all available space next to the sidebar. If the content overflows, this area will become scrollable.</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        <p>More content to demonstrate scrolling...</p>
        <p>Content block 2...</p><p>Content block 3...</p><p>Content block 4...</p><p>Content block 5...</p>
        <p>Content block 6...</p><p>Content block 7...</p><p>Content block 8...</p><p>Content block 9...</p>
        <p>End of content.</p>
    </main>
</div>
*/
How it works: The `.container` is set to `display: flex` to arrange its children horizontally. The `.sidebar` uses `flex: 0 0 250px;` which means it won't grow (`flex-grow: 0`), won't shrink (`flex-shrink: 0`), and has a `flex-basis` of `250px`. This keeps its width fixed. The `.main-content` uses `flex: 1;` shorthand, which is `flex: 1 1 0%;`. This tells it to `grow` (`flex-grow: 1`) to fill any remaining space, `shrink` if needed (`flex-shrink: 1`), and has an initial `flex-basis` of `0%`. This combination creates a fluid main content area next to a fixed sidebar. `overflow-y: auto;` ensures the main content area scrolls independently if its content exceeds its height.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs