In this post, we will learn how to create a modal box with the help of CSS and JavaScript. A modal is a dialog box that appears at the top of a web page. The modal is used when we have to show more content in a small space on a web page.

How To Create A Modal Box With CSS and JavaScript ?

Create A Modal Box

Example of Modal Box



To create a  modal box, we follow the following steps:

Step (1) : In this step we write HTML code as follows:

<button id="modalBtn">Open Me!</button>
<div class="modal" id="modalBox">
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Modal Header</h2>
</div>
<div class="modal-body">
<p>Some Text in the Modal Body</p>
<p>Some text..........</p>
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div> code-box

Step (2) : Now write CSS code as follows:

button {
border: none;
outline: none;
color: #fff;
font-size: 16px;
background-color: #000;
padding: 4px 8px;
cursor: pointer;
border-radius: 5px;
}
.modal {
display: none;
position: fixed;
z-index: 9999;
left: 0;
top: 0;
width: 100%;
overflow: auto;
padding-top: 100px;
height: 100%;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.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: animatemodal;
-webkit-animation-duration: 0.4s;
animation-name: animatemodal;
animation-duration: 0.4s;
}
@-webkit-keyframes animatemodal {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatemodal {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
.modal-header h2 {
margin-top: 20px;
color: #FFE;
}
.modal-footer h3 {
margin-top: 20px;
color: #FFE;
}
.modal-body p {
padding: 5px 0;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {
padding: 2px 16px;
}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.close {
position: absolute;
top: 0;
right: 0;
padding: 5px 10px;
font-size: 25px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
} code-box

Step (3) : In this step we write JavaScript as follows:
  • First, let us save the value of modalBox id in the modal variable from document.getElementById () method.
  • Similarly, the values ​​of modalBtn and close are saved in btn and span variables respectively.
  • Then trigger the onclick event on btn and show the modal.
  • Then to close the modal, trigger the onclick event on the span and hide the modal.
  • To hide the modal by clicking elsewhere on the web page, we use the window.onclick function and hide the modal.
var modal = document.getElementById("modalBox");
var btn = document.getElementById("modalBtn");
var span = document.getElementsByClassName("close")[0];

btn.onclick = function() {
modal.style.display = "block";
}

span.onclick = function() {
modal.style.display = "none";
}

window.onclick = function(event) {
    if (event.target == modal) {
    modal.style.display = "none";
    }
} code-box

Summary

In this post we learn how to make a modal box with CSS ands JavaScript. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.

Post a Comment

Previous Post Next Post