2647

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mike
  • 26,535
  • 3
  • 16
  • 4

42 Answers42

3282

There are a couple of CSS 3 measurement units called:

Viewport-Percentage (or Viewport-Relative) Lengths

What are Viewport-Percentage Lengths?

From the linked W3 Candidate Recommendation above:

The viewport-percentage lengths are relative to the size of the initial containing block. When the height or width of the initial containing block is changed, they are scaled accordingly.

These units are vh (viewport height), vw (viewport width), vmin (viewport minimum length) and vmax (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
<div></div>
CSS
div {
    height: 100vh;
}

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 and vw.

How is 100vh different from 100%?

Take this layout for example:

<body style="height: 100%">
    <div style="height: 200px">
        <p style="height: 100%; display: block;">Hello, world!</p>
    </div>
</body>

The p tag here is set to 100% height, but because its containing div has 200 pixels height, 100% of 200 pixels becomes 200 pixels, not 100% of the body height. Using 100vh instead means that the p tag will be 100% height of the body regardless of the div height. Take a look at this accompanying JSFiddle to easily see the difference!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • 68
    Strange behavior with scrolling when the element is actually taller than the viewport. The container maintains the viewport height and the content flows out of the container. `min-height:100vh` seems to work around this. – wdm Aug 18 '13 at 09:07
  • Viewport-Percentage don't have nearly enough support in order to be used in a production environment. You have to write a fallback every time which ends up creating more of a mess than if you had simply not used VP. – DGDD Jul 04 '14 at 15:41
  • 1
    @DGDD following on: there are plenty of other solutions here which work as far back as IE5, so there was no need for me to repeat what others have already said. This answer is solid enough for the majority of browsers released in the last 3 years since the spec was first introduced. – James Donnelly Jul 04 '14 at 15:49
  • Actually I was referring to mobiles which VPs are not supported on. With the amount of users that use mobiles (30% +), I think ignoring over a third of users can be considered a non-functioning answer. – DGDD Jul 04 '14 at 15:54
  • 9
    @DGDD this works on iOS Safari 6+, Android Browser 4.4+, BlackBerry Browser 10.0 and IEMobile 10.0. I don't know which mobile browser you're refering to, but those 4 make up over 90% of mobile browsers used. This question doesn't specify a requirement for working on mobile browsers. – James Donnelly Jul 04 '14 at 15:56
  • Okay, but the answer is still very myopic and only works in extremely limited situations. Flexbox CSS (http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/) would cover this answer and more. – robross0606 May 06 '15 at 21:12
  • 1
    @robross0606 again that is not the question I've answered here. That's a different question altogether, whose answer does indeed use flexbox: http://stackoverflow.com/questions/90178/make-a-div-fill-the-height-of-the-remaining-screen-space – James Donnelly May 06 '15 at 21:14
  • @DanielRodriguez according to [Can I Use...](http://caniuse.com/#search=vh) it works on all up-to-date mobile browsers except Opera Mini. – James Donnelly Apr 28 '17 at 08:20
  • You should also set `body { margin: 0 }` because even in case of `box-sizing: border-box`, margin is not included in measurement. – thSoft May 12 '17 at 14:27
  • VH isn't equal browser window height. It's viewport height. –  Sep 01 '17 at 07:25
  • @Soaku the viewport height is equal to the height of the region (viewport) the website displays in within a web browser. That's what I meant by "the height of the browser window", in much more basic terms. – James Donnelly Sep 01 '17 at 07:47
  • 1
    @JamesDonnelly Yes, I know. But that's not the answer to this question. Browser window height includes the navbar height. This would be better, because on mobile browsers, viewport height changes every time the navbar shows/hides. –  Sep 01 '17 at 07:49
  • you need to see this http://caniuse.com/#feat=viewport-units and than make desigion – webmak10 Sep 29 '15 at 14:54
  • 6
    Mobile alert: using vh mess with Chrome mobile which doesn't take into account its navbar. It then cover the top of your page with it. A serious issue if you have a menu there... – Offirmo Feb 22 '18 at 06:46
  • 1
    @Langdon, using `box-sizing: border-box` would help to avoid including `border` and `paddings` to `height` and `width`. [About box sizing](https://developer.mozilla.org/es/docs/Web/CSS/box-sizing) – chimos Jul 12 '18 at 13:23
  • 1
    Holy grail! you saved my day.. I was trying to apply gradient to the whole background and i saw a split at a certain height. After setting height to 100vh, its looking great – Divakar Rajesh Oct 19 '18 at 05:45
  • Incredibly useful! Allowed me to scrap many lines of JS and easily make a header with a min height for mobile, max height for desktop, and auto-adjusting range of heights in between for tablets, all with just a few simple declarations: `#header {height: 20vw; min-height: 64px; max-height: 128px; transition: 0.4s;}` – Mentalist Apr 11 '19 at 09:12
  • Wow I have been out of the front end UI game for a while. I didnt realize vh was a thing. This makes it quite easier than the hacks used back in the day. – Martin Jun 07 '21 at 16:58
  • Why have I not known about this yet. – Halo Mar 05 '22 at 20:08
  • As Offirmo mentioned, this does not take into account the top navbar. What is the workaround for that? – Jaideep Shekhar Aug 30 '23 at 10:45
609

If you want to set the height of a <div> or any element, you should set the height of <body> and <html> to 100% too. Then you can set the height of element with 100% :)

Here is an example:

body, html {
  height: 100%;
}

#right {
  height: 100%;
}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Ariona Rian
  • 9,153
  • 3
  • 24
  • 36
  • 127
    Correct me if I'm wrong, But i think you also need to set the height to all the parents of the div, to actually work – Dany Y Mar 28 '13 at 11:05
  • This won't work if I am using continuing design: page 6000px height, with blocks representing pages. I want one block to be exactly the viewport's height. – Qwerty Dec 21 '13 at 20:31
  • @DanyY, you are right. You do need to set the height to all the parents of the div, with the implications of everything having the height of the screen. Here is an [example](http://jsfiddle.net/f6WXR/). – toto_tico Jan 02 '14 at 07:31
  • This trick work for some cases but it wouldn't for some cases too, If you search for compatibility or more recommended way, you can view @James's Answer above :) Using Viewport Percentage Method :) , That's should work too. cheers – Ariona Rian Feb 19 '14 at 06:00
  • 9
    @Qwerty, here's the solution. Set css as so: `html { height: 100%*number of blocks; }`, `body { height: 100%;}`, `#div { height: 100%/number of blocks; }`. So if you have 3 sections, it will be `html { height: 300%; } body { height: 100%; } #div { height: 33.3% }` – cameronjonesweb Mar 12 '14 at 11:55
  • @DanyY, in addition to the steps mentioned in the answer, make a div with a class of `.body` and make its position `absolute`, then give this div a `height` of `100%` and `width` of `100%`, finally, don't forget to `bottom: 0` and `right: 0`. after that, whenever you need a full screen you can use this `.body` – Normal May 17 '22 at 09:15
312

If you’re able to absolutely position your elements,

position: absolute;
top: 0;
bottom: 0;

would do it.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Tinister
  • 11,097
  • 6
  • 35
  • 36
  • 22
    This works by taking the element out of the document flow and cementing its bottom value to the height of its parent. This is not ideal when your content exceeds the height of its parent. – Ricky Boyce Dec 18 '14 at 04:23
  • 2
    That does not work, when one of it's parents is set to `position:relative` and its height is not 100% of the viewport. It will adjust top and bottom to it's next relative or absolute ancestor. – Seika85 Jul 01 '16 at 14:39
150

You can use the view-port unit in CSS:

HTML:

<div id="my-div">Hello World!</div>

CSS:

#my-div {
    height: 100vh; /* vh stands for view-port height, and 1vh is 1% of screen height */
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
144

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 below I draw for you here:

How to make a div 100% height of the browser window?

Try the snippet I created for you as below:

.left {
  height: 100vh;
  width: 50%;
  background-color: grey;
  float: left;
}

.right {
  height: 100vh;
  width: 50%;
  background-color: red;
  float: right;
}
<div class="left"></div>
<div class="right"></div>
Alireza
  • 100,211
  • 27
  • 269
  • 172
  • 3
    Giving 100vh added a vertical scroll for the page , so followed this one but didnt worked. https://stackoverflow.com/questions/44645465/when-using-height-100vh-for-the-container-vertical-scrollbar-appears. Is there any way to avoid the vertical scroll and have the div touches the bottom without any content ? – Learner Nov 19 '19 at 05:57
  • @DILEEPTHOMAS look for existing `paddings/margins` – Legends Dec 12 '19 at 15:41
  • @Legends at the root file i tried giving as * {margin: 0, padding: 0} but didnt worked – Learner Dec 13 '19 at 06:44
  • 2
    @DILEEPTHOMAS take a look at this example here provided by Alireza, it also has a scrollbar because the body element has a margin. If you remove the margin the scrollbar is gone. It can have different causes, but this one I would checkout first. – Legends Dec 13 '19 at 08:35
124

All the other solutions, including the top-voted one with vh are sub-optimal when compared to the flex model solution.

With the advent of the CSS flex model, solving the 100% height problem becomes very, very easy: use height: 100%; display: flex on the parent, and flex: 1 on the child elements. They'll automatically take up all the available space in their container.

Note how simple the markup and the CSS are. No table hacks or anything.

The flex model is supported by all major browsers as well as IE11+.

html, body {
  height: 100%;
}
body {
  display: flex;
}

.left, .right {
  flex: 1;
}

.left {
  background: orange;
}

.right {
  background: cyan;
}
<div class="left">left</div>
<div class="right">right</div>

Learn more about the flex model here.

mb21
  • 34,845
  • 8
  • 116
  • 142
Airen
  • 2,139
  • 1
  • 14
  • 10
  • 5
    a word of warning: when the content of one of the left/right containers exceeds the original body height, the opposite container won't get resized, so the containers end up with different heights then. – schellmax Dec 14 '15 at 07:55
  • 1
    Example of the problem Schellmax points out: https://jsfiddle.net/Arete/b4g0o3pq/ – Arete Sep 12 '16 at 20:15
  • Depending on the design, `overflow-y: auto;` may avoid "containers exceeds the original body height". – Evi Song Sep 08 '18 at 08:54
  • the `body` also needs `margin: 0px` – bebbo Feb 23 '22 at 20:17
56

You don't mention a few important details like:

  • Is the layout fixed width?
  • Are either or both of the columns fixed width?

Here's one possibility:

body,
div {
  margin: 0;
  border: 0 none;
  padding: 0;
}

html,
body,
#wrapper,
#left,
#right {
  height: 100%;
  min-height: 100%;
}

#wrapper {
  margin: 0 auto;
  overflow: hidden;
  width: 960px; /* Width optional */
}

#left {
  background: yellow;
  float: left;
  width: 360px; /* Width optional, but recommended */
}

#right {
  background: grey;
  margin-left: 360px; /* Must agree with previous width */
}
<html>
<head>
  <title>Example</title>
</head>

<body>
  <div id="wrapper">
    <div id="left">
      Left
    </div>

    <div id="right"></div>
  </div>
</body>

</html>

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cletus
  • 616,129
  • 168
  • 910
  • 942
42

This is what worked for me:

<div style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; background: red;"> </div>

Use position:fixed instead of position:absolute, that way even if you scroll down the division will expand to the end of the screen.

Mobarak Ali
  • 751
  • 5
  • 19
Majda
  • 629
  • 7
  • 8
  • 2
    This code with the "position: [ fixed | absolute]" is great for the backgrounds of popups (like when you click to zoom a picture) but not so sure in the cases when you need to actually scroll down. Still useful dough! – Mannyfatboy Feb 24 '20 at 11:23
31

Here's a fix for the height.

In your CSS use:

#your-object: height: 100vh;

For browser that don't support vh-units, use modernizr.

Add this script (to add detection for vh-units)

// https://github.com/Modernizr/Modernizr/issues/572
// Similar to http://jsfiddle.net/FWeinb/etnYC/
Modernizr.addTest('cssvhunit', function() {
    var bool;
    Modernizr.testStyles("#modernizr { height: 50vh; }", function(elem, rule) {
        var height = parseInt(window.innerHeight/2, 10),
            compStyle = parseInt((window.getComputedStyle ?
                      getComputedStyle(elem, null) :
                      elem.currentStyle)["height"], 10);

        bool = !!(compStyle == height);
    });
    return bool;
});

Finally use this function to add the height of the viewport to #your-object if the browser doesn't support vh-units:

$(function() {
    if (!Modernizr.cssvhunit) {
        var windowH = $(window).height();
        $('#your-object').css({'height':($(window).height()) + 'px'});
    }
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonas Sandstedt
  • 1,160
  • 11
  • 12
29

Even with all of the answers here, I was surprised to find that none really solved the problem. If I used 100vh height/min-height, the layout broke when the content was longer than a page. If I instead used 100% height/min-height, the layout broke when the content was less than the page height.

The solution I found, which solved both cases, was to combine the top two answers:

html, body, #mydiv {
  height: 100%;
  min-height: 100vh;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
machineghost
  • 33,529
  • 30
  • 159
  • 234
  • 4
    I have a fixed 90px height navbar so i just had to change your "100vh" to "calc(100vh - 90px)" – remjx Mar 27 '20 at 04:17
28

100vw = 100% of the width of the viewport.

100vh = 100% of the height of the viewport.

If you want to set the div width or height 100% of browser-window-size you should use:

For width: 100vw

For height: 100vh

Or if you want to set it smaller size, use the CSS calc function. Example:

#example {
    width: calc(100vw - 32px)
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Руслан
  • 675
  • 7
  • 8
27

Try this - tested:

body {
  min-height: 100%;
}

#right, #left {
  height: 100%;
}

In later versions, you can use vh:

#right, #left {
  height: 100vh
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lukaszkups
  • 5,790
  • 9
  • 47
  • 85
26

100% works differently for width and height.

When you specify width: 100%, it means "take up 100% of the available width from the parent element or width of the window."

When you specify height: 100%, it only means "take up 100% of available height from the parent element." This means if you don't specify a height at a top level element, the height of all the children will be either 0 or height of the parent, and that is why you need to set the topmost element to have a min-height of window height.

I always specify the body to have a min-height of 100vh and it makes positioning and calculations easy,

body {
  min-height: 100vh;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dinesh Pandiyan
  • 5,814
  • 2
  • 30
  • 49
21

A full page is called a 'viewport' and you can design an element according to its viewport in CSS 3.

Such units are called viewport-percentage lengths and are relative to the size of the initial containing block.

  • Viewport-Height is called vh. The complete height of a page is 100vh.
  • Viewport-Width is called vw. The complete height of a page is 100vw.
  • There also exist vmin (viewport minimum length) and vmax (viewport maximum length).

So now, your problem can easily be solved by adding the following to your CSS:

.classname-for-right-div /* You could also use an ID */ {
  height: 100vh;
}

Here is information about the Viewport-relative lengths.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SK-the-Learner
  • 523
  • 5
  • 18
16

Add min-height: 100% and don't specify a height (or put it on auto). It totally did the job for me:

.container{     
    margin: auto;
    background-color: #909090;
    width: 60%;
    padding: none;
    min-height: 100%;
}
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Remus Simion
  • 177
  • 1
  • 2
15

The simplest way is to do it like this.

div {
    background: red;
    height: 100vh;
}

body {
    margin: 0px;
}
<div></div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
EMILO
  • 271
  • 4
  • 12
14

If you set the html and body_ height to 100%, it will cover the whole page.

And if you set any particular div minimum height to 100%, so it will cover the whole window like this:

CSS

html, body {
  height: 100%;
}

div#some-div {
  min-height: 100%;
}

Remember

This will only work if the div's direct parent is body, as percentage always inherited from the direct parent and by doing the above CSS code you are telling the div to inherit the height 100% from the direct parent (body) and make it your min-height: 100%.

Another way

Simply set the div height to 100vh. It means 100 viewport height.

CSS

div#some-div {
  height: 100vh
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • Also note that `minHeight: 100vh` won't work - it needs to be `height: 100vh` otherwise children set to `height: 100%` won't resize – David Tedman-Jones Aug 11 '22 at 02:58
  • @DavidJones Have you looked at both the Question and answer correctly, I haven't used `min-height:100vh` anywhere in the answer. The question is how to make a div 100% of browser window. – Sunderam Dubey Aug 11 '22 at 06:24
12

There are several methods available for setting the height of a <div> to 100%.

Method (A):

html,
body {
  height: 100%;
  min-height: 100%;
}
.div-left {
  height: 100%;
  width: 50%;
  background: green;
}
.div-right {
  height: 100%;
  width: 50%;
  background: gray;
}
<div class="div-left"></div>
<div class="div-right"></div>

Method (B) using vh:

html,
body {
  height: 100%;
  min-height: 100%;
}
.div-left {
  height: 100vh;
  width: 50%;
  background: green;
  float: left;
}
.div-right {
  height: 100vh;
  width: 50%;
  background: gray;
  float: right;
}
<div class="div-left"></div>
<div class="div-right"></div>

Method (c) using flex box:

html,
body {
  height: 100%;
  min-height: 100%;
}
.wrapper {
  height: 100%;
  min-height: 100%;
  display: flex;
}
.div-left {
  width: 50%;
  background: green;
}
.div-right {
  width: 50%;
  background: gray;
}
<div class="wrapper">
  <div class="div-left"></div>
  <div class="div-right"></div>
</div>
Penguin9
  • 481
  • 13
  • 23
Santosh Khalse
  • 12,002
  • 3
  • 36
  • 36
11

This worked for me:

html, body {
    height: 100%; /* IMPORTANT!!! Stretches viewport to 100% */
}

#wrapper {
    min-height: 100%; /* Minimum height for a modern browser */
    height:auto !important; /* Important rule for a modern browser */
    height:100%; /* Minimum height for Internet Explorer */
    overflow: hidden !important; /* Firefox scroll-bar */
}

Taken from this page.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
testing
  • 19,681
  • 50
  • 236
  • 417
11

Try to set height:100% in html & body

html, 
body {
    height: 100%;
}

And if you want to 2 div height same use or set the parent element display:flex property.

dippas
  • 58,591
  • 15
  • 114
  • 126
Mohammed Javed
  • 866
  • 2
  • 9
  • 24
  • The last sentence is *incomprehensible* (use of [machine translation](https://en.wikipedia.org/wiki/Google_Translate)?). Can you [fix it](https://stackoverflow.com/posts/36620191/edit), please? Thanks in advance. – Peter Mortensen Jan 17 '23 at 20:19
9

Here is something that is not exactly like what you had in previous answers, but it could be helpful to some:

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  margin: 0px;
}

#one {
  background-color: red;
}

#two {
  margin-top: 0px;
  background-color: black;
  color: white;
  overflow-y: scroll;
}

https://jsfiddle.net/newdark/qyxkk558/10/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
User128848244
  • 3,250
  • 3
  • 24
  • 22
8

Block elements consume the full width of their parent, by default.

This is how they meet their design requirement, which is to stack vertically.

9.4.1 Block formatting contexts

In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block.

This behavior, however, does not extend to height.

By default, most elements are the height of their content (height: auto).

Unlike with width, you need to specify a height if you want extra space.

Therefore, keep these two things in mind:

  • unless you want full width, you need to define the width of a block element
  • unless you want content height, you need to define the height of an element

.Contact {
  display: flex;     /* full width by default */
  min-height: 100vh; /* use full height of viewport, at a minimum */
}

.left {
  flex: 0 0 60%;
  background-color: tomato;
}

.right {
  flex: 1;
  background-color: pink;
}

body { margin: 0; } /* remove default margins */
<div class="Contact">
  <section class="left">
    <div class="">
      <h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</h1>
    </div>
  </section>
  <section class="right">
    <img />
  </section>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
7

One of the options is using CSS table. It has great browser support and even works in Internet Explorer 8.

JSFiddle Example

html, body {
  height: 100%;
  margin: 0;
}
.container {
  display: table;
  width: 100%;
  height: 100%;
}
.left, .right {
  display: table-cell;
  width: 50%;
}
.right {
  background: grey;
}
<div class="container">
  <div class="left"></div>
  <div class="right"></div>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stickers
  • 75,527
  • 23
  • 147
  • 186
7

Just use the "vh" unit instead of "px", which means view-port height.

height: 100vh;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
khalifeh
  • 105
  • 1
  • 6
7

Try this once...

* {
  padding: 0;
  margin: 0;
}

.parent_div {
  overflow: hidden;
  clear: both;
  color: #FFF;
  text-align: center;
}

.left_div {
  float: left;
  height: 100vh;
  width: 50%;
  background-color: blue;

}

.right_div {
  float: right;
  height: 100vh;
  width: 50%;
  background-color: green;
}
<div class=" parent_div">
  <div class="left_div">Left</div>
  <div class="right_div">Right</div>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sunil R.
  • 851
  • 8
  • 15
6

Use FlexBox CSS

Flexbox is a perfect fit for this type of problem. While mostly known for laying out content in the horizontal direction, Flexbox actually works just as well for vertical layout problems. All you have to do is wrap the vertical sections in a flex container and choose which ones you want to expand. They’ll automatically take up all the available space in their container.

halfer
  • 19,824
  • 17
  • 99
  • 186
robross0606
  • 544
  • 1
  • 9
  • 19
5

You need to do two things, one is to set the height to 100% which you already did. Second is set the position to absolute. That should do the trick.

html,
body {
  height: 100%;
  min-height: 100%;
  position: absolute;
}

Source

Prabhakar
  • 6,458
  • 2
  • 40
  • 51
5

You can use display: flex and height: 100vh

html, body {
  height: 100%;
  margin: 0px;
}
body {
  display: flex;
}

.left, .right {
  flex: 1;
}

.left {
  background: orange;
}

.right {
  background: cyan;
}
<div class="left">left</div>
<div class="right">right</div>
Thuc Nguyen
  • 235
  • 2
  • 8
  • 1
    There are already several flexbox solutions posted, including one with this exact implementation posted only 26 days before this answer. – TylerH Jan 03 '18 at 18:02
5

.wrapper {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  height: 100vh; /* Height window (vh) */
}
.wrapper .left{
  width: 80%; /* Width optional, but recommended */
}
.wrapper .right{
  width: 20%; /* Width optional, but recommended */
  background-color: #DD1F26;
}
<!--
vw: hundredths of the viewport width.
vh: hundredths of the viewport height.
vmin: hundredths of whichever is smaller, the viewport width or height.
vmax: hundredths of whichever is larger, the viewport width or height.
-->
<div class="wrapper">
  <div class="left">
    Left
  </div>
  <div class="right">
    Right
  </div>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RamNiwas Sharma
  • 337
  • 2
  • 6
5

Actually what worked for me best was using the vh property.

In my React application I wanted the div to match the page high even when resized. I tried height: 100%;, overflow-y: auto;, but none of them worked when setting height:(your percent)vh; it worked as intended.

Note: if you are using padding, round corners, etc., make sure to subtract those values from your vh property percent or it adds extra height and make scroll bars appear. Here's my sample:

.frame {
  background-color: rgb(33, 2, 211);
  height: 96vh;
  padding: 1% 3% 2% 3%;
  border: 1px solid rgb(212, 248, 203);
  border-radius: 10px;
  display: grid;
  grid-gap: 5px;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 50px 100px minmax(50px, 1fr) minmax(50px, 1fr) minmax(50px, 1fr);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamal S
  • 1,649
  • 1
  • 19
  • 24
  • By the way, you mention 'rounded corners' affecting the height of your element, I expect setting `box-sizing: border-box;` would overcome this, it means that the border size is also taken into account when calculating the size (width and height) of the element. – Neek Jun 15 '19 at 14:28
5

Now use height: 100vh; for a fixed window height:

<style>
    .header-top {
        height: 100vh;
        background: #000;
        color: #FFF;
        display: flex;
        align-items: center;
        padding: 10px;
        justify-content: space-around;
    }

    .header-top ul {
        list-style: none;
        padding: 0;
        margin: 0;
        display: flex;
        align-items: center;
    }

    .header-top ul li {
        padding:0px 10px;
    }
</style>

<div class="header-top">
    <div class="logo">Hello</div>
    <ul>
        <li>Menu</li>
        <li>About Us</li>
        <li>Contact US</li>
        <li>Login</li>
    </ul>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

Try the following CSS:

html {
    min-height: 100%;
    margin: 0;
    padding: 0;
}

body {
    height: 100%;
}

#right {
    min-height: 100%;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ProblemChild
  • 556
  • 1
  • 8
  • 16
4

2023 Update

The use of 100vh for setting the height of elements on mobile devices has been found to be unreliable, as browser UI may take up some of the screen size. Up until now, the only way to calculate the real height was by using JavaScript. However, with the introduction of new units in 2023, such as dvh, lhv, and svh, it is now possible to achieve dynamic height without the need for JavaScript. Google it to learn more about these units.

New code sample:

.yourElement {
  height: 100vh; /* fallback */
  height: 100dvh; /* dynamic viewport height */
}
MAZ
  • 643
  • 5
  • 18
3

This stuff will resize height of content automatically according to your browser. I hope this will work for you. Just try this example given below.

You have to set up only height:100%.

html,body {
  height: 100%;
  margin: 0;
}
.content {
  height: 100%;
  min-height: 100%;
  position: relative;
}
.content-left {
  height: auto;
  min-height: 100%;
  float: left;
  background: #ddd;
  width: 50%;
  position: relative;
}

#one {
  background: url(http://cloud.niklausgerber.com/1a2n2I3J1h0M/red.png) center center no-repeat scroll  #aaa;
  width: 50%;
  position: relative;
  float: left;
}

#two {
 background: url(http://cloud.niklausgerber.com/1b0r2D2Z1y0J/dark-red.png) center center no-repeat scroll #520E24;
  width: 50%;
  float: left;
  position: relative;
  overflow-y: scroll;
}
<div class='content' id='one'></div>
<div class='content-left' id='two'></div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rudra
  • 535
  • 4
  • 16
2

If you use position: absolute; and jQuery, you could use

$("#mydiv").css("height", $(document).height() + "px");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    There is no need for JavaScript. The modern solution is to use the [CSS flex box model](http://stackoverflow.com/a/17067826/1269037). – Dan Dascalescu Nov 06 '14 at 19:42
  • If you are using JS, I found this to be quite useful. NOTE: its besting to use this with $( window ).load(), as you will get different results being returned using F5 or CTRL+F5. Check this link for more info. http://stackoverflow.com/questions/13314058/jquery-height-returns-different-results-using-f5-or-ctrlf5 –  May 09 '16 at 15:27
1

Amazingly, none of the >30 answers so far give the best solution:

min-height: 100vh
Asker
  • 1,299
  • 2
  • 14
  • 31
0

Easiest:

html,
body {
  height: 100%;
  min-height: 100%;
}
body {
  position: relative;
  background: purple;
  margin: 0;
  padding: 0;
}
.fullheight {
  display: block;
  position: relative;
  background: red;
  height: 100%;
  width: 300px;
}
<html class="">

<body>
  <div class="fullheight">
    This is full height.
  </div>
</body>

</html>
Ruslan López
  • 4,433
  • 2
  • 26
  • 37
Tony Smith
  • 869
  • 1
  • 11
  • 25
0

You can use the following CSS to make a div 100% of the height of the browser window:

display: block;
position: relative;
bottom: 0;
height: 100%;
SK-the-Learner
  • 523
  • 5
  • 18
mohammad nazari
  • 951
  • 8
  • 11
0

try using 'height: 100%' on the body too.

0

You can do this simply by adding padding and margin to the body of the page:

in css:

body {
    padding: 0px;
    margin: 0px;
}

and then retry the width: 100%; again and it should work just fine.

Jack Smith
  • 17
  • 4
-1

Stupidly easy solution which supports cross-domain and also supports browser re-size.

<div style="height: 100vh;">
   <iframe src="..." width="100%" height="80%"></iframe>
</div>

Adjust the iframe height property as required (leave the div height property at 100vh).

Why 80%? In my real-world scenario I have a header inside the div, before the iframe, which consumes some vertical space - so I set the iframe to use 80% instead of 100% (otherwise it would be the height of the containing div, but start after the header, and overflow out the bottom of the div).

youcantryreachingme
  • 1,065
  • 11
  • 17
-2

Even though this solution is done with jQuery, I thought it may be useful for anyone doing columns to fit the screen size.

For columns starting at the top of the page, this solution is the simplest.

body, html {
  height: 100%;
}

div#right {
  height: 100%
}

For columns that are not starting at the top of the page (for example: if they are starting below the header).

<script>
     $(document).ready(function () {
         var column_height = $("body").height();
         column_height = column_height - 100; // 100 is the header height
         column_height = column_height + "px";
         $("#column").css("height",column_height);
    });
</script>

The first method applies the body height to it and the columns as well, which means that is starting_pixels + height100%.

The second method gets the height of page shown to the user by getting the height of the body and then subtracts the header size to know how much height is left to display the column.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • I made some edits but this is a great way to get the div to be the size of the window and works dynamically well. The 100% will render only one time, so multiple screens do not work. I would add a $(window).resize(function() { as well. It looks the same minus what I added. – Rob May 20 '13 at 15:33
  • 4
    No need for JavaScript. The modern solution is to use the much simpler [CSS flex box model](http://stackoverflow.com/a/17067826/1269037). – Dan Dascalescu Nov 06 '14 at 19:43
  • 1
    Adding external library like Jquery to solve a 100% CSS issue is not useful, also the same script can be done in plain javascript. – FranSanchis Jul 07 '20 at 10:26