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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.