How to make a modal using CSS, HTML, and JavaScript

A modal is a me​ssage box that is displayed on top of your screen.

svg viewer

​To create a modal in CSS:

  • HTML is required to create the container.
  • JavaScript is used to add event listeners to make the modal functional.
  • CSS is used to style the modal

Implementation

HTML:

  1. A button is created. Clicking on it will open the modal.
  2. A div is created. This will be a container for the modal.
  3. Another div is created. This will encapsulate all the contents of the modal.
  4. All the elements to be displayed on the modal are created. In the example below, the modal contains some text and a close button. Note that the header and footer for the modal can also be created here.

JavaScript: 5. Get the button created in HTML, and set the display property of the modal contents to block, in the onclick() method, so that the modal contents are visible after the button is clicked. 6. Get the close button, and set the display property of the modal to none, in the onclick() method, so that the modal contents are invisible after the button is clicked.

CSS: 7. The styles for some HTML elements are specified.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved