Skip to the main content

Off-canvas Drawer

Free Navigation

Two side panels, one from each edge, that open over the page as native modal dialogs and give focus back to their button when they close.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<div class="ocd">
  <header class="ocd__bar">
    <button class="ocd__open" type="button" aria-haspopup="dialog" aria-controls="ocd-nav">
      <svg viewBox="0 0 20 20" width="20" height="20" aria-hidden="true" focusable="false"><path d="M3 5h14M3 10h14M3 15h14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
      <span>Menu</span>
    </button>
    <p class="ocd__brand">Pocket library</p>
    <button class="ocd__open" type="button" aria-haspopup="dialog" aria-controls="ocd-filters">
      <span>Filters</span>
      <svg viewBox="0 0 20 20" width="20" height="20" aria-hidden="true" focusable="false"><path d="M3 5h14M6 10h8M9 15h2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
    </button>
  </header>

  <main class="ocd__page">
    <h1 class="ocd__h1" id="ocd-top">Spring reading list</h1>
    <p class="ocd__intro">A made-up page. Open the menu or the filters to see a side panel slide over it.</p>
    <div class="ocd__cards">
      <article class="ocd__card"><h2 class="ocd__h2" id="ocd-now">Reading now</h2><p>Three short books about maps, tides and gardens.</p></article>
      <article class="ocd__card"><h2 class="ocd__h2" id="ocd-later">Saved for later</h2><p>A pile of essays that wait for a quiet weekend.</p></article>
    </div>
  </main>

  <dialog class="ocd__dialog" id="ocd-nav" aria-labelledby="ocd-nav-title">
    <div class="ocd__panel">
      <div class="ocd__panel-head">
        <h2 class="ocd__panel-title" id="ocd-nav-title">Browse</h2>
        <button class="ocd__close" type="button">Close<span class="ocd__sr"> the menu</span></button>
      </div>
      <nav aria-label="Browse the library">
        <ul class="ocd__links">
          <li><a class="ocd__link" href="#ocd-top" aria-current="page">All books</a></li>
          <li><a class="ocd__link" href="#ocd-now">Reading now</a></li>
          <li><a class="ocd__link" href="#ocd-later">Saved for later</a></li>
        </ul>
      </nav>
      <p class="ocd__note">Escape, the Close button or a click on the dimmed page closes this panel.</p>
    </div>
  </dialog>

  <dialog class="ocd__dialog ocd__dialog--end" id="ocd-filters" aria-labelledby="ocd-filters-title">
    <div class="ocd__panel">
      <div class="ocd__panel-head">
        <h2 class="ocd__panel-title" id="ocd-filters-title">Filters</h2>
        <button class="ocd__close" type="button">Close<span class="ocd__sr"> the filters</span></button>
      </div>
      <fieldset class="ocd__set">
        <legend class="ocd__legend">Shelf</legend>
        <label class="ocd__check"><input type="checkbox" checked><span>Short reads</span></label>
        <label class="ocd__check"><input type="checkbox"><span>Picture books</span></label>
        <label class="ocd__check"><input type="checkbox"><span>Essays</span></label>
      </fieldset>
      <fieldset class="ocd__set">
        <legend class="ocd__legend">Sort by</legend>
        <label class="ocd__check"><input type="radio" name="ocd-sort" checked><span>Title</span></label>
        <label class="ocd__check"><input type="radio" name="ocd-sort"><span>Length</span></label>
      </fieldset>
      <button class="ocd__apply" type="button">Show results</button>
    </div>
  </dialog>
</div>

CSS

.ocd {
  --ocd-page: #f4f6fb;
  --ocd-surface: #ffffff;
  --ocd-ink: #1b2240;
  --ocd-muted: #505a78;
  --ocd-line: #d9deee;
  --ocd-bar: #1d2a5c;
  --ocd-bar-ink: #ffffff;
  --ocd-accent: #b05a00;
  --ocd-accent-ink: #ffffff;
  --ocd-wash: #eef1fa;
  --ocd-ring: #1b2240;
  box-sizing: border-box;
  width: min(100%, 38rem);
  overflow: hidden;
  color: var(--ocd-ink);
  background: var(--ocd-page);
  border: 1px solid var(--ocd-line);
  border-radius: 18px;
  box-shadow: 0 1px 2px rgb(27 34 64 / 6%), 0 14px 32px -14px rgb(27 34 64 / 28%);
  font: 1rem/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.ocd *, .ocd *::before, .ocd *::after { box-sizing: border-box; }
.ocd__sr { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.ocd__bar { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; padding: 0.6rem 0.75rem; color: var(--ocd-bar-ink); background: var(--ocd-bar); }
.ocd__brand { margin: 0; font-weight: 800; letter-spacing: 0.01em; text-align: center; }
.ocd__open { display: inline-flex; align-items: center; gap: 0.45rem; min-height: 44px; padding: 0 0.8rem; font: inherit; font-weight: 700; color: var(--ocd-bar-ink); background: rgb(255 255 255 / 12%); border: 0; border-radius: 10px; cursor: pointer; }
.ocd__open:hover { background: rgb(255 255 255 / 22%); }
.ocd__open:focus-visible { outline: 3px solid #ffffff; outline-offset: 2px; }
.ocd__page { padding: 1.5rem 1.25rem 1.75rem; }
.ocd__h1 { margin: 0; font-size: 1.5rem; line-height: 1.2; }
.ocd__intro { margin: 0.4rem 0 1.1rem; max-width: 28rem; color: var(--ocd-muted); }
.ocd__cards { display: grid; gap: 0.75rem; }
.ocd__card { padding: 1rem; background: var(--ocd-surface); border: 1px solid var(--ocd-line); border-radius: 14px; }
.ocd__card p { margin: 0.2rem 0 0; color: var(--ocd-muted); font-size: 0.95rem; }
.ocd__h2 { margin: 0; font-size: 1.05rem; }

.ocd__dialog { width: 100%; max-width: none; height: 100%; max-height: none; margin: 0; padding: 0; color: var(--ocd-ink); background: transparent; border: 0; overflow: hidden; }
.ocd__dialog::backdrop { background: rgb(14 18 38 / 60%); }
.ocd__panel { display: flex; flex-direction: column; gap: 1rem; width: min(20rem, 86vw); height: 100%; padding: 1rem; overflow-y: auto; overscroll-behavior: contain; background: var(--ocd-surface); box-shadow: 0 0 40px rgb(0 0 0 / 35%); }
.ocd__dialog--end .ocd__panel { margin-left: auto; }
.ocd__panel-head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; }
.ocd__panel-title { margin: 0; font-size: 1.2rem; }
.ocd__close { min-height: 44px; padding: 0 0.9rem; font: inherit; font-weight: 700; color: var(--ocd-ink); background: var(--ocd-wash); border: 0; border-radius: 10px; cursor: pointer; }
.ocd__links { display: grid; gap: 0.25rem; margin: 0; padding: 0; list-style: none; }
.ocd__link { display: flex; align-items: center; min-height: 48px; padding: 0 0.9rem; font-weight: 600; color: var(--ocd-ink); text-decoration: none; border-radius: 10px; }
.ocd__link:hover { background: var(--ocd-wash); }
.ocd__link[aria-current="page"] { color: var(--ocd-accent-ink); background: var(--ocd-bar); }
.ocd__note { margin: auto 0 0; font-size: 0.85rem; color: var(--ocd-muted); }
.ocd__set { margin: 0; padding: 0; border: 0; }
.ocd__legend { margin-bottom: 0.25rem; padding: 0; font-size: 0.75rem; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ocd-muted); }
.ocd__check { display: flex; align-items: center; gap: 0.7rem; min-height: 44px; cursor: pointer; }
.ocd__check input { width: 1.2rem; height: 1.2rem; margin: 0; accent-color: var(--ocd-bar); }
.ocd__apply { min-height: 46px; margin-top: auto; padding: 0 1rem; font: inherit; font-weight: 700; color: var(--ocd-accent-ink); background: var(--ocd-accent); border: 0; border-radius: 12px; cursor: pointer; }
.ocd__apply:hover { filter: brightness(1.1); }
.ocd__close:focus-visible, .ocd__link:focus-visible, .ocd__check input:focus-visible, .ocd__apply:focus-visible { outline: 3px solid var(--ocd-ring); outline-offset: 2px; }

.ocd__panel { translate: -100% 0; transition: translate 0.28s ease; }
.ocd__dialog--end .ocd__panel { translate: 100% 0; }
.ocd__dialog[open] .ocd__panel { translate: 0 0; }
.ocd__dialog { transition: display 0.28s allow-discrete, overlay 0.28s allow-discrete; }
.ocd__dialog::backdrop { opacity: 0; transition: opacity 0.28s ease, display 0.28s allow-discrete, overlay 0.28s allow-discrete; }
.ocd__dialog[open]::backdrop { opacity: 1; }
@starting-style {
  .ocd__dialog[open] .ocd__panel { translate: -100% 0; }
  .ocd__dialog--end[open] .ocd__panel { translate: 100% 0; }
  .ocd__dialog[open]::backdrop { opacity: 0; }
}
@media (max-width: 26rem) {
  .ocd__page { padding-inline: 1rem; }
  .ocd__brand { font-size: 0.9rem; }
  .ocd__open { padding: 0 0.6rem; }
}
@media (prefers-color-scheme: dark) {
  .ocd {
    --ocd-page: #11152a; --ocd-surface: #1b2140; --ocd-ink: #eceffb; --ocd-muted: #b0b8d6; --ocd-line: #2f3866; --ocd-bar: #26336e;
    --ocd-accent: #ffb454; --ocd-accent-ink: #261500; --ocd-wash: #262e57; --ocd-ring: #eceffb;
    box-shadow: 0 1px 2px rgb(0 0 0 / 35%), 0 16px 34px -14px rgb(0 0 0 / 70%);
  }
  .ocd__link[aria-current="page"] { color: #ffffff; background: #3a4a9c; }
}
@media (prefers-reduced-motion: reduce) {
  .ocd__panel, .ocd__dialog, .ocd__dialog::backdrop { transition: none; }
}

JavaScript

(function () {
  'use strict';
  var root = document.querySelector('.ocd');
  if (!root) return;

  Array.prototype.forEach.call(root.querySelectorAll('[aria-haspopup="dialog"]'), function (opener) {
    opener.addEventListener('click', function () {
      var drawer = document.getElementById(opener.getAttribute('aria-controls'));
      if (drawer && !drawer.open) drawer.showModal();
    });
  });

  Array.prototype.forEach.call(root.querySelectorAll('dialog'), function (drawer) {
    // A click on the dimmed page lands on the dialog itself, because the panel does not fill it.
    drawer.addEventListener('click', function (event) { if (event.target === drawer) drawer.close(); });
    Array.prototype.forEach.call(drawer.querySelectorAll('.ocd__close, .ocd__apply, .ocd__link'), function (control) {
      control.addEventListener('click', function () { drawer.close(); });
    });
  });

  Array.prototype.forEach.call(root.querySelectorAll('.ocd__link'), function (link) {
    link.addEventListener('click', function () {
      Array.prototype.forEach.call(root.querySelectorAll('.ocd__link'), function (other) { other.removeAttribute('aria-current'); });
      link.setAttribute('aria-current', 'page');
    });
  });
})();

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • Each drawer is a native modal dialog with a heading as its name, so the page behind is inert while it is open.
  • Escape closes the drawer and focus returns to the button that opened it.
  • Buttons that open a dialog say so with aria-haspopup and name the dialog with aria-controls.
  • The slide uses transform and stops under reduced motion.