How to make a modal with CSS only

9

You can easily make your website modal box with only CSS and no JavaScript.

Follow below steps:

Step 1 : Add HTML

<div class="site_wrapper">
  <a class="btn" href="#first_css_modal">Open CSS Modal!</a>
</div>

<div class="modal" id="first_css_modal">
  <div class="modal_dialog">
    <div class="modal_header">
      <h1>Modal with CSS only <br/>@ <small style="color:red;">:target pseudo-class</small> </h1>
    </div>
    <div class="modal_content">
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      </p>
    </div>
    <div class="modal_footer">
      <a href="#" class="close_modal">Close Modal</a>
    </div>
  </div>
</div>

Step 2 : Add CSS

.site_wrapper {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:linear-gradient(0deg, rgba(0,130,100,1) 0%, rgba(206,210,32,1) 100%);
}
.site_wrapper .btn {
  display: inline-block;
  text-decoration: none;
  padding: 12px 24px;
  background-color: #fff;
  border-radius: 8px;
  text-transform: uppercase;
  color: #000000;
  font-family: 'Blinker', sans-serif;
  font-weight: 600;
}
.modal {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.75);
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s ease-in-out;
}
.modal:target {
  visibility: visible;
  opacity: 1;
}
.modal_dialog {
  border-radius: 10px;
  position: relative;
  max-width: 640px;
  width:100%;
  background: #fff;
  padding:16px 16px;
}
.modal_header h1 {
  margin-top: 0;
  margin-bottom: 0;
}
.modal_footer {
  text-align: right;
}
.close_modal {
  display: inline-block;
  text-decoration: none;
  padding: 8px 16px;
  background-color: #000000;
  border-radius: 5px;
  text-transform: uppercase;
  color: #ffffff;
}

Checkout Live demo view Modal

Result:

Leave a Reply