How to Show Image Popup using Javascript and CSS in HTML Websites


If you’re building a HTML website, you can display popup windows to flash offers or breaking news. Most of the modern HTML websites don’t use the old method to display popup windows, because the browsers block them by default. So instead of showing popup windows, you can display popup within the web page by using Javascript and CSS in HTML sites.

The popup you display inside the web page can either be a image or text. If you use content management system like WordPress, there are lots of plugins to complete the task. For a basic HTML website, let us see how to do it. Let’s assume that you want to display some offer on the home page when the user loads the homepage / index.html.

1-13-2013 1-01-53 AM

First create the image that you want to display. For example: ad offers / brand related information etc. So, once the image is ready, open your index.html and paste the following codes before the </head> tag.

<script type="text/javascript" src=""></script>

<script type="text/javascript">
$(document).ready(function() {

var id = '#dialog';

//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

//Set heigth and width to mask to fill up the whole screen

//transition effect

//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();

//Set the popup window to center
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);

//transition effect

//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior


//if mask is clicked
$('#mask').click(function () {



<style type="text/css">

body {

a {color:#333; text-decoration:none}
a:hover {color:#ccc; text-decoration:none}

#mask {
#boxes .window {
#boxes #dialog {
padding:0px 10px 10px 440px;
background:url(pop-back.png) no-repeat;

The above code, the first part is a javascript code to show the popup in a lightbox effect. Following it is the CSS code for it. In the final #boxes #dialog css code, change the width and height according to your image size. Also the “close it” text on the popup image – you can play with the padding numbers to fix it where ever you need them. Also the background:url (enter the url address of the image)

Now following code should be entered before the </body> tag in the index.html.

<div id="boxes">
<div style="top: 199.5px; right: 551.5px; display: none;" id="dialog" class="window">
<a href="#" class="close">Close it</a>
<!-- Mask to cover the whole screen -->
<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;" id="mask"></div>

Finally save the index.html page and upload it. When ever the visitor lands on your home page, he will be shown the popup image in a lightbox effect.

For a limited time I'm sharing some select Tips and Tricks and How-To Guides for FREE.

Speak Your Mind