Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make DIV cover 100% of viewport instead of 100% of Body

My post is in regards to http://www.thepostboard.net

I need to make the black box that is currently covering 100% of the viewport cover the entire page instead. You'll only be able to notice this if your screen needs a scrollbar to view the website--otherwise it looks fine.

I have the overlaying div set to:

position: absolute;
height: 100%;
width: 100%;

I also have body set to:

height: 100%;
margin: 0px 0px;
padding: 0px; 0px;

But for some reason on my laptop if I scroll down I see the edge of the DIV and nothing is blocked off underneath it.

The purpose of this is to create a custom lightbox for the website. I have disabled the Javascript that dims the box so you can see the effect.

How do I make it cover the entire page, not just the viewport?

(My laptop is a 1366x768, so presumably you'd see it on anything with a height resolution <= 760).

like image 370
TigerTrussell Avatar asked Dec 03 '10 08:12

TigerTrussell


2 Answers

The <div> needs to be

top: 0;
left: 0;
position: fixed;
width: 100%;
height: 100%;
background-color: red;

It will then stay within the viewport's dimensions and "travel" with the scrollbar.

Edit: it will only cover the entire viewport if <body> is styled to

margin: 0;
padding: 0;
like image 110
Linus Kleen Avatar answered Nov 09 '22 22:11

Linus Kleen


This solution has worked best for me:

#overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

No need to style any other elements except the tag.

like image 44
dtbarne Avatar answered Nov 09 '22 21:11

dtbarne