The following is a simple JavaScript popup code example.
HTML code:
<!--Open Popup Button-->
<button id="myBtn">Open Popup</button>
<!--Popup-->
<div id="myModal" class="modal">
<!--Popup Content-->
<div class="modal-content">
<span class="close">×</span>
<p>Text in the popup...</p>
</div>
</div>
CSS code:
/*The popup (background)*/
.modal {
display: none; /*Hidden by default*/
position: fixed; /*Fixed positioning*/
z-index: 1; /*Set to the top layer*/
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
/*Popup content*/
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/*Close button*/
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
JavaScript code:
//Get the popup
var modal = document.getElementById('myModal');
//The button object that opens the popup
var btn = document.getElementById("myBtn");
//Get the <span> element to close the popup
var span = document.querySelector('.close');
//Click the button to open the popup
btn.onclick = function() {
modal.style.display = "block";
}
//Click <span> (x) to close the popup
span.onclick = function() {
modal.style.display = "none";
}
//Close the popup when the user clicks elsewhere
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
Live Demo
Adding a Header and Footer to the Popup
HTML code:
<!--Open Popup Button-->
<button id="myBtn">Open Popup</button>
<!--Popup-->
<div id="myModal" class="modal">
<!--Popup Content-->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Popup Header</h2>
</div>
<div class="modal-body">
<p>Popup text...</p>
<p>Popup text...</p>
</div>
<div class="modal-footer">
<h3>Popup Footer</h3>
</div>
</div>
</div>
CSS code:
/*Popup (background)*/
.modal {
display: none; /*Hidden by default*/
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
/*Popup content*/
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/*Add animation*/
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/*Close button*/
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5C8AB8;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5C8AB8;
color: white;
}