What is a slide-in menu?
On a phone or tablet, there's rarely room to show a full navigation bar. A slide-in menu (sometimes called an off-canvas menu or drawer) solves this: the menu hides off the side of the screen, and slides into view when you tap a button, usually one with three horizontal lines, known as a "hamburger" icon.
In this tutorial we'll build a slide-in menu without any JavaScript, using a modern HTML feature called the Popover API and a little CSS animation.
Try it: press the button below to open the menu. Close it with the × button, by clicking outside the menu, or by pressing Esc:
The old checkbox method
The original version of this tutorial used a popular trick from the time: a hidden checkbox and its <label>. Clicking the label ticked the checkbox, and CSS used input:checked ~ .slide-menu to slide the menu in. It was clever, but it had real problems:
| Problem | Why it matters |
|---|---|
| The "button" was really a checkbox | Screen readers (software that reads pages aloud) announced it as a checkbox, not a menu button |
| Hidden links could still be reached | Keyboard users pressing Tab moved focus onto invisible menu links off the side of the screen |
| No easy way to close it | Pressing Esc or clicking outside the menu did nothing |
<li> items directly inside <nav> | Not valid HTML: list items must sit inside a <ul> or <ol> |
Step 1: the HTML with popover
A popover is any element that sits hidden until something opens it, then appears on top of everything else on the page. HTML now has this built in. You add a popover attribute to the element you want to show, and point a button at it with popovertarget:
<button type="button" popovertarget="site-menu" aria-label="Open menu">
<svg ... aria-hidden="true">...</svg>
</button>
<nav id="site-menu" class="slide-menu" popover aria-label="Main">
<div class="slide-menu__header">
<strong>Menu</strong>
<button type="button" popovertarget="site-menu" popovertargetaction="hide"
aria-label="Close menu">×</button>
</div>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/products/">Products</a></li>
<li><a href="/services/">Services</a></li>
<li><a href="/faq/">FAQ</a></li>
<li><a href="/register/">Register</a></li>
<li><a href="/sign-in/">Sign in</a></li>
</ul>
</nav>
What the browser does for you
With just these attributes, and no JavaScript, the browser handles all of this:
- Opening and closing: the menu button toggles the menu, and the close button with
popovertargetaction="hide"closes it. - Light dismiss: clicking anywhere outside the menu, or pressing Esc, closes it automatically.
- Hidden means hidden: while closed, the menu's links can't be reached with the Tab key.
- On top of everything: the open menu appears in the top layer, a special layer above all other content, so you never need to fight with
z-index. - Screen reader support: the button tells assistive technology whether the menu is open or closed.
What about the icon? The original used the ≡ character (≡) for the hamburger icon. Its size and thickness vary between fonts, so we use a small inline SVG instead, which looks the same everywhere. The button's aria-label gives it a proper name, "Open menu", for screen readers.
Step 2: positioning the panel
Browsers give popovers some default styles: centred in the screen, with a border and padding. We'll replace those to make a full-height panel down the left side:
.slide-menu {
position: fixed;
inset: 0 auto 0 0;
width: min(280px, 85vw);
height: 100%;
max-height: none;
margin: 0;
padding: 20px 0;
border: 0;
overflow-y: auto;
color: #ffffff;
background: #1e3a8a;
box-shadow: 4px 0 24px rgb(0 0 0 / 0.3);
}
| Line | What it does |
|---|---|
position: fixed; | Pins the panel to the browser window, so it stays put even if the page scrolls |
inset: 0 auto 0 0; | Shorthand for top, right, bottom and left: attached to the top, bottom and left edges |
width: min(280px, 85vw); | 280px wide, but never more than 85% of a small screen. vw means a percentage of the window's width. |
height, max-height, margin | Override the popover's defaults so it fills the full height with no gaps |
overflow-y: auto; | Lets long menus scroll inside the panel |
box-shadow | A soft shadow along the right edge, lifting the panel above the page |
Step 3: the slide animation
By default a popover simply appears and disappears. To make it slide, we move it off-screen when closed, and back into place when open:
.slide-menu {
/* ...the styles from step 2... */
translate: -100% 0;
transition: translate 0.3s ease,
overlay 0.3s ease allow-discrete,
display 0.3s ease allow-discrete;
}
.slide-menu:popover-open {
translate: 0 0;
}
@starting-style {
.slide-menu:popover-open {
translate: -100% 0;
}
}
There are a few new ideas here, so let's take them one at a time:
translate: -100% 0moves the panel left by its own full width, just off the screen.:popover-openis a pseudo-class that matches the popover while it's open. Here it slides the panel back to0.@starting-styletells the browser where an animation should start from when an element first appears. Without it, the menu would simply pop into place, because there's no "before" position to animate from.allow-discreteondisplayandoverlaykeeps the menu visible and in the top layer until the closing slide has finished. Without it, the menu would vanish instantly when closed.
Good to know: @starting-style and allow-discrete are newer features. In a browser that doesn't support them, the menu still opens and closes perfectly; it just appears instantly instead of sliding. That's progressive enhancement at work.
Step 4: the backdrop and links
Dimming the page
Every popover in the top layer comes with a ::backdrop pseudo-element, a layer covering the rest of the page behind it. We can fade it to a semi-transparent black to focus attention on the menu, using the same animation technique:
.slide-menu::backdrop {
background: rgb(0 0 0 / 0);
transition: background-color 0.3s ease,
overlay 0.3s ease allow-discrete,
display 0.3s ease allow-discrete;
}
.slide-menu:popover-open::backdrop {
background: rgb(0 0 0 / 0.5);
}
@starting-style {
.slide-menu:popover-open::backdrop {
background: rgb(0 0 0 / 0);
}
}
Styling the menu links
.slide-menu ul {
list-style: none;
margin: 0;
padding: 0;
}
.slide-menu a {
display: block;
padding: 14px 20px;
color: #ffffff;
text-decoration: none;
border-left: 4px solid transparent;
transition: background-color 0.2s ease, border-color 0.2s ease;
}
.slide-menu a:hover {
background: rgb(255 255 255 / 0.1);
border-left-color: #93c5fd;
}
.slide-menu a:focus-visible {
outline: 3px solid #f59e0b;
outline-offset: -3px;
}
display: blockmakes each link fill the full width, so the whole row is clickable.- A transparent left border turns light blue on hover, giving a clear highlight without the layout shifting.
:focus-visiblegives keyboard users a clear outline as they move through the links.
Tip: white text on the deep blue #1e3a8a has a contrast ratio of about 10 to 1, so it's easy to read. If you change the colour, keep the contrast at 4.5 to 1 or higher.
A right-hand menu and other tips
Sliding in from the right
To bring the menu in from the right instead, attach it to the right edge and move it off-screen to the right:
.slide-menu--right {
inset: 0 0 0 auto;
translate: 100% 0;
box-shadow: -4px 0 24px rgb(0 0 0 / 0.3);
}
.slide-menu--right:popover-open {
translate: 0 0;
}
@starting-style {
.slide-menu--right:popover-open {
translate: 100% 0;
}
}
Respect reduced motion
Large sliding panels can be uncomfortable for people who are sensitive to motion. Make the menu appear instantly for visitors who've turned on "reduce motion" in their device settings:
@media (prefers-reduced-motion: reduce) {
.slide-menu,
.slide-menu::backdrop {
transition: none;
}
}
Browser support
The Popover API works in all current major browsers: Chrome, Edge, Firefox and Safari. In a very old browser that doesn't understand the popover attribute, the menu would stay hidden off-screen. If you need to support those, wrap the sliding styles in @supports selector(:popover-open) { ... }, so older browsers simply show the menu as an ordinary list of links.
Showing a normal menu on large screens
A slide-in menu is mainly for small screens. On a desktop, it's usually better to show the links in a regular navigation bar. A common approach is to hide the menu button on wide screens with a media query, and show a separate desktop menu instead.
The complete code
Here's all the CSS together. Use it with the HTML from step 1:
.slide-menu {
position: fixed;
inset: 0 auto 0 0;
width: min(280px, 85vw);
height: 100%;
max-height: none;
margin: 0;
padding: 20px 0;
border: 0;
overflow-y: auto;
color: #ffffff;
background: #1e3a8a;
box-shadow: 4px 0 24px rgb(0 0 0 / 0.3);
translate: -100% 0;
transition: translate 0.3s ease,
overlay 0.3s ease allow-discrete,
display 0.3s ease allow-discrete;
}
.slide-menu:popover-open { translate: 0 0; }
@starting-style {
.slide-menu:popover-open { translate: -100% 0; }
}
.slide-menu::backdrop {
background: rgb(0 0 0 / 0);
transition: background-color 0.3s ease,
overlay 0.3s ease allow-discrete,
display 0.3s ease allow-discrete;
}
.slide-menu:popover-open::backdrop { background: rgb(0 0 0 / 0.5); }
@starting-style {
.slide-menu:popover-open::backdrop { background: rgb(0 0 0 / 0); }
}
.slide-menu__header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 20px 16px;
}
.slide-menu ul { list-style: none; margin: 0; padding: 0; }
.slide-menu a {
display: block;
padding: 14px 20px;
color: #ffffff;
text-decoration: none;
border-left: 4px solid transparent;
transition: background-color 0.2s ease, border-color 0.2s ease;
}
.slide-menu a:hover { background: rgb(255 255 255 / 0.1); border-left-color: #93c5fd; }
.slide-menu a:focus-visible { outline: 3px solid #f59e0b; outline-offset: -3px; }
@media (prefers-reduced-motion: reduce) {
.slide-menu, .slide-menu::backdrop { transition: none; }
}
Wrapping up
The Popover API turns a slide-in menu into a few HTML attributes and some CSS. The browser takes care of opening, closing, light dismiss, keyboard access and stacking, while translate, @starting-style and ::backdrop give it a smooth, polished slide. It's simpler than the old checkbox trick, and it works for everyone.