DEV Community

David Day
David Day

Posted on Edited on

CSS-only Offcanvas with Native HTML

I previously wrote an article on my rewrite of PicoCSS. One of the components that I introduced in the rewrite was a CSS-only and classless offcanvas using native HTML.

On the HTML side, the component works by leveraging the native <dialog> element along with command/commandfor attributes. According to the Mozilla Developer Network, the <dialog> element has been available across browsers since March 2022 and the command/commandfor attributes since December 2025

On the CSS side, the component leverages :has pseudo-class to assert a specific markup and when the user doesn't have a preference specified for the prefers-reduced-motion setting then it uses the transform property. Again, according to MDN, the :has pseudo-class has been available across browsers since December 2023 and the transform property since September 2015.

Take for example this markup:

<button type="button" command="show-modal" commandfor="demo-offcanvas">Open offcanvas</button>
<dialog id="demo-offcanvas" closedby="any" aria-labelledby="demo-offcanvas-title">
  <aside>
    <article>
      <header>
        <button type="button" command="request-close" commandfor="demo-offcanvas" aria-label="Close"></button>
        <h2 id="demo-offcanvas-title">Offcanvas panel</h2>
      </header>
      <p>A full-height side panel.</p>
      <footer>
        <button type="button" command="request-close" commandfor="demo-offcanvas">Close panel</button>
      </footer>
    </article>
  </aside>
</dialog>
Enter fullscreen mode Exit fullscreen mode

The browser will natively open a dialog without any JavaScript, so all we need to do is style the dialog to look like an offcanvas. With this specific example, the DOM is structure like this: <dialog> > <aside> > <article>. The reason behind this design decision is simple:

  • <dialog>: We need to leverage the built-in HTML behavior
  • <aside>: We are indicating that this dialog should be on the "side".
    • It is sort of a stretch on the semantics side.
    • When <aside> was introduced, there was some debate on if it represented content on the side or content tangentially related to the main document and the latter sort of won out.
    • I'm using the former interpretation in this case because I think it logically makes sense for this situation.
  • <article>: This supplies the visual container.
    • The optional <header> element shows the offcanvas title and close button
    • The optional <footer> element is pushed down to the bottom of the view area with any additional information
    • This is a carryover of PicoCSS's original implementation of the modal component

To style the dialog, you can leverage the :has pseudo-class so that you can differentiate between a regular, plain-ole, dialog and an offcanvas. For example:

:where(dialog) {
  /* regular dialog styles... */
}

:where(dialog:has(> aside)) {
  /* offcanvas styles */
}
Enter fullscreen mode Exit fullscreen mode

In this situation we can differentiate the offcanvas by saying "I want to style every dialog that has an aside element as it's direct child" by combining the :has and > selectors.

If you would like to see how I styled the offcanvas in T'Pico, you can check out the source code here: https://github.com/dday9/t-pico/blob/master/src/components/offcanvas.css

And as always, here's a working demo of an offcanvas in action: blob:https://coddy.tech/673eec74-b1a0-49f2-bd0b-7c58193edc24

Top comments (0)