Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow a div to cover the whole page instead of the area within the container

Tags:

css

I'm trying to make a semi-transparent div cover the whole screen. I tried this:

#dimScreen {     width: 100%;     height: 100%;     background:rgba(255,255,255,0.5); } 

But that doesn't cover the whole screen, it only covers the area within the div.

like image 982
Juicy Avatar asked Sep 03 '13 09:09

Juicy


People also ask

How do I cover the whole page in CSS?

We can do this purely through CSS thanks to the background-size property now in CSS3. We'll use the html element (better than body as it's always at least the height of the browser window). We set a fixed and centered background on it, then adjust it's size using background-size set to the cover keyword.

How do I make Div full screen on button click?

You'll want a fixed position element at 100% width and height , if you don't have a background color or image you'll be able to click through it. Set z-index higher then all other elements to ensure it is at the front if you need that.


1 Answers

Add position:fixed. Then the cover is fixed over the whole screen, also when you scroll.
And add maybe also margin: 0; padding:0; so it wont have some space's around the cover.

#dimScreen {     position:fixed;     padding:0;     margin:0;      top:0;     left:0;      width: 100%;     height: 100%;     background:rgba(255,255,255,0.5); } 

And if it shouldn't stick on the screen fixed, use position:absolute;

CSS Tricks have also an interesting article about fullscreen property.

Edit:
Just came across this answer, so I wanted to add some additional things.
Like Daniel Allen Langdon mentioned in the comment, add top:0; left:0; to be sure, the cover sticks on the very top and left of the screen.

If you want some elements to be at the top of the cover (so it doesn't cover everything), then add z-index. The higher the number, the more levels it covers.

like image 173
Michael Schmidt Avatar answered Sep 29 '22 15:09

Michael Schmidt