How to stretch div height to fill parent div - CSS
You can stretch a <div>
to match its parent's height using CSS:
The parent <div>
should have a set height:
This way, the child fills the parent, considering the parent's height is defined. Use flexbox or grid for dynamic parent heights.
Flexbox for responsive layout
Flexbox allows the child <div>
to stretch responsively. This is the key to kickass adaptive layouts:
Smoothing out the padding and borders
To take into account padding or borders when stretching your divs, include box-sizing: border-box;
:
This makes the padding and borders join the div's height party.
Avoiding overflow party-crashers
Keep overflow issues from crashing your party:
Or, hide any overflow:
Total screen domination with full-height layouts
Achieving a full-height layout is like claiming your entire screen real estate — height set at 100% for both html
and body
:
Combine this with absolute positioning for complete domination:
For the rebels: dynamic content
For dynamic stretchy content management, use min-height
instead of height
:
Adapt with responsive sizing
Stay responsive with flexbox. Adapts to screen sizes and device orientations like a charm:
Tampering with div dimensions: grid system
The CSS Grid layout lets you tweak and twist div dimensions:
Resilient layouts across browsers
Cross-check your layouts with multiple browsers for compatibility. It's like a health-check for your code.
Was this article helpful?