Explain Codes LogoExplain Codes Logo

100% Min Height CSS Layout

html
responsive-design
css-layout
best-practices
Anton ShumikhinbyAnton Shumikhin·Oct 31, 2024
TLDR

Apply min-height: 100vh; to your container to expand itself to fit the full viewport height regardless of its content’s size.

html, body { height: 100%; margin: 0; padding: 0; } .container { min-height: 100vh; /* Cover the entire viewport */ position: relative; /* Stage for our footer rockstar */ padding-bottom: 50px; /* Making room for the footer */ background: #f8f8f8; /* Aesthetic splash */ margin: 0 auto; /* VIP seat at the center */ width: 100%; /* Spreading arms wide open */ } .footer { position: absolute; /* Glued at the bottom like it has commitment issues */ bottom: 0; height: 50px; /* Unchanging like an old friend */ width: 100%; /* United width the entire page */ }
<div class="container"> <div class="content"> <!-- Your content here. Don't be shy! --> </div> <div class="footer"> <!-- Your footer is at your feet --> </div> </div>

Immovable headers and footers

Have a immoveable header or footer? Subtract their heights from the min-height using CSS’s calc() function to ensure They don’t spill over the screen.

.content { min-height: calc(100vh - (80px + 70px)); /* header height + footer height */ }

Adaptive content sizing

When working with content whose size can't be predetermined, opt for the calc() function to dynamically substitute the min-height whilst accommodating fixed headers and footers:

.content { min-height: calc(100vh - (height_of_header + height_of_footer)); }

Overflow management

Handle overflow effectively with timely scrolls to ensure user experience isn't compromised:

.content { min-height: calc(100vh - (height_of_header + height_of_footer)); overflow-y: auto; /* The scroll appears when the flood comes in */ }

Cross-browser compatibility

Anticipate the unpredictable: verify your layout's adaptability by testing with varying text sizes and window dimensions:

.container { min-height: 100vh; -webkit-min-height: 100vh; /* Old WebKit browsers still on your invite list */ -moz-min-height: 100vh; /* Mozilla fans, check! */ -ms-min-height: 100vh; /* For the die-hard Microsoft users */ box-sizing: border-box; /* Fuss-free height calculation */ }

Smart practices

Parent-child height inheritance

To allow child elements to inherit parent height, demand a minimum out of the parent and let the percentage do the talking:

.parent { min-height: 1px; /* Minimal ask for full inheritance rights */ } .child { height: 100%; /* Full height inheritance */ }

Make your footer stick to the bottom of the page, regardless of content size, by using flex-grow:

body { display: flex; flex-direction: column; } .content { flex: 1; /* Making room at the bottom or stretching up */ }

Visual demos

Refer to practical visual demos to understand fluid CSS layout implementations.