Creating a modal window without using JavaScript

Have you often wanted to create a modal window (a window that opens inside your website) but don't know anything about JavaScript? Let's find out how!
•
Updated on August 14, 2026
•
2 min read
Believemy logo

Have you often wanted to create a modal window (a window that opens within your website) but don't know JavaScript? Let’s discover together how to successfully achieve this!

Image

 

Step 1

Let's start with the HTML code.

HTML
<a href="#demo">Open Modal</a>

<div id="demo" class="modal">
  <div class="modal_content">
    <h1>Hello</h1>

    <p>Welcome to the modal window!</p>

    <a href="#" class="modal_close">×</a>
  </div>
</div>

The anchor of our link (#demo) serves as a reference to the id that will be on our modal window.

Step 2

Let's style the link.

CSS
a {
  text-decoration: none;
  padding: 15px;
  background-color: white;
  border-radius: 5px;
  text-transform: uppercase;
  color: black;
}

Step 3

Let's take care of the modal window.

CSS
.modal {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  top: 0; right: 0;
  bottom: 0; left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(77, 77, 77, .7);
  transition: all .4s;
}

Step 4

Let's detect the click!

CSS
.modal:target {
  visibility: visible;
  opacity: 1;
}

It's thanks to the target pseudo-class that we can do without JavaScript!

Step 5

Let's style the content of the modal to finish.

CSS
.modal_content {
  border-radius: 4px;
  position: relative;
  width: 500px;
  max-width: 90%;
  background: white;
  padding: 1.5em 2em;
}

.modal_close {
  position: absolute;
  top: 10px;
  right: 10px;
  color: grey;
  text-decoration: none;
}
Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Make an animated notification message with JavaScript

Whether it's to quickly warn your users that a feature of your site is not yet available or to let them know that an action has been successfully completed, notifications are the simplest and most modern way to set up. Find out how in this article!

December 9, 2024

Create a full-screen menu on click

Having a menu has become an indispensable classic. But having a menu that takes up the whole screen is ideal when you want your site to be totally responsive (and therefore suitable for mobile devices, for example). This article will help you to set up a full-screen menu that is activated on click!

December 9, 2024

Understanding the Window object with JavaScript

Find out all about the Window object with JavaScript: URL manipulation with window.location, history with window.history, DOM with window.document, browser with window.navigator, and screen with window.screen.

December 9, 2024

Create an image slideshow

Let's discover together how to create a simple and effective image slideshow using HTML, CSS and JavaScript!

December 6, 2024

Create a menu that spans the entire page using CSS

From time to time we have to ask our visitors which page they want to go to, by displaying the main sections of our website. This menu is often not very pleasant to use, so let's find out together in this article how to make it much more modern, with beautiful animations that will make your passers-by want to visit your site!

December 9, 2024

Creating animated presentation cards with JavaScript

Whether you're presenting the team behind your project or anything else, using animated presentation cards will give your page a successful finishing touch. Find out how in this article!

December 9, 2024

Creating a clock using JavaScript

Imagine a clock that shows the time to your users, on a travel site for example, with the time zone of the destination country? Let's create this magnificent clock together, using JavaScript!

December 9, 2024

Creating a random colour generator with JavaScript

Want to practise your JavaScript skills? Try building a random colour generator! In this article, we'll look at how to do it.

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.