I want to make a div fit the initial height and width of a users screen.
I think the following crudely drawn diagram explain this better:
#div
{
width: 100%;
height: 100%;
}
does not seem to work
I want to make a div fit the initial height and width of a users screen.
I think the following crudely drawn diagram explain this better:
#div
{
width: 100%;
height: 100%;
}
does not seem to work
If I understand correctly (there's some room for confusion here):
#screenFiller {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
border: 15px solid orange
}
You might be after the position: fixed
version: http://jsfiddle.net/zRpNp/1/
Personally I like the pure CSS solution. Just use the vh
unit.
#filldiv
{
height: 100vh;
}
That will make the div fill the height of the browser window.
In addition, you can also make it fill the width of the browser window, this only uses the vw
unit.
#filldiv
{
width: 100vw;
}
Both are shown in this fiddle
I personally really like both of these units because they can be used for dynamic resizing of things such as font sizes.
For clarification...
I'd use vh
and vw
(viewport height & viewport width)
<html>
<body>
<div style="height:100vh;width:100vw">First screen section, fills a whole screen</div>
<div style="height:100vh;width:100vw;">Hmm... this too</div>
</body>
</html>
100% Doesn't work because it fills 100% of a container(body) only, not a screen.
Edit: A few browsers don't support it, but I find that most modern browsers do
i think this is the easiest way... by letting the browser tell he height... using java script
`
<html>
<head>
<title>viewport</title>
<style type="text/css">
* { padding:0; margin:0; }
#test { background:#aaa; height:100%; width:100%; }
</style>
<script type="text/javascript">
window.onload = function() {
var height = getViewportHeight();
alert("This is what it looks like before the Javascript. Click OK to set the height.");
if(height > 0)
document.getElementById("test").style.height = height + "px";
}
function getViewportHeight() {
var h = 0;
if(self.innerHeight)
h = window.innerHeight;
else if(document.documentElement && document.documentElement.clientHeight)
h = document.documentElement.clientHeight;
else if(document.body)
h = document.body.clientHeight;
return h;
}
</script>
</head>
<body>
<div id="test">
<h1>Test</h1>
</div>
</body>
</html>`
Setting your height to 100% in a div only means that your div will fill 100% of its container; not 100% of the page.
I would suggest that you would either want to use a min-height declaration on your div or its container or use JavaScript to find the initial height of the user's screen (because everybody will have something slightly different) and set that as an inline style on your div.
Code something along the lines of the following will return the screen dimensions:
var x,y;
if (self.innerHeight) // all except Explorer
{
x = self.innerWidth;
y = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight)
// Explorer 6 Strict Mode
{
x = document.documentElement.clientWidth;
y = document.documentElement.clientHeight;
}
else if (document.body) // other Explorers
{
x = document.body.clientWidth;
y = document.body.clientHeight;
}
You can do this completely with CSS too:
html, body {
margin: 0;
padding: 0;
min-height: 100%;
width: 100%;
}
div {
height: 100%;
width: 100%;
}
another way to do this with CSS only...
http://jsfiddle.net/pxfunc/qQ45K/
html, body {height:100%;} /* explicitly set html & body height */
#fillScreen {
position:relative;
top:0;
left:0;
height:100%;
width:100%;
}
<html><body style="margin: 0;">
<div style="height: 100%; width: 100%; background: #ccc;">a</div>
<div style="background: #777;height: 100%; width: 100%">b</div>
</body>
</html>
works for me
Your css style is looking for an id="div"
not a div tag. This should work assuming your div in parented by the <body>
or another 100%, 100% html element:
<div id="fullViewPort">
My Content
</div>
#fullViewPort {
width: 100%;
height: 100%;
}
<style>
body {
margin:0;
padding:0;
}
#try {
height:100%;
width:100%;
margin:0;
padding:0;
clear:both;
}
#next {
height:10%;
}
</style>
<body>
<div id="try">hello1</div>
<div id="next">hello2</div>
</body>
works for me.
Why you don't try :
<html><body style="margin: 0;">
<div style="height: 100%; width: 100%">stuff that user see</div>
<div style="height: 100%; width: 100%">Other stuff</div></body>
</html>
that should work