Modal Window Using JavaScript

Modal Window Using JavaScript
Code Snippet:Modal Window
Author: Maria Tatosh
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 691
License: MIT
Edit Code online: View on CodePen
Read More

Modal windows are a common feature in web design to grab the user’s attention and display important information without navigating to a new page. This JavaScript code snippet helps you to create a modal window.

The main functionality is to display a modal window when a button is clicked, which can be used to show important messages or options to the user. It appears and disappears smoothly without any page refresh, enhancing user experience.

How to Create Modal Window Using Javascript

1. First, create the basic HTML structure for your modal window. In this code, we have a button that triggers the modal window and a hidden modal window itself.

<button class="modal-window-btn">MODAL WINDOW</button>
<div class="modal-window">
  <h3>Oops...</h3>
  <p>Something went wrong!</p>
  <button>OK</button><a href="#">Why do I have this issue?</a>
</div>
<div class="overlay"></div>

2. Next, apply CSS styles to your elements to make the modal visually appealing. You can modify the CSS code according to your preferences.

@import url("https://fonts.googleapis.com/css?family=Palanquin:300,400,500,600,700&display=swap");
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  outline: none;
  font-family: "Palanquin", sans-serif;
}

button {
  background: #363d4e;
  color: #fff;
  letter-spacing: 2px;
  border: 0;
  cursor: pointer;
}

.modal-window-btn {
  font-size: 16px;
  font-weight: 700;
  transition: 0.5s;
  padding: 15px 30px;
}

.modal-window {
  width: 400px;
  height: 260px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #fff;
  border-radius: 10px;
  padding: 20px;
  transition: 0.7s;
  position: absolute;
  top: -260px;
  z-index: 100;
}
.modal-window h3 {
  font-size: 35px;
}
.modal-window p {
  font-size: 20px;
}
.modal-window button {
  font-size: 16px;
  border-radius: 5px;
  padding: 5px 30px;
  margin: 15px 0;
}
.modal-window a {
  width: 100%;
  display: block;
  color: #f5b687;
  font-size: 20px;
  font-weight: 700;
  text-align: center;
  text-decoration: none;
  border-top: 1px solid #eee;
  padding: 5px 0;
}

.overlay {
  width: 100vw;
  height: 100vh;
  display: none;
  background: rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
}

.modal-window-active .modal-window-btn {
  display: none;
}
.modal-window-active .modal-window {
  top: calc(50% - 130px);
}
.modal-window-active .overlay {
  display: block;
}

3. Finally, let’s make the modal window interactive using JavaScript. Add the following JavaScript code to your project:

document.querySelector(".modal-window-btn").addEventListener("click", () => {
	document.body.classList.add("modal-window-active");
})

document.addEventListener("click", (e) => {
	if (!e.target.classList.contains("modal-window-btn") && 
		 	!e.target.classList.contains("modal-window")) {
		document.body.classList.remove("modal-window-active");
	}
})

You can customize the content inside the modal window to display any message, options, or information you want to convey to the user. Modify the HTML within the modal-window div according to your needs.

That’s all! hopefully, you have successfully created a modal window in your web/app project. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X