<main>
<h1>CSS Only Modal</h1>
<p> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quos, animi. Voluptatibus, architecto perferendis eos fugit dicta expedita cum incidunt, temporibus iure, officiis vitae. Officia nobis quibusdam repellendus voluptatem illum maxime?</p>
<a class="modal-btn" href="#modal">Click here to open modal</a>
</main>
<section role="dialog" aria-labelledby="modal-title" class="modal" id="modal">
<a class="modal-overlay" href="#" tabindex="-1"></a>
<div class="modal-content">
<a title="Close modal" aria-label="Close modal" href="#" class="modal-close">× </a>
<h2 id="modal-title"> Hello there! </h2>
<p>Quibusdam nulla velit dolores sed neque facere ut, tenetur fuga itaque? Nostrum in numquam sapiente iusto excepturi cum iure aspernatur quia soluta eum? Suscipit sunt dicta libero? Eos, necessitatibus dignissimos!</p>
<p> Click the close button or anywhere outside the modal to close it.</p>
</div>
</section>
<footer>
<p>
Pen by <a href="https://www.jemimaabu.com" target="_blank">Jemima Abu</a><span style="color: #D11E15"> ♥</span>
</p>
</footer>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 20px;
padding-top: 54px;
background-color: #fcfcfc;
position: relative;
}
main {
min-height: 100vh;
max-width: 1240px;
margin: auto;
padding-bottom: 64px;
}
h1 {
font-style: normal;
font-weight: 800;
font-size: 4.5em;
line-height: 120%;
letter-spacing: -0.02em;
text-transform: uppercase;
margin: 0;
}
h2 {
margin: 0;
font-weight: 600;
font-size: 3em;
line-height: 110%;
display: flex;
align-items: center;
letter-spacing: -0.02em;
}
p {
max-width: 721px;
font-style: normal;
font-weight: 400;
font-size: 16px;
line-height: 140%;
margin: 0;
margin-top: 35px;
}
.modal-btn {
transition: background 250ms;
padding: 16px 24px;
border-radius: 4px;
background-color: #3a3a3a;
color: #fcfcfc;
text-transform: uppercase;
font-size: 12px;
letter-spacing: 0.1em;
margin-top: 32px;
display: inline-block;
text-decoration: none;
}
.modal-btn:hover,
.modal-btn:focus {
background-color: black;
}
.modal {
position: fixed;
min-height: 100vh;
width: 100%;
top: 0;
left: 0;
display: flex;
z-index: 2;
}
.modal:not(:target) {
visibility: hidden;
transition-delay: 500ms;
transition-property: visibility;
}
.modal:target .modal-content {
transform: translateY(100vh);
animation: 500ms ease-in-out slideUp forwards;
}
.modal:not(:target) .modal-content {
transform: translateY(0);
animation: 500ms ease-out slideDown forwards;
}
.modal:target .modal-overlay {
opacity: 0;
animation: 500ms linear fadeIn forwards;
}
.modal:not(:target) .modal-overlay {
opacity: 1;
animation: 500ms linear fadeOut forwards;
}
.modal-overlay {
width: 100%;
height: 100%;
position: absolute;
background-color: rgba(0, 0, 0, 0.5);
left: 0;
}
.modal-content {
transition: transform 1s;
background: #fff;
width: 75%;
position: relative;
margin: auto;
height: 75%;
padding: 48px 24px;
border-radius: 4px;
max-width: 1000px;
}
.modal-close {
font-size: 36px;
text-decoration: none;
color: inherit;
position: absolute;
right: 24px;
top: 10px;
}
@keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes slideUp {
from {
transform: translateY(100vh);
}
to {
transform: translateY(0);
}
}
@keyframes slideDown {
from {
transform: translateY(0);
}
to {
transform: translateY(100vh);
}
}
footer {
position: absolute;
width: 100%;
left: 0;
bottom: 0;
text-align: center;
padding: 16px;
background-color: #eaeaea90;
display: flex;
justify-content: center;
}
footer p {
color: #221133;
margin: 0;
}
footer a {
text-decoration: none;
color: inherit;
}
This Pen doesn't use any external JavaScript resources.