How to create a Popup with CSS and Javascript

Written by on December 26, 2009 in CSS, Resources - 15 Comments
pop

Popups suck.

The good thing is you don’t see that many of them any more. That’s because browsers recognize that they suck and are most likely spam or ads and so they block the popups. The old way of creating a popup was to force open a new browser window over the current browser window. So this is what browsers block. But what if you actually have a need to use a popup window for something useful and not spammy? This happened to me the other day. A client wanted a popup window on their homepage to wish everyone a Merry Christmas.

Knowing I couldn’t use the old popup method, I went searching. I knew it’d be easy enough to use CSS to put a floating div over the content of the site, which creates a popup effect. The problem was how do you close the popup once it’s open? Javascript would be needed for this. Check out the demo I setup which shows how the CSS popup can be set to open on pageload or by clicking on a link; and then how Javascript allows you to have a “close” button that shuts or hides the popup. Then continue reading for the code and downloadable files.

If you check out the source code of the html file, you’ll see the following in the body. The “blanket” is the effect of putting the transparent black background behind the popup, which helps separate it from the rest of the content on the page. The “popUpDiv” is where you put the content of your popup.

<div id="blanket" style="display:none"></div>
<div id="popUpDiv" style="display:none">

<a href="#" onclick="popup('popUpDiv')" >Click to Close CSS Pop Up</a>
</div>
<a href="#" onclick="popup('popUpDiv')">Click to Open CSS Pop Up</a>

If you want the popup to load on page load, put the following in your body tag.

<body   onload="popup('popUpDiv')">

Here is the CSS used to style the blanket effect and the popup div. You’ll see I turned the blanket effect off for Internet Explorer, since it doesn’t appear to support “opacity” (big surprise).

#blanket {
background-color:#111;
opacity: 0.65;
*background:none;
position:absolute;
z-index: 9001;
top:0px;
left:0px;
width:100%;
}

#popUpDiv {
position:absolute;
background:url(pop-back.jpg) no-repeat;
width:400px;
height:400px;
border:5px solid #000;
z-index: 9002;
}

There is a Javacript file linked to the html file to make some of the magic happen. I’m going to make the demo files available so you can play with it and make your own. Enjoy!

The popup automatically centers itself in the screen. To adjust this for your popup, open the javascript file. You’ll see numbers for the height and width: window_width=window_width/2-200;//200 is half popup’s width

Change “200″ so it’s half the width of yours; do the same for the line that declares the height.

Credits:
Script Source

Sponsors

About the Author

Mike Ilsley "ilz" is a Boston based website designer, front-end developer, WordPress addict, owner of Beantown Design, and exclusive ThemeForest author. Follow him on Twitter and Facebook, and don't forget to subscribe to the WebDesign&Such email RSS feed. Also, check out his latest project, jQueryMobileShowcase.com.


  • http://www.jonathansace.com Jonathan Sace

    Cool! Keep up the good work and thank you for sharing.

  • http://www.africanhiphop.com/forum/viewtopic.php?f=10&t=11238&p=126229#p126229 Leida Wieciech

    Fantastic post! This could aid lots of people find out about this matter. Do you want to incorporate video clips together with these? It could undoubtedly help out. Your reason was spot on and owing to you; I probably won’t have to describe everything to my pals. I can simply direct them here

  • http://conceptualdesigngroup.net/portfolio.html# Dave G

    Hey Man – Thanks for that explanation – it’s allot cleaner and easier to understand from where I was working from originally. I got the window to open but now I want to place an image into it. I saw on another site that you had another page flowing into it. Any ideas? Also when I click on test link my browser shoots to the top. Any fix on that? Thanks in Advance, Dave G

    • ilz

      Missed your question. If you still need help let me know.

  • Azril

    Thanks, you are so kind!

  • Oran

    Thanks for the tutorial and example. Through resources such as this, I have been learning html, then css, and this is my first foray into js. I’ve learned a lot from exploring your example, but am hung up on a couple of aspects. One aspect is positioning. I see how the height and width are calculated, but don’t know how to specify what the position is relative to (I believe that it is relative to body in the example). For example, if I might want to position the popup image relative to the top of the div that the popup html is in (e.g. mainContent) or, even better, relative to the current vertical scroll position in the window. Any help is appreciated. Thanks!

    • ilz

      One of the tricky things when learning css is understanding relative positioning. It’s not actually relative to other items on the page, it’s relative to it’s original position on the page.

      The popup in this example is actually positioned with absolute positioning.

      Check out this page if you still are trying to grasp the relative positioning topic:

      http://www.w3schools.com/Css/css_positioning.asp

      • Oran

        Oh, yeah :)
        Thanks for the advice. Very helpful tutorial. JavaScript looks like it’s gonna be a lot of fun.

  • Jason

    I also have a positioning question….how can I make it so that the popup image is always centered in the browser, no matter what size it is.

    Thanks.

    BTW, this tutorial is great!!!!

    • ilz

      Glad you like it. I edited the post to answer your question, here’s the answer:

      The popup automatically centers itself in the screen. To adjust this for your popup, open the javascript file. You’ll see numbers for the height and width: window_width=window_width/2-200;//200 is half popup’s width

      Change “200″ so it’s half the width of yours; do the same for the line that declares the height.

      • Jason

        Thanks ilz!

        I tried this and it still did not work – but my issue (which I just found out) was with dotnetnuke. If anyone is going to use this function in DNN, then you must call the js file in your skin.ascx file:

        <script src="css-pop.js” type=”text/javascript”>

        Otherwise, it only centers itself within the content pane it is placed in, and not the entire browser window. Might be TMI, but just in case anyone wants to use this on their DNN pages….

        Thanks, again!

  • http://ATE rich

    great coding.

    one thing… is there a way to make the popup and blanket disappear after a few seconds, so that I can remove the link?

  • Dan

    If the user resizes the webpage once the popup has already been enabled, it doesn’t automatically center itself within the new window size. Is there a way to do this? It seems to be handled within the javascript, is there any way to position it in the css? Otherwise, a great tutorial, thanks!

  • http://www.oaktechng.com Samin T

    Please can you help me with a useful resources on this popup,in the index page of this website.

    http://www.armpension.com/

    I know it to be an onload event. Pls help me with this.

  • Pingback: TTRH Updates | kristin kreates