UI Components Easy
Drawer Navigation
Mobile side-drawer navigation with slide-in panel, backdrop overlay, close button, and stacked nav links.
Open in Lab
MCP
css vanilla-js
Targets: JS HTML
Code
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--bg: #050910;
--nav-bg: #0d1117;
--accent: #3b82f6;
--text: #e2e8f0;
--text-muted: #94a3b8;
--muted: #475569;
--drawer-width: 75%;
}
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: var(--bg);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
color: var(--text);
padding: 2rem;
}
/* โโ Demo wrapper โโ */
.demo {
width: 100%;
max-width: 400px;
display: flex;
flex-direction: column;
align-items: center;
gap: 2rem;
}
.demo-title {
font-size: 1.5rem;
font-weight: 800;
text-align: center;
}
.demo-sub {
color: var(--muted);
font-size: 0.875rem;
text-align: center;
}
/* โโ Phone frame โโ */
.phone-frame {
width: 320px;
border-radius: 32px;
border: 1.5px solid rgba(255, 255, 255, 0.08);
background: var(--nav-bg);
overflow: hidden;
box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.04);
}
.phone-screen {
position: relative;
width: 100%;
height: 520px;
overflow: hidden;
}
/* โโ Status bar โโ */
.status-bar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px 6px;
font-size: 12px;
font-weight: 600;
color: var(--text-muted);
}
.status-icons {
font-size: 9px;
letter-spacing: 3px;
}
/* โโ Top bar โโ */
.top-bar {
display: flex;
align-items: center;
gap: 16px;
padding: 8px 16px 12px;
border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.top-bar-title {
font-size: 18px;
font-weight: 600;
}
/* โโ Hamburger button โโ */
.hamburger-btn {
display: flex;
flex-direction: column;
justify-content: center;
gap: 5px;
width: 36px;
height: 36px;
padding: 8px;
background: none;
border: none;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.hamburger-btn span {
display: block;
width: 100%;
height: 2px;
background: var(--text);
border-radius: 2px;
transition: background 0.3s;
}
.hamburger-btn:hover span {
background: var(--accent);
}
/* โโ Page content โโ */
.page-content {
padding: 28px 20px;
}
.page-content h2 {
font-size: 1.25rem;
margin-bottom: 10px;
}
.page-content p {
font-size: 0.85rem;
color: var(--text-muted);
line-height: 1.6;
margin-bottom: 20px;
}
.card-placeholder {
height: 64px;
background: rgba(255, 255, 255, 0.04);
border-radius: 10px;
margin-bottom: 10px;
border: 1px solid rgba(255, 255, 255, 0.04);
}
/* โโ Backdrop overlay โโ */
.backdrop {
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
visibility: hidden;
transition: opacity 0.3s, visibility 0.3s;
z-index: 10;
}
.drawer-open .backdrop {
opacity: 1;
visibility: visible;
}
/* โโ Drawer panel โโ */
.drawer {
position: absolute;
top: 0;
left: 0;
width: var(--drawer-width);
height: 100%;
background: var(--accent);
transform: translateX(-100%);
transition: transform 0.3s ease;
z-index: 20;
padding: 24px 20px;
display: flex;
flex-direction: column;
}
.drawer-open .drawer {
transform: translateX(0);
}
/* โโ Close button โโ */
.close-btn {
align-self: flex-end;
background: none;
border: none;
color: #fff;
font-size: 28px;
width: 36px;
height: 36px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
border-radius: 8px;
transition: background 0.3s;
}
.close-btn:hover {
background: rgba(255, 255, 255, 0.15);
}
/* โโ Nav links โโ */
.nav-links {
list-style: none;
margin-top: 32px;
display: flex;
flex-direction: column;
gap: 4px;
}
.nav-links a {
display: block;
padding: 14px 16px;
color: #fff;
text-decoration: none;
font-size: 16px;
font-weight: 500;
border-radius: 10px;
transition: background 0.3s;
}
.nav-links a:hover {
background: rgba(255, 255, 255, 0.15);
}const container = document.getElementById("drawerContainer");
const openBtn = document.getElementById("openDrawer");
const closeBtn = document.getElementById("closeDrawer");
const backdrop = document.getElementById("backdrop");
function openDrawer() {
container.classList.add("drawer-open");
}
function closeDrawer() {
container.classList.remove("drawer-open");
}
openBtn.addEventListener("click", openDrawer);
closeBtn.addEventListener("click", closeDrawer);
backdrop.addEventListener("click", closeDrawer);
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") closeDrawer();
});<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Drawer Navigation</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="demo">
<h1 class="demo-title">Drawer Navigation</h1>
<p class="demo-sub">Side drawer with slide-in panel and backdrop.</p>
<div class="phone-frame">
<div class="phone-screen" id="drawerContainer">
<!-- Status bar -->
<div class="status-bar">
<span>9:41</span>
<span class="status-icons">โโโ</span>
</div>
<!-- Top bar with hamburger -->
<div class="top-bar">
<button class="hamburger-btn" id="openDrawer" aria-label="Open menu">
<span></span>
<span></span>
<span></span>
</button>
<span class="top-bar-title">My App</span>
</div>
<!-- Page content -->
<div class="page-content">
<h2>Welcome</h2>
<p>Tap the hamburger icon to open the drawer navigation.</p>
<div class="card-placeholder"></div>
<div class="card-placeholder"></div>
</div>
<!-- Backdrop -->
<div class="backdrop" id="backdrop"></div>
<!-- Drawer panel -->
<nav class="drawer" id="drawer">
<button class="close-btn" id="closeDrawer" aria-label="Close menu">×</button>
<ul class="nav-links">
<li><a href="#home">Home</a></li>
<li><a href="#profile">Profile</a></li>
<li><a href="#messages">Messages</a></li>
<li><a href="#settings">Settings</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>Drawer Navigation
A mobile side-drawer that slides in from the left with a semi-transparent backdrop. Includes close button, nav links, and smooth CSS transitions.
Features
- Slide-in drawer panel from left
- Semi-transparent backdrop overlay
- Close button (ร) at top of drawer
- Stacked navigation links with hover states
- Hamburger button to open
- Keyboard accessible (Escape to close)
How it works
- Hamburger button toggles
.drawer-openclass on the container - Drawer panel translates from
translateX(-100%)totranslateX(0) - Backdrop fades in with
opacitytransition - Escape key and backdrop click close the drawer