Skip to content

Instantly share code, notes, and snippets.

@kyrylo
Last active November 26, 2024 16:59
Show Gist options
  • Save kyrylo/1265f012a1913d873b345746153b5b45 to your computer and use it in GitHub Desktop.
Save kyrylo/1265f012a1913d873b345746153b5b45 to your computer and use it in GitHub Desktop.
<details> dropdown with a backdrop
<!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>
@kyrylo
Copy link
Author

kyrylo commented Nov 26, 2024

Screenshot 2024-11-26 at 11 23 22 AM

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