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
How to make the main content div fill height of screen with CSS and HTML
Making the main content div fill the height of the screen is a common layout requirement in web development. This can be achieved using CSS positioning properties or modern layout techniques like flexbox and CSS Grid.
Syntax
.main-content {
height: 100vh;
/* or */
position: absolute;
top: header-height;
bottom: footer-height;
/* or */
flex: 1;
}
Method 1: Using Viewport Height (vh)
The simplest approach is to use viewport height units where 100vh equals 100% of the viewport height −
<!DOCTYPE html>
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
height: 100vh;
background-color: #f0f0f0;
display: flex;
align-items: center;
justify-content: center;
font-family: Arial, sans-serif;
}
.main-content {
background-color: #4CAF50;
color: white;
padding: 20px;
border-radius: 8px;
}
</style>
</head>
<body>
<div class="container">
<div class="main-content">
Main content filling the screen height
</div>
</div>
</body>
</html>
A full-height container with a centered green box containing white text fills the entire viewport height.
Method 2: Using Absolute Positioning
You can use absolute positioning to fill the space between header and footer −
<!DOCTYPE html>
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 60px;
background-color: #333;
color: white;
display: flex;
align-items: center;
padding: 0 20px;
}
.main-content {
position: absolute;
top: 60px;
bottom: 40px;
left: 0;
right: 0;
background-color: #e8f5e8;
padding: 20px;
overflow-y: auto;
}
.footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 40px;
background-color: #666;
color: white;
display: flex;
align-items: center;
padding: 0 20px;
}
</style>
</head>
<body>
<div class="header">Header (60px height)</div>
<div class="main-content">
<h2>Main Content Area</h2>
<p>This div fills the remaining space between header and footer.</p>
</div>
<div class="footer">Footer (40px height)</div>
</body>
</html>
A layout with a dark header at top (60px), light green main content area filling the middle space, and a gray footer at bottom (40px).
Method 3: Using Flexbox
Flexbox provides a clean solution for creating full-height layouts −
<!DOCTYPE html>
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
height: 100%;
}
.container {
height: 100vh;
display: flex;
flex-direction: column;
}
.header {
background-color: #2196F3;
color: white;
padding: 15px;
text-align: center;
}
.main-content {
flex: 1;
background-color: #fff3cd;
padding: 20px;
display: flex;
align-items: center;
justify-content: center;
}
.footer {
background-color: #28a745;
color: white;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<header class="header">Flexbox Header</header>
<main class="main-content">
<div>Main content area that fills remaining space</div>
</main>
<footer class="footer">Flexbox Footer</footer>
</div>
</body>
</html>
A flexbox layout with blue header, yellow main content area that fills the remaining space, and green footer.
Conclusion
Use viewport height (100vh) for simple full-height containers, absolute positioning for fixed header/footer layouts, or flexbox for more flexible responsive designs. Flexbox is generally the most modern and maintainable approach.
Advertisements
