In this web development project we are going to create a modal component using HTML CSS & JavaScript.
What is a modal? it is a container for a specific message with optional actions. Our custom modal will container a header component for the modal title, a body container for the content and a footer section for what ever extions you wish to communicate to the user.

Modal Project content:
- Intro
- Project overview
- Project Structure
- Basic CSS styling
- Button classes styling using CSS
- Create the modal component in HTML
- Styling the modal component using CSS
- Add click event to the button using JavaScript
- Create the modal component suing JavaScript
- Close the modal using JavaScript click event listener
- The this keyword for accessing the button in HTML using JavaScript
- Create a second modal
- Toggle between two modals
- Create a new HTML page using a click event on the closing button
Useful Links:
Copy the code for this project “Build a Payroll Web App using JavaScript” from below and just follow along if you wish
Project Img:
None
1 Modal HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Custom Modal</title>
</head>
<body>
<div class="container">
<h1>Custom Modal</h1>
<button class="btn btn-success" onclick="lunchModal()">Click me</button>
<!--
<div class="modal">
<div class="modal-container">
<div class="modal-content">
<div class="modal-header">
Modal title <span class="btn btn-close">X</span>
</div>
<div class="modal-body">
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Laudantium nesciunt fugiat minima aliquam voluptatem delectus
velit blanditiis consequatur ipsum dolores ipsam soluta quasi vero
nemo esse impedit maxime, facere hic.
</div>
<div class="modal-footer">
<button class="btn btn-cancel">No</button>
<button class="btn btn-success">Accept</button>
</div>
</div>
</div>
</div> -->
</div>
<script src="modal.js"></script>
</body>
</html>
2 Modal CSS code:
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Roboto", sans-serif;
}
.container {
width: 1000px;
max-width: 100%;
padding: 0 20px;
margin: 0 auto;
text-align: center;
}
h1 {
font-size: 5rem;
margin: 5rem auto;
}
/*Custom Btn*/
.btn {
display: inline-block;
font-weight: 500;
line-height: 1.5;
color: #212529;
text-align: center;
text-decoration: none;
vertical-align: middle;
cursor: pointer;
user-select: none;
background-color: transparent;
border: 1px solid transparent;
padding: 0.375rem 0.75rem;
font-size: 1rem;
border-radius: 0.25rem;
transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.3);
}
.btn-success {
background-color: #26a69a;
color: #fff;
}
.btn-cancel {
background-color: #ee6e73;
color: #fff;
}
.btn-close {
background-color: #212529;
color: #dddd;
}
.btn:active {
transform: scale(0.98);
box-shadow: none;
}
/*Custom modal*/
.modal {
position: absolute;
top: 0;
left: 0;
display: block;
box-sizing: border-box;
z-index: 1000;
width: 100%;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
outline: 0;
background-color: rgba(0, 0, 0, 0.2);
animation: fadeIn-Out 0.3s ease;
}
@keyframes fadeIn-Out {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.modal-container {
position: relative;
max-width: 500px;
margin: 1.75rem auto;
box-shadow: 0 0 15px 2px rgba(0, 0, 0, 0.2);
}
.modal-content {
position: relative;
display: flex;
flex-direction: column;
width: 100%;
pointer-events: auto;
background-color: #fff;
background-clip: padding-box;
border: 1px solid rgba(0, 0, 0, 0.2);
border-radius: 0.3rem;
outline: 0;
}
.modal-header {
display: flex;
flex-shrink: 0;
align-items: center;
justify-content: space-between;
padding: 1rem 1rem;
border-bottom: 1px solid #dee2e6;
background-color: rgba(102, 102, 102, 0.1);
border-top-left-radius: calc(0.3rem - 1px);
border-top-right-radius: calc(0.3rem - 1px);
font-size: 1.2rem;
font-weight: 700;
}
.modal-body {
position: relative;
flex: 1 1 auto;
padding: 1rem;
line-height: 1.5;
}
.modal-footer {
display: flex;
flex-wrap: wrap;
flex-shrink: 0;
align-items: center;
justify-content: flex-end;
padding: 0.75rem;
border-top: 1px solid #dee2e6;
border-bottom-right-radius: calc(0.3rem - 1px);
border-bottom-left-radius: calc(0.3rem - 1px);
}
3 Modal JavaScript code:
// console.log("log");
const body = document.querySelector("body");
function lunchModal(e) {
// console.log(body);
let createModal = document.createElement("modal");
createModalFunc(modal());
function createModalFunc(passInModal) {
createModal.innerHTML = passInModal;
body.appendChild(createModal);
}
if (e.id === "Accept") {
console.log("yes");
createModalFunc(modalInfo());
} else if (e.id === "Back") {
createModalFunc(modal());
}
}
// create modal
const modal = () => `
<div class="modal">
<div class="modal-container">
<div class="modal-content">
<div class="modal-header">
Modal title <span class="btn btn-close" id="Close" onclick="removeModal()">X</span>
</div>
<div class="modal-body">
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Laudantium nesciunt fugiat minima aliquam voluptatem delectus
velit blanditiis consequatur ipsum dolores ipsam soluta quasi vero
nemo esse impedit maxime, facere hic.
</div>
<div class="modal-footer">
<button class="btn btn-cancel" onclick="removeModal()">No</button>
<button class="btn btn-success" id="Accept" onclick="lunchModal(this);removeModal(this)">Accept</button>
</div>
</div>
</div>
</div>
`;
// info modal
const modalInfo = () => `
<div class="modal">
<div class="modal-container">
<div class="modal-content">
<div class="modal-header">
Terms & conditions <span class="btn btn-close" onclick="removeModal();newPage()">X</span>
</div>
<div class="modal-body">
You have accepted the terms and conditions!
</div>
<div class="modal-footer">
<button class="btn btn-cancel" id="Back" onclick="removeModal(this);lunchModal(this)">Back</button>
</div>
</div>
</div>
</div>
`;
// close modal function
const removeModal = (e) => {
// console.log(e.parentElement);
// const closeBtn = document.querySelector("#Close");
// console.log(closeBtn);
// const modal = e.parentElement.parentElement.parentElement.parentElement;
// modal.remove();
const modal = document.querySelector("modal");
// console.log(modal);
modal.remove();
};
// create new page
function newPage() {
body.innerHTML = `<div class = "container">
<h1><i class="fas fa-thumbs-up"></i> Like this video </h1>
</div>`;
}
View the full video tutorial
Become a web developer!
Learn HTML CSS & Sass JavaScript Bootstrap and more...
