Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Creating a Full Height Page with Fixed Sidebar and Scrollable Content Area in CSS
A full-height page with a fixed sidebar and scrollable content area is a common layout pattern in web design. This layout uses CSS positioning to create a sidebar that remains fixed while the main content area can scroll independently.
Syntax
/* Fixed Sidebar */
.sidebar {
position: fixed;
height: 100%;
width: sidebar-width;
}
/* Main Content */
.main {
margin-left: sidebar-width;
height: 100vh;
overflow-y: auto;
}
Example
The following example creates a fixed sidebar with navigation links and a scrollable content area −
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
}
.sidebar {
height: 100vh;
width: 200px;
position: fixed;
top: 0;
left: 0;
background-color: #2c3e50;
padding-top: 20px;
box-shadow: 2px 0 5px rgba(0,0,0,0.1);
}
.sidebar a {
display: block;
padding: 15px 20px;
text-decoration: none;
color: #ecf0f1;
font-size: 16px;
border-bottom: 1px solid #34495e;
transition: all 0.3s ease;
}
.sidebar a:hover {
background-color: #3498db;
padding-left: 30px;
}
.main {
margin-left: 200px;
padding: 20px;
height: 100vh;
overflow-y: auto;
background-color: #f8f9fa;
}
.content-section {
margin-bottom: 40px;
padding: 20px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.tall-content {
height: 800px;
background: linear-gradient(45deg, #e74c3c, #f39c12);
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
}
</style>
</head>
<body>
<div class="sidebar">
<a href="#">Dashboard</a>
<a href="#">Profile</a>
<a href="#">Settings</a>
<a href="#">Reports</a>
<a href="#">Help</a>
</div>
<div class="main">
<h1>Main Content Area</h1>
<div class="content-section">
<h2>Section 1</h2>
<p>This is the main content area that can scroll independently of the fixed sidebar. The sidebar remains visible at all times while this content can be scrolled.</p>
</div>
<div class="content-section">
<h2>Section 2</h2>
<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.</p>
</div>
<div class="tall-content">
Scroll to see the fixed sidebar effect
</div>
<div class="content-section">
<h2>Bottom Section</h2>
<p>This content demonstrates that the sidebar stays fixed while the main content scrolls. The layout maintains its structure across different screen heights.</p>
</div>
</div>
</body>
</html>
Output
A webpage with a dark blue fixed sidebar (200px wide) on the left containing navigation links that highlight on hover. The main content area on the right has a light background with white content cards and can scroll independently while the sidebar remains fixed in position.
Key Points
- position: fixed − Keeps the sidebar fixed relative to the viewport
- height: 100vh − Makes the sidebar full viewport height
- margin-left − Pushes the main content to the right of the sidebar
- overflow-y: auto − Enables vertical scrolling for the main content
Conclusion
This layout pattern is perfect for dashboard interfaces and navigation-heavy websites. The fixed sidebar provides constant access to navigation while allowing the main content to scroll freely, creating an efficient user experience.
Advertisements
