CSS
Creating a Dynamic Full-Page Layout with CSS Grid and minmax
Design robust full-page layouts using CSS Grid, where headers, footers, and main content adapt fluidly to viewport changes with grid-template-rows and minmax.
html, body {
margin: 0;
padding: 0;
height: 100%;
font-family: Arial, sans-serif;
}
.grid-container {
display: grid;
grid-template-rows: auto 1fr auto; /* Header, Content (fills remaining), Footer */
min-height: 100vh; /* Ensure container fills at least viewport height */
}
.header {
background-color: #333;
color: white;
padding: 20px;
text-align: center;
}
.main-content {
background-color: #f4f4f4;
padding: 20px;
overflow-y: auto; /* Enable scrolling for content if it overflows */
display: flex;
flex-direction: column;
gap: 15px;
}
.main-content h2 {
margin-top: 0;
}
.main-content p {
line-height: 1.6;
}
.footer {
background-color: #666;
color: white;
padding: 15px;
text-align: center;
}
<!-- HTML for context -->
<div class="grid-container">
<header class="header">
<h1>My Dynamic Grid Layout</h1>
</header>
<main class="main-content">
<h2>Welcome to the Main Content Area</h2>
<p>This area is designed to fill the available vertical space. If its content exceeds the viewport height, it will scroll independently. The `1fr` unit in `grid-template-rows` is key here, allowing it to stretch and contract.</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.</p>
<p>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>Consider resizing your browser window to see how the main content area adjusts its height. The header and footer maintain their content-dependent height.</p>
<p>For even more control, you could use `grid-template-rows: minmax(min-content, max-content) 1fr minmax(min-content, max-content);` to ensure header/footer don't shrink below their content, but `auto` often suffices.</p>
<p>More content to ensure scrolling behavior can be observed if the screen is small enough or content is extensive.</p>
<p>End of main content.</p>
</main>
<footer class="footer">
<p>© 2023 Dynamic Grid Layout. All rights reserved.</p>
</footer>
</div>
How it works: This snippet creates a full-page layout using CSS Grid where the header and footer rows take up only the space their content requires (`auto`), while the `.main-content` row is assigned `1fr`. This `1fr` unit ensures the main content area expands to fill all remaining vertical space in the grid container, pushing the footer to the bottom. If the content within `.main-content` overflows its allocated height, `overflow-y: auto` makes it independently scrollable, preventing the entire page from scrolling unnecessarily. `min-height: 100vh` on the container ensures it always fills the viewport.