Html css height absolute

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

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.

Good way to go, but vh, vw etc units are known to be buggy. There’s this lightweight js alternative if you need it: joaocunha.github.io/vunit

Here’s a simple and clear explanation of the CSS height property with percentage values: stackoverflow.com/a/31728799/3597276

42 Answers 42

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

How is 100vh different from 100% ?

Take this layout for example:

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!

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.

@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.

@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.

@robross0606 again that is not the question I’ve answered here. That’s a different question altogether, whose answer does indeed use flexbox: stackoverflow.com/questions/90178/…

Читайте также:  Ffmpeg options python discord

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.

If you want to set the height of a or any element, you should set the height of and to 100% too. Then you can set the height of element with 100% 🙂

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

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.

@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.

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

@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% >

If you’re able to absolutely position your elements,

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

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.

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.

You can use the view-port unit in CSS:

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:

Giving 100vh added a vertical scroll for the page , so followed this one but didnt worked. stackoverflow.com/questions/44645465/…. Is there any way to avoid the vertical scroll and have the div touches the bottom without any content ?

@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.

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

Learn more about the flex model here.

Читайте также:  Binary and operator in java

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.

You don’t mention a few important details like:

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 */ >

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.

This is what worked for me:

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

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!

Here’s a fix for the height.

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 :

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:

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:

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

In later versions, you can use vh :

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,

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).
Читайте также:  Java запуск пула потоков

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 */

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

The simplest way is to do it like this.

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:

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.

Also note that minHeight: 100vh won’t work — it needs to be height: 100vh otherwise children set to height: 100% won’t resize

@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.

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

html, body < height: 100%; min-height: 100%; >.div-left < height: 100%; width: 50%; background: green; >.div-right

Method (B) using vh:

html, body < height: 100%; min-height: 100%; >.div-left < height: 100vh; width: 50%; background: green; float: left; >.div-right

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

Try to set height:100% in html & body

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

The last sentence is incomprehensible (use of machine translation?). Can you fix it, please? Thanks in advance.

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

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 */
 

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.

Источник

Оцените статью