A modal is a dialog box or popup window that is displayed over the current page. The modal is used to display additional HTML components on the current page without changing/hiding the existing elements. The modal popup is displayed on top of the current page with the backdrop.
In this example code snippet, we will create a modal popup with HTML, CSS, and jQuery.
<!-- Button to trigger modal -->
<button id="mbtn">Open Modal</button>
<!-- The Modal -->
<div id="modalDialog" class="modal">
<div class="modal-content animate-top">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close">
<span aria-hidden="true">x</span>
</button>
</div>
<div class="modal-body">
<p>Woohoo, you're reading this text in a modal!</p>
<p>Modal body content goes here...</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary close">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
// Get the modal
var modal = $('#modalDialog');
// Get the button that opens the modal
var btn = $("#mbtn");
// Get the <span> element that closes the modal
var span = $(".close");
$(document).ready(function(){
// When the user clicks the button, open the modal
btn.on('click', function() {
modal.show();
});
// When the user clicks on <span> (x), close the modal
span.on('click', function() {
modal.fadeOut();
});
});
// When the user clicks anywhere outside of the modal, close it
$('body').bind('click', function(e){
if($(e.target).hasClass("modal")){
modal.fadeOut();
}
});
.animate-top{
position:relative;
animation:animatetop 0.4s
}
@keyframes animatetop{
from{top:-300px;opacity:0}
to{top:0;opacity:1}
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.275);
}
.modal-content {
margin: 5% auto;
width: 500px;
max-width: 90%;
background-color: #fff;
border: 1px solid rgba(0, 0, 0, 0.175);
border-radius: .3rem;
outline: 0;
}
.modal-header {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: start;
-ms-flex-align: start;
align-items: flex-start;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
padding: 1rem;
border-bottom: 1px solid #e9ecef;
border-top-left-radius: .3rem;
border-top-right-radius: .3rem;
}
.modal-title {
margin-bottom: 0;
line-height: 1.5;
margin-top: 0;
font-size: 1.25rem;
}
.modal-header .close {
float: right;
font-size: 1.5rem;
font-weight: 700;
line-height: 1;
color: #000;
text-shadow: 0 1px 0 #fff;
opacity: .5;
padding: 1rem;
margin: -1rem -1rem -1rem auto;
background-color: transparent;
border: 0;
}
.close:not(:disabled):not(.disabled) {
cursor: pointer;
}
.modal-body {
flex: 1 1 auto;
padding: 1rem;
}
.modal-body p {
margin-top: 0;
margin-bottom: 1rem;
}
.modal-footer {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: end;
-ms-flex-pack: end;
justify-content: flex-end;
padding: 1rem;
border-top: 1px solid #e9ecef;
}
.modal-footer>*{
margin: 5px;
}
/* buttons */
.btn {
display: inline-block;
font-weight: 400;
text-align: center;
white-space: nowrap;
vertical-align: middle;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
cursor: pointer;
}
.btn:focus, .btn:hover {
text-decoration: none;
}
.btn-primary {
color: #fff;
background-color: #007bff;
border-color: #007bff;
}
.btn-primary:hover {
color: #fff;
background-color: #0069d9;
border-color: #0062cc;
}
.btn-secondary {
color: #fff;
background-color: #7c8287;
border-color: #7c8287;
}
.btn-secondary:hover {
color: #fff;
background-color: #6c757d;
border-color: #6c757d;
}