Featured Article

Effortless HTML JavaScript Modal Example Tutorial With Code

Adam Jul 13, 2026

In web development, creating overlays or pop-ups for additional information is a common practice. HTML and JavaScript, two of the core pillars of web development, can be used together to create responsive and interactive modal windows. Let's explore a simple yet effective example of an HTML/JavaScript modal window.

Popup Share Modal UI Design using HTML CSS & JavaScript
Popup Share Modal UI Design using HTML CSS & JavaScript

Before delving into the code, it's essential to understand that a modal is a type of pop-up window that displays important information and demands user action before it can be dismissed. Modals are typically used for login forms, error messages, or additional content. Now, let's create a basic modal using HTML and JavaScript.

CSS Modal Examples That You Can Download and Edit
CSS Modal Examples That You Can Download and Edit

Creating the HTML Structure

The first step is to create the HTML structure for our modal. We'll need an overlay to sit behind the modal and capture click events to close the modal.

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

We will create a simple overlay with a modal container. Inside the modal container, we'll add a close button and a paragraph of content.

Modal Structure

Login Form Modal in CSS & JavaScript
Login Form Modal in CSS & JavaScript

<div id="overlay"></div> and <div id="modal"><span id="close">×</span><p>This is a sample modal!</p></div>

Here, the #overlay is empty to start with and gains a class of 'show' when the modal is active. The #modal contains the close button and the content. The close button is represented by the <span id="close">×</span>.

Adding Modal to the Page

Modern HTML Forms
Modern HTML Forms

To include this modal in our web page, we'll add the overlay and modal to our page's body.

<div id="overlay"></div><div id="modal"><span id="close">×</span><p>This is a sample modal!</p></div>

Implementing JavaScript Functionality

𝙃𝙏𝙈𝙇 𝙏𝙖𝙜𝙨 𝙘𝙝𝙚𝙖𝙩 𝙨𝙝𝙚𝙚𝙩
𝙃𝙏𝙈𝙇 𝙏𝙖𝙜𝙨 𝙘𝙝𝙚𝙖𝙩 𝙨𝙝𝙚𝙚𝙩

Now, let's bring our modal to life with JavaScript. We'll add an event listener to a button on our page that triggers the modal. We'll also add 'click' event listeners to both the close button and overlay to allow users to dismiss the modal.

Opening the Modal

Alert Notification Message in HTML CSS & JavaScript
Alert Notification Message in HTML CSS & JavaScript
The <script> Tag in HTML
The <script> Tag in HTML
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
an image of a computer screen with text
an image of a computer screen with text
CSV To HTML Table In Javascript
CSV To HTML Table In Javascript
SIGN IN FORM ANIMATION
SIGN IN FORM ANIMATION
Responsive Delete Confirmation Modal Popup UI Design (HTML, CSS, JavaScript) – Free Download
Responsive Delete Confirmation Modal Popup UI Design (HTML, CSS, JavaScript) – Free Download
Create a Animated Modal Using HTML, CSS & JavaScript | With Source Code (2/2)
Create a Animated Modal Using HTML, CSS & JavaScript | With Source Code (2/2)
Embedding Images in HTML using <img>
Embedding Images in HTML using <img>
Toggle Fullscreen in Javascript
Toggle Fullscreen in Javascript
Pure CSS Modal Using Summary and Details Tags
Pure CSS Modal Using Summary and Details Tags
the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber
HTML <select> Box – Dropdown Menus Simplified
HTML <select> Box – Dropdown Menus Simplified
a web page with the message hello modal with react
a web page with the message hello modal with react
3 Daily JavaScript Modal Dialog
3 Daily JavaScript Modal Dialog
CSS image slider
CSS image slider
Create Image Accordion Slider With HTML CSS And JavaScript
Create Image Accordion Slider With HTML CSS And JavaScript
30+ Modal CSS Window Examples with Source Code - OnAirCode
30+ Modal CSS Window Examples with Source Code - OnAirCode
div vs span in HTML: Know the Difference
div vs span in HTML: Know the Difference
CSS Gradient Card Border Animation
CSS Gradient Card Border Animation

To open the modal, we'll assign a class of 'show' to the overlay and #modal via JavaScript. We'll also set their display to 'block'.

document.querySelector('#open-modal').addEventListener('click', () => { document.querySelector('#overlay').classList.add('show'); document.querySelector('#modal').style.display = 'block'; document.querySelector('#overlay').style.display = 'block';});

Closing the Modal

For dismissing the modal, we will remove the 'show' class from the overlay and #modal, and set their display to 'none'.

document.querySelector('#close').addEventListener('click', () => { document.querySelector('#overlay').classList.remove('show'); document.querySelector('#modal').style.display = 'none'; document.querySelector('#overlay').style.display = 'none';});

Finally, we need to ensure that when the user clicks outside the modal, it also gets closed.

This simple HTML/JavaScript modal example showcases the power of these web technologies and how they can be used to create interactive features in web development. From here, you can customize and enhance this modal with additional features.