Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a div adopt the height of the screen?

Tags:

html

css

height

I tried using

height: 100%

but this makes the div only as high as its contents - it just contains a single word of text.

How can I force the div to use the screen height instead?

like image 689
ipso facto Avatar asked Jul 08 '09 00:07

ipso facto


People also ask

How do I make a div fit the height of my screen?

Syntax: To set a div element height to 100% of the browser window, it can simply use the following property of CSS: height:100vh; Example: HTML.

How do I center a div according to my screen size?

Easiest way to center something regardless of page width is margin: auto; in your CSS, with height and width defined. Save this answer.

How do I make my div take 100% height of parent div?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.


2 Answers

You need the body and html elements to have 100% height as well.

Try the following CSS:

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}

YourDivSelector {
    height: 100%;
}

The margin and padding must be set to 0 to prevent autoscroll in Firefox.

like image 163
SLaks Avatar answered Oct 23 '22 20:10

SLaks


You should set all the containers that contain the div to height:100% as well, including the body and html tags.

like image 44
Andrew Johnson Avatar answered Oct 23 '22 18:10

Andrew Johnson