<main>
<h1>CSS Only Modal</h1>
<h2 class="caveat">*with some JavaScript for accessibility</h2>
<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" aria-hidden="true" class="modal" id="modal">
<a class="modal-overlay" aria-hidden="true" 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;
padding-bottom: 64px;
margin: auto;
}
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;
}
.caveat {
font-size: 1.3em;
}
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) {
display: none;
}
.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;
}
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;
}
/* Look ma, no JavaScript! */
const modal = document.getElementById('modal')
window.addEventListener('popstate', () => {
const target = window.location.hash
if (target === '#modal') {
modal.focus();
modal.setAttribute('aria-hidden', false);
return
}
modal.setAttribute('aria-hidden', true)
});
window.addEventListener('keydown', (e) => {
if (e.key == "Escape" && window.location.hash === '#modal') {
window.location.hash = ""
}
})
This Pen doesn't use any external JavaScript resources.