JavaScript Modal Popup Code with Example

JavaScript Modal Popup Code with Example
Code Snippet:Simple modal
Author: Artyom Safin
Published: January 9, 2024
Last Updated: January 22, 2024
Downloads: 738
License: MIT
Edit Code online: View on CodePen
Read More

In this JavaScript Modal Popup Code with Example, we’ll create a simple modal popup using HTML, CSS, and JavaScript. When you click the “Open Modal” button, the modal with some content will appear on the screen. You can close the modal by clicking the close button or anywhere outside the modal. The code showcases how to use event listeners to control the visibility of the modal and overlay elements.

How to Create JavaScript Modal Popup

1. Let’s start by creating the basic HTML structure. We need a button to open the modal, the modal container itself, and an overlay to cover the background when the modal is open.

<div class="overlay">
  
</div>

<div class="modal">
  <h1>This is modal</h1>
  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur rem ipsa deserunt modi vitae quis minima, atque autem saepe corporis!</p>
  <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nostrum tempora illum, eius, minima quo dolorem repellat assumenda error quae inventore fugiat aliquid eos culpa. Tempora vel doloribus rerum, reiciendis at natus eum labore quas tempore, exercitationem alias. Dignissimos, tenetur aut!</p>
  <button class="modal__button-close">
  </button>
</div>

<button type="button" class="modal__button-open">Open Modal</button>

2. Now, let’s style our modal and make it visually appealing. We’ll use CSS to achieve this. Here’s a snippet of the essential CSS styles:

@import url('https://fonts.googleapis.com/css?family=PT+Sans:400,700');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  height: 100%;
}

body { 
  font-family: 'PT Sans', sans-serif;
  background-color: #567;

}


.modal {
  background-color: orange;
  width: 50%;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 50px;
  box-shadow: 0px 0px 30px rgba(0,0,0,.3);
  word-wrap: break-word;
  transform-origin: center;
  transition: opacity .4s ease-out, visibility .4s, transform .4s ;
}

h1 {
  padding: 20px;
}

p {
  text-indent: 20px;
  margin-bottom: 20px;
}

.overlay {
  position: fixed;
  background-color: rgba(0,0,0,.3);
  width: 100%;
  height: 100%;
  backdrop-filter: blur(5px);
  transition: opacity .4s ease-out, visibility .4s;
  top: 0;
  left: 0;
}

.modal__button-close {
  background-color: transparent;
  border: none;
  color: #fff;
  padding: 10px;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  right: 5px;
  top: 5px;
  cursor: pointer;
  transition: color .2s;
}

.modal__button-close::before {
  content: '';
  display: block;
  width: 100%;
  height: 3px;
  background-color: currentColor;
  box-shadow: 0px 10px 0px currentColor, 0px -10px 0px currentColor;
}

.modal__button-close:hover,
.modal__button-close:active {
  color: rgba(255, 255, 255, .7);
}



.modal--closed,
.overlay--hidden {
  visibility: hidden;
  opacity: 0;
  z-index: -10;
}

.modal--closed {
  transform: translate(-50%, -50%) scale(.2);
}


.modal__button-open {
  border: none;
  background-color: transparent;
  text-transform: uppercase;
  font-weight: 700;
  color: #fff;
  padding: 20px;
  border-radius: 12px;
  font-size: 28px;
  cursor: pointer;
  border: 3px solid orange;
  transition: background-color .2s ease-out;
  
}
.modal__button-open:focus {
  outline: none;
}
.modal__button-open:hover,
.modal__button-open:active {
  background-color: orange;
}

Feel free to customize the colors, fonts, and dimensions according to your design preferences.

3. To make the modal interactive, we’ll use JavaScript. We’ll create a JavaScript file and add the following code:

const modal = document.querySelector('.modal')
const overlay = document.querySelector('.overlay')
const modalCloseButton = document.querySelector('.modal__button-close')
const modalOpenButton = document.querySelector('.modal__button-open')

function showModal() {
  modal.classList.toggle('modal--closed')
  overlay.classList.toggle('overlay--hidden')
}


modalCloseButton.addEventListener('click', showModal)
modalOpenButton.addEventListener('click', showModal)
overlay.addEventListener('click', showModal)

Now that you have a working modal, you can customize it further to suit your specific needs. Modify the content, style, and behavior to make it perfect for your project.

That’s all! hopefully, you have successfully integrated this JavaScript Modal Popup code example into your 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