Last active
November 26, 2024 16:59
-
-
Save kyrylo/1265f012a1913d873b345746153b5b45 to your computer and use it in GitHub Desktop.
<details> dropdown with a backdrop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Dropdown with Backdrop</title> | |
<style> | |
body { | |
font-family: "Lucida Grande"; | |
margin: 0; | |
} | |
.mt-40 { | |
margin: 10rem; | |
} | |
.mx-auto { | |
margin-left: auto; | |
margin-right: auto; | |
} | |
.dropdown-menu { | |
position: absolute; | |
background: white; | |
border: 1px solid silver; | |
padding: 0.5rem; | |
z-index: 20; | |
} | |
.backdrop { | |
position: fixed; | |
top: 0; | |
left: 0; | |
width: 100vw; | |
height: 100vh; | |
background: rgba(0, 0, 0, 0.5); | |
z-index: 10; | |
display: none; | |
} | |
details[open] .backdrop { | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="mt-40 mx-auto" style="width: 400px;"> | |
<h1>Dropdown with Backdrop</h1> | |
<details id="dropdown"> | |
<summary>Dropdown menu</summary> | |
<div class="backdrop"></div> | |
<ul class="dropdown-menu"> | |
<li>Option 1</li> | |
<li>Option 2</li> | |
<li>Option 3</li> | |
</ul> | |
</details> | |
</div> | |
<script> | |
document.addEventListener("DOMContentLoaded", () => { | |
const backdrop = dropdown.querySelector(".backdrop"); | |
// Close dropdown when clicking on the backdrop | |
backdrop.addEventListener("click", () => { | |
dropdown.removeAttribute("open"); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Author
kyrylo
commented
Nov 26, 2024

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment