I understand that I answer this long, long after being asked, but I landed here after I briefly dwelled on this problem, and none of the current answers are correct.
Here is the correct answer:
body { position: relative; }
What is it! Now your item will be positioned relative to the <body> , and not the viewport.
I would not worry about position: fixed , as its browser support remains spotty. Safari before iOS 5 did not support it at all.
Note that your <div> element will span the <body> border-box (box + padding + border), but it will not cover its margin. Compensate by setting negative positions on the <div> (e.g. top: -20px ) or by removing the <body> marker.
I would also recommend setting <body> to height: 100% to ensure that you never end up in a partially hidden viewport on a shorter page.
Two valid points taken from previous answers. As Ben Blank says, you can lose the width and height declarations by positioning all four corners instead. And the Mercator is right that you should use an identifier instead of a class for such an important separate element.
This leaves the following recommended full CSS:
body { position: relative; margin: 0; } #screenMask { position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: 1000; background-color: #000; opacity: 0.7; filter: alpha(opacity=70); visibility: hidden; }
And HTML:
<body> <div id="screenMask"></div> </body>
I hope this helps someone else!
Aaron Adams Aug 16 '12 at 3:32 2012-08-16 03:32
source share