--- url: "https://web.dev/articles/building/a-sidenav-component" titulek: "Building a sidenav component | Articles | web.dev" h1: "Building a sidenav component Stay organized with collections Save and categorize content based on your preferences." popis: "A foundational overview of how to build a responsive slide out sidenav" jazyk: "en" kanonicka: "https://web.dev/articles/building/a-sidenav-component" drobecky: ["Articles", "Building a sidenav component"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:35:03" znaku: 8857 slov: 1360 kodovani: "iso-8859-2" sha256: "e14ff366c84e12d1fd7c2c8e9394e604813ff8ad1fafadbd8cd13f1b97696660" --- Building a sidenav component Stay organized with collections Save and categorize content based on your preferences. A foundational overview of how to build a responsive slide out sidenav Adam Argyle In this post I want to share with you how I prototyped a Sidenav component for the web that is responsive, stateful, supports keyboard navigation, works with and without JavaScript, and works across browsers. Try the [demo](https://gui-challenges.web.app/sidenav/dist/). If you prefer video, here's a YouTube version of this post: Overview It's tough building a responsive navigation system. Some users will be on a keyboard, some will have powerful desktops, and some will visit from a small mobile device. Everyone visiting should be able to open and close the menu. Desktop to mobile responsive layout demo Light and dark theme down on iOS and Android Web Tactics In this component exploration I had the joy of combining a few critical web platform features: CSS `:target` CSS grid CSS transforms CSS Media Queries for viewport and user preference JS for `focus` UX enhancements My solution has one sidebar and toggles only when at a "mobile" viewport of `540px` or less. `540px` will be our breakpoint for switching between the mobile interactive layout and the static desktop layout. CSS `:target` pseudo-class One `` link sets the url hash to `#sidenav-open` and the other to empty (`''`). Lastly, an element has the `id` to match the hash: ` ` Clicking each of these links changes the hash state of our page URL, then with a pseudo-class I show and hide the sidenav: `@media (max-width: 540px) { #sidenav-open { visibility: hidden; } #sidenav-open:target { visibility: visible; } }` CSS Grid In the past, I only used absolute or fixed position sidenav layouts and components. Grid though, with its `grid-area` syntax, lets us assign multiple elements to the same row or column. Stacks The primary layout element `#sidenav-container` is a grid that creates 1 row and 2 columns, 1 of each are named `stack`. When space is constrained, CSS assigns all of the `
` element's children to the same grid name, placing all elements into the same space, creating a stack. `#sidenav-container { display: grid; grid: [stack] 1fr / min-content [stack] 1fr; min-height: 100vh; } @media (max-width: 540px) { #sidenav-container > * { grid-area: stack; } }` The `