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

7

6

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?

Nissy answered 8/7, 2009 at 0:39 Comment(1)
Please post your HTML and CSS.Dowitcher
I
11

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.

Iain answered 8/7, 2009 at 0:45 Comment(0)
D
1

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

Dowitcher answered 8/7, 2009 at 0:45 Comment(1)
This was what I needed but I gave SLaks the answer tag because his came before yours. Thanks!Nissy
G
1

You also need to set html and body to height:100%;

 html,body{height:100%}
Gossamer answered 8/7, 2009 at 0:50 Comment(0)
M
1

I had the same issue. Setting the html and body height to 100% didn't work, but when I combined min-height of 100vh and a height of 100% on the div it worked.

html, body {
   height: 100%;
}

div {
   min-height: 100vh; 
   height: 100%;
}
Matherne answered 29/1, 2021 at 14:5 Comment(0)
X
0

You can get the width and height of the window using JavaScript and then use those values to set the height and width of the div, as needed.

Xenocryst answered 8/7, 2009 at 0:42 Comment(0)
C
0

maybe

min-height:100%;

what are you trying to do exactly? post some more info and we can help you more

Calender answered 8/7, 2009 at 0:45 Comment(0)
A
0

You can only meaningfully use height=100% if its containing element's height is definided. Its 100%, of what? no height if defined anywhere. You can use javascript to get the height of the current window (as previously mentioned), or specify a specific height of 800px or whatever value. :D

Angevin answered 8/7, 2009 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.