How to make the main content div fill height of screen with css
To make your content div stretch to the full height of the screen, just slap on height: 100vh;
to it:
Note that vh
stands for Viewport Height. 100vh
is equal to 100% of the current viewport height. However, this straightforward method may get tangled up when you add a header and footer. Read on for more complex cases!
Layout with header and footer
When you're working with header
and footer
sections, it's a different ball game. Here are two methods to ensure your main content div adjusts correctly:
The Flexbox way
CSS Flexbox comes to the rescue:
This grows the content dynamically between the header and footer sections.
The CSS Grid method
CSS Grid is also a worthy opponent in this battle:
The main content is set to grow and push the footer to the bottom, making the footer beg for mercy (forgive the dramatics, but you get the idea).
For the love of footers and dynamic content
Making footers behave
Absolute positioning or flexbox can be used to pin down those footers.
Or by using absolute positioning:
Managing overflowing content
Ensure your content doesn't get too excited and start causing scrollbars due to padding:
In case your contents decide to go beyond the viewport, consider using overflow: auto;
to keep them in check:
Making friends with all browsers
Viewport units in older browsers
Older browsers may get a bit grumpy and not support vh
. In these cases, use:
Flexbox and grid fallbacks
For browsers, who are still living in the past and do not support Flexbox or Grid:
This is like doing a rain dance to make Flexbox and Grid work using older CSS properties.
Conversation with multiple devices
Responsive design
Make your content div chameleon-like, adapting to its surroundings. How? Media queries and relative units:
Not-so-friendly UI elements
Mobile browsers, being the special snowflakes that they are, may have UI elements that obscure your content. Fight back with calc()
:
Was this article helpful?