How do I create a CSS-only modal dialog?
Rashid D
rashid d profile pic

Creating a CSS-only modal dialog involves using CSS to control the appearance and behavior of the modal, and HTML for the modal content. Here's a step-by-step explanation of how you can create a CSS-only modal dialog: HTML Markup: Start by creating the HTML structure for your modal dialog. Use appropriate HTML elements to represent the modal container, overlay, close button, and the content within the modal.

1
2
3
4
5
6
7
8
div class="modal">
  <div class="modal-overlay"></div>
  <div class="modal-content">
    <h2>Modal Title</h2>
    <p>Modal content goes here...</p>
    <button class="modal-close">&times;</button>
  </div>
</div>

CSS Styling: Apply the following CSS styles to create the CSS-only modal dialog:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
odal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s linear 0.25s, opacity 0.25s;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  max-width: 500px;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  border: none;
  background: transparent;
  font-size: 20px;
  cursor: pointer;
}

.modal:target {
  visibility: visible;
  opacity: 1;
  transition-delay: 0s;
}

In this example, the.modal class represents the container element for the modal dialog. It is positioned fixed withtop: 0 andleft: 0 to cover the entire viewport. Thez-index property ensures that the modal appears above other content. Thevisibility andopacity properties are initially set tohidden and0 to hide the modal. Thetransition property controls the animation when the modal is shown or hidden. The.modal-overlay class represents the overlay element that covers the entire viewport. It is positioned absolute withtop: 0 andleft: 0. Thebackground-color property sets the color andrgba() is used to add transparency to the overlay. The.modal-content class represents the content container within the modal. It is positioned absolute and centered usingtransform: translate(-50%, -50%). Adjust thepadding,background-color,border-radius, andmax-width properties to style the content area. The.modal-close class represents the close button within the modal. It is positioned absolute in the top-right corner. Customize the appearance of the close button by adjusting thefont-size,background, andcursor properties. The:target pseudo-class is used to apply styles to the modal when its corresponding anchor tag is targeted in the URL. This allows us to control the visibility and opacity of the modal. To open the modal, you can use an anchor tag with ahref attribute targeting theid of the modal. For example,Open Modal. By applying these CSS styles, the modal dialog will appear with a dark overlay and the content centered on the screen. Clicking the close button or outside the modal will close the modal dialog. Remember to customize the styles, add animations, and enhance the functionality based on your specific design requirements.