Hello Friends, In this tutorial we are to see how to create a simple Modal Popup using Jquery and CSS.

Modal Popup using Jquery and CSS:

Here is the example for creating Modal Popup using Jquery and CSS.

CSS:


html {
font-family: "roboto", helvetica;
position: relative;
height: 100%;
font-size: 100%;
line-height: 1.5;
color: #444;
}

h2 {
margin: 1.75em 0 0;
font-size: 5vw;
}

h3 { font-size: 1.3em; }

.v-center {
height: 100vh;
width: 100%;
display: table;
position: relative;
text-align: center;
}

.v-center > div {
display: table-cell;
vertical-align: middle;
position: relative;
top: -10%;
}

.btn {
font-size: 3vmin;
padding: 0.75em 1.5em;
background-color: #fff;
border: 1px solid #bbb;
color: #333;
text-decoration: none;
display: inline;
border-radius: 4px;
-webkit-transition: background-color 1s ease;
-moz-transition: background-color 1s ease;
transition: background-color 1s ease;
}

.btn:hover {
background-color: #ddd;
-webkit-transition: background-color 1s ease;
-moz-transition: background-color 1s ease;
transition: background-color 1s ease;
}

.btn-small {
padding: .75em 1em;
font-size: 0.8em;
}

.modal-box {
display: none;
position: absolute;
z-index: 1000;
width: 98%;
background: white;
border-bottom: 1px solid #aaa;
border-radius: 4px;
box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);
border: 1px solid rgba(0, 0, 0, 0.1);
background-clip: padding-box;
}
@media (min-width: 32em) {

.modal-box { width: 70%; }
}

.modal-box header,
.modal-box .modal-header {
padding: 1.25em 1.5em;
border-bottom: 1px solid #ddd;
}

.modal-box header h3,
.modal-box header h4,
.modal-box .modal-header h3,
.modal-box .modal-header h4 { margin: 0; }

.modal-box .modal-body { padding: 2em 1.5em; }

.modal-box footer,
.modal-box .modal-footer {
padding: 1em;
border-top: 1px solid #ddd;
background: rgba(0, 0, 0, 0.02);
text-align: right;
}

.modal-overlay {
opacity: 0;
filter: alpha(opacity=0);
position: absolute;
top: 0;
left: 0;
z-index: 900;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3) !important;
}

a.close {
line-height: 1;
font-size: 1.5em;
position: absolute;
top: 5%;
right: 2%;
text-decoration: none;
color: #bbb;
}

a.close:hover {
color: #222;
-webkit-transition: color 1s ease;
-moz-transition: color 1s ease;
transition: color 1s ease;
}

Jquery:

$(function(){

var appendthis = ("<div class='modal-overlay js-modal-close'></div>");

 $('a[data-modal-id]').click(function(e) {
 e.preventDefault();
 $("body").append(appendthis);
 $(".modal-overlay").fadeTo(500, 0.7);
 //$(".js-modalbox").fadeIn(500);
 var modalBox = $(this).attr('data-modal-id');
 $('#'+modalBox).fadeIn($(this).data());
 }); 
 
 
$(".js-modal-close, .modal-overlay").click(function() {
 $(".modal-box, .modal-overlay").fadeOut(500, function() {
 $(".modal-overlay").remove();
 });
 
});
 
$(window).resize(function() {
 $(".modal-box").css({
 top: ($(window).height() - $(".modal-box").outerHeight()) / 2,
 left: ($(window).width() - $(".modal-box").outerWidth()) / 2
 });
});
 
$(window).resize();
 
});

Add a reference to following Jquery Library.

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

HTML:

Declare the following Modal markup.


<div id="myPopup" class="modal-box">
<header> <a href="#" class="js-modal-close close">&times;</a>
<h3>Debug On Web</h3>
</header>
<div class="modal-body">
<p>This is sample Modal Popup using Jquery and CSS</p>
</div>
<footer> <a href="#" class="btn btn-small js-modal-close">Close</a> </footer>
</div>

Use the following open modal popup using the following link.

<a class="js-open-modal btn" href="#" data-modal-id="myPopup">Open Popup</a> 

Complete Source Code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Modal Popup using Jquery and CSS</title>

<style>
html {
font-family: "roboto", helvetica;
position: relative;
height: 100%;
font-size: 100%;
line-height: 1.5;
color: #444;
}

h2 {
margin: 1.75em 0 0;
font-size: 5vw;
}

h3 { font-size: 1.3em; }

.v-center {
height: 100vh;
width: 100%;
display: table;
position: relative;
text-align: center;
}

.v-center > div {
display: table-cell;
vertical-align: middle;
position: relative;
top: -10%;
}

.btn {
font-size: 3vmin;
padding: 0.75em 1.5em;
background-color: #fff;
border: 1px solid #bbb;
color: #333;
text-decoration: none;
display: inline;
border-radius: 4px;
-webkit-transition: background-color 1s ease;
-moz-transition: background-color 1s ease;
transition: background-color 1s ease;
}

.btn:hover {
background-color: #ddd;
-webkit-transition: background-color 1s ease;
-moz-transition: background-color 1s ease;
transition: background-color 1s ease;
}

.btn-small {
padding: .75em 1em;
font-size: 0.8em;
}

.modal-box {
display: none;
position: absolute;
z-index: 1000;
width: 98%;
background: white;
border-bottom: 1px solid #aaa;
border-radius: 4px;
box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);
border: 1px solid rgba(0, 0, 0, 0.1);
background-clip: padding-box;
}
@media (min-width: 32em) {

.modal-box { width: 70%; }
}

.modal-box header,
.modal-box .modal-header {
padding: 1.25em 1.5em;
border-bottom: 1px solid #ddd;
}

.modal-box header h3,
.modal-box header h4,
.modal-box .modal-header h3,
.modal-box .modal-header h4 { margin: 0; }

.modal-box .modal-body { padding: 2em 1.5em; }

.modal-box footer,
.modal-box .modal-footer {
padding: 1em;
border-top: 1px solid #ddd;
background: rgba(0, 0, 0, 0.02);
text-align: right;
}

.modal-overlay {
opacity: 0;
filter: alpha(opacity=0);
position: absolute;
top: 0;
left: 0;
z-index: 900;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3) !important;
}

a.close {
line-height: 1;
font-size: 1.5em;
position: absolute;
top: 5%;
right: 2%;
text-decoration: none;
color: #bbb;
}

a.close:hover {
color: #222;
-webkit-transition: color 1s ease;
-moz-transition: color 1s ease;
transition: color 1s ease;
}
</style>
</head>
<body>
<section class="v-center">
<div>
<h2>Modal Popup using Jquery and CSS</h2>

<br/>
<a class="js-open-modal btn" href="#" data-modal-id="myPopup">Open Popup</a> </div>
</section>
<div id="myPopup" class="modal-box">
<header> <a href="#" class="js-modal-close close">&times;</a>
<h3>Debug On Web</h3>
</header>
<div class="modal-body">
<p>This is sample Modal Popup using Jquery and CSS</p>
</div>
<footer> <a href="#" class="btn btn-small js-modal-close">Close</a> </footer>
</div>

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(function(){

var appendthis = ("<div class='modal-overlay js-modal-close'></div>");

$('a[data-modal-id]').click(function(e) {
e.preventDefault();
$("body").append(appendthis);
$(".modal-overlay").fadeTo(500, 0.7);
//$(".js-modalbox").fadeIn(500);
var modalBox = $(this).attr('data-modal-id');
$('#'+modalBox).fadeIn($(this).data());
});

$(".js-modal-close, .modal-overlay").click(function() {
$(".modal-box, .modal-overlay").fadeOut(500, function() {
$(".modal-overlay").remove();
});

});

$(window).resize(function() {
$(".modal-box").css({
top: ($(window).height() - $(".modal-box").outerHeight()) / 2,
left: ($(window).width() - $(".modal-box").outerWidth()) / 2
});
});

$(window).resize();

});
</script>
</body>
</html>

Output in the Browser:

Modal Popup using Jquery and CSS

View Demo:

Conclusion:

Hope this is a useful topic for your. I will appreciate your comments, Feedback and Suggestions.

Thank You.

Leave a Comment