I have a layout with two columns - a left div
and a right div
.
The right div
has a grey background-color
, and I need it to expand vertically depending on the height of the user's browser window. Right now, the background-color
ends at the last piece of content in that div
.
I've tried height:100%
, min-height:100%;
etc.
You don't mention a few important details like:
Here's one possibility:
There are many variations on this depending on which columns need to be fixed and which are liquid. You can do this with absolute positioning too but I've generally found better results (particularly in terms of cross-browser) using floats instead.
You can use
vh
in this case which is relative to 1% of the height of the viewport...That means if you want to cover off the height, just simply use
100vh
.Look at the image I draw for you here:
Try the snippet I created for you as below:
Easiest:
Try to set
height:100%
inhtml
&body
And if you want to 2 div height same use or set the parent element
display:flex
property.css
There are a couple of CSS3 measurement units called:
Viewport-Percentage (or Viewport-Relative) Lengths
What are Viewport-Percentage Lengths?
From the linked W3 Candidate Recommendation above:
These units are
vh
(viewport height),vw
(viewport width),vmin
(viewport minimum length) andvmax
(viewport maximum length).How can this be used to make a divider fill the height of the browser?
For this question, we can make use of
vh
:1vh
is equal to 1% of the viewport's height. That is to say,100vh
is equal to the height of the browser window, regardless of where the element is situated in the DOM tree:HTML
CSS
This is literally all that's needed. Here is a JSFiddle example of this in use.
What browsers support these new units?
This is currently supported on all up-to-date major browsers apart from Opera Mini. Check out Can I use... for further support.
How can this be used with multiple columns?
In the case of the question at hand, featuring a left and a right divider, here is a JSFiddle example showing a two-column layout involving both
vh
andvw
.How is
100vh
different to100%
?Take this layout for example:
The
p
tag here is set to 100% height, but because its containingdiv
has 200px height, 100% of 200px becomes 200px, not 100% of thebody
height. Using100vh
instead means that thep
tag will be 100% height of thebody
regardless of thediv
height. Take a look at this accompanying JSFiddle to easily see the difference!