How to fade background and show a dialog box

There's a nice trick we do at Pageflakes whenever we show a dialog box. We make the background faded so that user's attention is dragged to the dialog box and user does not get confused what to do. Here's how it looks:

Here's how it works:

  • First we create an absolute DIV which is semi-transparent
  • Above that DIV, we create another absolutely positioned DIV which contains the dialog box

We also need to make sure user cannot click on anything else outside the DIV area. We do this by eliminating all mouse events on the semi-transparent DIV.

Here's the DIV code:

<div id="blockUI" class="translucent" style='display: none; background-color: gray;
        width: 100%; height: 100%; position: absolute; left: 0px; top: 0px; z-index: 50000;"
        onclick="return false" onmousedown="return false" onmousemove="return false"
        onmouseup="return false" ondblclick="return false">
        &nbsp;</div>

The translucent class is like this:

.translucent
{
filter:alpha(opacity=50);
-moz-opacity:0.5;
opacity:0.5;
}

Update: There was a problem in the translucent class for which it was not working in FF and OP. It's fixed.

Published Wed, Aug 2 2006 15:23 by omar
Filed under:

Comments

# re: How to fade background and show a dialog box

Saturday, August 05, 2006 11:41 AM by Nicholson
Are you from www.pageflakes.com ?

# Another Link Round-Up

Thursday, August 17, 2006 5:46 AM by Life, Universe and Everything according to Dirk
Remote Membership/Roles Management of ASP.NET 2.0 Applications Flickr-ing about with .NET VM Additions

# re: How to fade background and show a dialog box

Thursday, August 17, 2006 10:49 PM by omar
Nicholson, yes. I do some work for Pageflakes :)

# re: How to fade background and show a dialog box

Tuesday, September 05, 2006 1:43 AM by chemtech
Kind of missing a bit, no? We can use this cross browser compliant javascript in the header to toggle the div(s): function toggle(id){ if (document.getElementById){ var layer = document.getElementById(id); layer.style.display = (layer.style.display == 'none') ? 'block' : 'none'; } } And then we can call it like: Block UI Or like maybe like this if you prefer: Block UI Or via another script to pop up all kinds of div goodness over top with a z-alignment greater than 50000. BUT in Firefox the transparent opacity of BlockUI still doesn't work :( And in IE there seems to be a slight margin on the right hand side. Other than that it's awesome, any tips to fix it? Kind Regards,

# re: How to fade background and show a dialog box

Friday, February 09, 2007 8:23 AM by jeebu

omar,

can this technology be implemented when u open a popu window...

<div id="blockUI" class="translucent" style='display: none; background-color: gray;

       width: 100%; height: 100%; position: absolute; left: 0px; top: 0px; z-index: 50000;"

       onclick="return false" onmousedown="return false" onmousemove="return false"

       onmouseup="return false" ondblclick="return false">

       &nbsp;</div>

how to use this above code./...

can u explain me in detail regarding it

thanx

jeebu

# re: How to fade background and show a dialog box

Friday, February 09, 2007 9:05 AM by jeebu

omar,

my requirement is like when opening a popup background should go grey fade...

jeebu

# re: How to fade background and show a dialog box

Monday, February 26, 2007 11:49 AM by Alvito

Hello,

Can u show the HTML for the div that needs to hold content. i believe the code you stated above just shows the div for the background .

# re: How to fade background and show a dialog box

Thursday, July 19, 2007 12:46 PM by Dj_Kat

How do you get the dialog box to display without opacity. I've been trying this without any luck the background fades nicely tho

# re: How to fade background and show a dialog box

Wednesday, January 16, 2008 5:55 PM by YSUT

Is there any way to blur the background ?

Thx anyway

# re: How to fade background and show a dialog box

Friday, March 07, 2008 10:24 AM by chandresh

Pl let me know this dialog box working "FireFox"

Thanks

# re: How to fade background and show a dialog box

Tuesday, August 04, 2009 10:14 AM by pat

Dj_Kat: Your dialog box looks like it has opacity because it is being displayed behind the masking div. If you give it a z-index attribute with a value higher than 50000 (as that is what the masking div is set to) it will display correctly i.e. in front of the mask...

Hope that helps...

Leave a Comment

(required) 
(required) 
(optional)
(required)