Question
I have a two-column layout with a left <div> and a right <div>.
The right <div> has a gray background color, and I want it to stretch vertically to match the height of the user's browser window. At the moment, the background color only extends as far as the content inside that <div>.
I tried using the following CSS, but it did not work as expected:
height: 100%;
min-height: 100%;
How can I make the right <div> fill the full height of the browser window?
Short Answer
By the end of this page, you will understand why height: 100% often does not work in CSS layouts, how percentage heights depend on parent elements, and how to use viewport units like 100vh to make an element fill the browser window height reliably.
Concept
In CSS, making an element fill the full browser height sounds simple, but there is an important rule behind it: percentage heights only work if the parent has an explicit height.
For example, if you write:
.right {
height: 100%;
}
that 100% does not mean "100% of the browser window" by itself. It means "100% of the height of my parent element." If the parent does not have a fixed or defined height, the browser has nothing concrete to measure against.
That is why height: 100% and min-height: 100% often appear to do nothing.
A more direct solution is to use viewport units:
min-height: 100vh;
Here, vh stands for viewport height.
100vh= 100% of the visible browser window height50vh= half the browser window height
This is useful when you want a section, column, sidebar, hero area, or full-page panel to fill the screen.
This concept matters because full-height layouts are common in:
- dashboard sidebars
- landing pages
Mental Model
Think of percentage height like saying:
"Make this box as tall as its container."
That only works if the container already knows its own height.
If the container says:
"I don't know my height yet; I'm just as tall as my content."
then the child cannot calculate 100%.
Viewport height is different. It is like saying:
"Ignore the parent. Match the browser window itself."
So:
height: 100%= match the parent's heightheight: 100vh= match the screen height
That is why 100vh is usually the simpler answer when you want full browser height.
Syntax and Examples
Core syntax
Option 1: Use viewport height
.right {
background: #ddd;
min-height: 100vh;
}
This makes the element at least as tall as the browser window.
Option 2: Use percentage height with parent heights defined
html, body {
height: 100%;
}
.container {
height: 100%;
}
.right {
height: 100%;
background: #ddd;
}
This works only if every relevant parent element also has a defined height.
Beginner-friendly example
<div class="layout">
<div class="left">Menu</div>
<div class="right">Content area
Step by Step Execution
Consider this example:
<div class="right">Hello</div>
.right {
background: lightgray;
min-height: 100vh;
}
Here is what happens step by step:
- The browser creates a
<div>with the textHello. - The
background: lightgray;rule paints the element's background. min-height: 100vh;tells the browser:- make this element at least as tall as the viewport
- if content becomes taller, allow it to expand
- If the browser window is 900 pixels tall,
100vhbecomes 900 pixels. - The gray background now covers the full visible height.
- If you add more content later and the content needs 1200 pixels, the div grows beyond 900 pixels because
min-heightsets a minimum, not a maximum.
Now compare with this:
.right {
background: lightgray;
: ;
}
Real World Use Cases
Common uses for full-height elements
Sidebars in dashboards
Admin panels often have a navigation sidebar that should stretch from the top to the bottom of the screen.
Split-screen layouts
A left navigation panel and right content panel often need full browser height.
Hero sections
Landing pages commonly use a section that fills the first screen.
Login and signup pages
Many authentication screens center a form inside a full-height container.
Mobile app-style web layouts
Web apps often need panels that occupy the visible screen height.
Example
.sidebar {
width: 240px;
min-height: 100vh;
background: #2d3748;
}
This ensures the sidebar remains full height even when it has only a small amount of content.
Real Codebase Usage
In real projects, developers usually combine full-height elements with modern layout tools such as Flexbox or Grid.
Common patterns
Use min-height instead of height
.main-panel {
min-height: 100vh;
}
This is often better because content can still grow naturally.
Use Flexbox for two-column layouts
.layout {
display: flex;
}
.sidebar {
width: 250px;
}
.content {
flex: 1;
min-height: 100vh;
}
Set root heights when using percentage layouts
html, body {
height: 100%;
}
This is common in older codebases or layouts built around percentage heights.
Guard against default spacing
Browsers add default body margin, which can make a full-height layout look slightly wrong.
Common Mistakes
1. Using height: 100% without defining parent heights
Broken example:
.right {
height: 100%;
}
Why it fails:
100%depends on the parent's height- if the parent height is
auto, the browser cannot resolve it
Fix:
html, body {
height: 100%;
}
or use:
.right {
min-height: 100vh;
}
2. Using height when min-height is better
Broken approach:
.right {
height: 100vh;
}
Possible issue:
- if content becomes taller than the screen, it may overflow awkwardly
Comparisons
| Approach | What it means | Works when | Best use case |
|---|---|---|---|
height: 100% | Match parent height | Parent has explicit height | Nested layouts with defined parent heights |
min-height: 100% | At least parent height | Parent has explicit height | Percentage-based layouts |
height: 100vh | Exactly viewport height | Always based on viewport | Full-screen sections with fixed height |
min-height: 100vh | At least viewport height | Always based on viewport | Full-height sections that may grow with content |
height vs
Cheat Sheet
/* Most common full-browser-height solution */
.element {
min-height: 100vh;
}
/* Percentage height requires parent heights */
html, body {
height: 100%;
}
.parent {
height: 100%;
}
.child {
height: 100%;
}
Quick rules
100%means 100% of the parent height%heights need explicit parent heights100vhmeans 100% of the browser viewport heightmin-height: 100vhis usually better thanheight: 100vhbody { margin: 0; }often helps full-screen layouts look correct
Good default for a full-height column
body {
margin: 0;
}
.right {
: ;
: ;
}
FAQ
Why does height: 100% not work on my div?
Because it depends on the parent's height. If the parent does not have an explicit height, the browser cannot calculate 100%.
What does 100vh mean in CSS?
It means 100% of the viewport height, which is the visible browser window height.
Should I use height: 100vh or min-height: 100vh?
Usually min-height: 100vh is better because the element can still grow if its content becomes taller than the screen.
Do I need to set html, body { height: 100%; }?
Only if you are using percentage heights like height: 100%. It is not required for 100vh.
Why is there still space around my full-height div?
The browser may be applying a default margin to the body. Try body { margin: 0; }.
Can Flexbox help with full-height columns?
Yes. Flexbox is a common way to create side-by-side columns, and it works well with min-height: 100vh.
Does background color automatically fill the screen?
No. The background only fills the element's actual size. You must make the element itself tall enough.
Mini Project
Description
Build a simple two-column page layout where the right content panel always fills at least the full height of the browser window. This demonstrates how viewport height and Flexbox work together in a practical layout.
Goal
Create a two-column layout with a left sidebar and a right content area whose background fills the full browser height.
Requirements
- Create a left sidebar and a right content panel
- Place both columns side by side
- Make the right panel fill at least the full browser height
- Add a background color to the right panel
- Remove default page spacing so the layout touches the browser edges
Keep learning
Related questions
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.
CSS Parent Selector Explained: Selecting a Parent <li> from an <a>
Learn whether CSS has a parent selector, how :has() works, and practical alternatives for styling a parent li from a child anchor.
CSS Previous Sibling Selector: What Exists and How to Work Around It
Learn whether CSS has a previous sibling selector, why it does not, and practical ways to style earlier elements using CSS alternatives.