← All components
Specimen U-002 · icon

Theme toggle

Sun to moon: detached rays pull in one by one, the sun winds up and the moon draws in. Plus five ways to move instead.

  • [ 0 deps ]No JavaScript and no packages. Just HTML and CSS you paste into your project.
  • state: aria-pressedYour code changes aria-pressed (or data-state) on the element; the CSS animates the change. That attribute is the only thing you wire up.
  • early releaseReady to use, but names of classes and settings may still change before version 1.0. Pin the version you copy.
  • all modern browsersUses only CSS that Chrome, Safari, Firefox and Edge have all supported for at least 2.5 years (Baseline widely available).
  • 1.16 kB · moderateThe CSS is 1191 bytes after gzip, which is how browsers download it. Lightweight is under 1 kB, moderate under 2.5 kB, heavy above.
Fig. 01 — 250 % · click to toggle
State
Variant
Line ends

Rounded ends and corners.

Colour Auto · follows the text colour
Preview background
<button class="u-theme-toggle" type="button" aria-pressed="false" aria-label="Dark mode">
  <svg class="u-theme-toggle__svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" aria-hidden="true" focusable="false">
    <g class="u-theme-toggle__sun">
      <circle class="u-theme-toggle__core" cx="12" cy="12" r="4" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:0" x1="12" y1="5" x2="12" y2="2.75" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:1" x1="16.95" y1="7.05" x2="18.54" y2="5.46" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:2" x1="19" y1="12" x2="21.25" y2="12" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:3" x1="16.95" y1="16.95" x2="18.54" y2="18.54" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:4" x1="12" y1="19" x2="12" y2="21.25" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:5" x1="7.05" y1="16.95" x2="5.46" y2="18.54" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:6" x1="5" y1="12" x2="2.75" y2="12" pathLength="1" />
      <line class="u-theme-toggle__ray" style="--i:7" x1="7.05" y1="7.05" x2="5.46" y2="5.46" pathLength="1" />
    </g>
    <g class="u-theme-toggle__night">
      <path class="u-theme-toggle__moon" d="M11.05 4.35A8.2 8.2 0 1 0 20.16 13.28A6.6 6.6 0 0 1 11.05 4.35Z" pathLength="1" />
    </g>
  </svg>
</button>
Use it in your project

Three steps, no install.

  1. 01

    Add the CSS

    Copy the CSS tab into your stylesheet, or save it as theme-toggle.css and link it. It works on its own; our design tokens are optional.

  2. 02

    Paste the markup

    Copy the HTML tab where the theme toggle should go. Your playground choices (variant, size, colour…) are already in it.

  3. 03

    Flip the state

    Set aria-pressed to "true" or "false" from your code. That's the whole wiring. Pick your framework below.

Accessibility

Use on a <button> with a fixed aria-label such as "Dark mode"; aria-pressed="true" means dark mode is on. Screen readers announce it as a toggle.

Source

packages/core/src/components/theme-toggle