Aduok Code

A Power Rocker Switch, Built from a 3D-Tilted Paddle, Two Swapping Contact Shadows, CSS-Drawn I and O Glyphs, and One aria-checked Attribute That Flips a Set of Custom Properties

Introduction

This is a single HTML file that draws a soft, rounded rocker switch like the one on the back of a power strip. The left side carries the I mark for on and the right side carries the O mark for off. Click it and the paddle rocks toward the side you pressed: that half sinks into the housing, its mark darkens, and the shadow underneath changes shape as if the paddle lifted off one end and pushed down on the other.

The page rests on four ideas: a paddle tilted in 3D with perspective and rotateY, inner shading that fades in on the pressed half, two shadows under the paddle that trade a sharp ring for a soft blur, and one aria-checked attribute on a real button that swaps a handful of CSS custom properties. Every visual difference between on and off comes from that one attribute.

What you will learn

How to keep light and dark themes as pure colour tokens — how to mark up a toggle as role="switch" so screen readers announce it properly — how to describe a component state as a set of custom properties — how to fake depth with perspective, inset shadows and masks — how to animate between two box-shadows — how to draw the IEC power glyphs with plain CSS boxes — and how to wrap it all in a small class that emits its own change event.

01
Part One
Colour Tokens and Dark Mode

Step 1 — Name Every Colour Once

The switch is made of many shades of the same cool grey: the face of the paddle, the dark top edge of the well it sits in, the light bottom edge, the embossed highlight under each mark, the separator line. Each one gets a name on :root. The component styles only ever use those names, never a raw colour.

CSS
:root {
  --hue: 223;
  --ease: cubic-bezier(0.65, 0, 0.35, 1);
  --dur: 0.3s;

  --bg: hsl(var(--hue) 10% 90%);
  --fg: hsl(var(--hue) 10% 10%);
  --muted: hsl(var(--hue) 10% 60%);
  --face: hsl(var(--hue) 10% 90%);
  --well-top: hsl(var(--hue) 10% 65%);
  --well-bottom: hsl(var(--hue) 10% 87.5%);
  --well-depth: hsl(var(--hue) 10% 70%);
  --cast: hsl(var(--hue) 10% 10% / 0.7);
  /* ...plus focus, rim, glow, separator and emboss tokens */

  font-size: clamp(60px, calc(60px + 60 * (100vw - 280px) / 3560), 120px);
}

Dark mode is then just a second list of values inside prefers-color-scheme: dark. It does not repeat a single selector from the component. The top edge of the well goes almost black, the face drops to 30% lightness, and the muted mark goes darker instead of lighter so the unpressed side still reads as recessed.

CSS
@media (prefers-color-scheme: dark) {
  :root {
    --bg: hsl(var(--hue) 10% 30%);
    --fg: hsl(var(--hue) 10% 90%);
    --muted: hsl(var(--hue) 10% 20%);
    --face: hsl(var(--hue) 10% 30%);
    --well-top: hsl(var(--hue) 10% 5%);
    --cast: hsl(var(--hue) 10% 10% / 0.9);
    /* ... */
  }
}

The root font size is clamped between 60 and 120 pixels and every size in the switch is in em. That makes the whole thing one scalable object: change the root size and the paddle, shadows, glyphs and radii all grow together.

A theme should be a list of colours, not a second copy of the component.

02
Part Two
A Real Switch in the Markup

Step 2 — One Button with role="switch"

The control is a native button, so it is focusable and responds to Space and Enter with no extra code. It carries role="switch" and aria-checked, which is the pattern screen readers understand for an on/off control: it is announced as "Power, switch, off". The visible marks are decoration, so everything inside the button is aria-hidden, and the accessible name comes from a visually hidden Power label.

HTML
<span class="sr-only" id="power-label">Power</span>
<button class="rocker" type="button" role="switch"
        aria-checked="false" aria-labelledby="power-label" data-rocker>
  <span class="rocker__cast rocker__cast--on"  aria-hidden="true"></span>
  <span class="rocker__cast rocker__cast--off" aria-hidden="true"></span>
  <span class="rocker__bezel" aria-hidden="true">
    <span class="rocker__paddle">
      <span class="rocker__mark rocker__mark--on"><span class="rocker__glyph"></span></span>
      <span class="rocker__sep"></span>
      <span class="rocker__mark rocker__mark--off"><span class="rocker__glyph"></span></span>
    </span>
  </span>
</button>

From the outside in, there are four layers. The button itself is the well cut into the panel. The two cast spans are the shadows the paddle throws onto the well. The bezel is the housing that holds the paddle and gives it perspective. The paddle is the part that tilts, and it holds the two marks with a thin separator between them.

Do not store state in aria-labelledby

A common shortcut is to point aria-labelledby at whichever option is selected and style off that. It works visually, but it changes the button's accessible name to "C" or "F" or "O", and the screen reader never hears that it is a toggle. Keep the name fixed and put the state in aria-checked.

03
Part Three
State as Custom Properties

Step 3 — Describe Off, Then Override It for On

Instead of writing one block of rules for the off state and another for the on state, the component declares a short list of custom properties that describe the off state. Every part of the switch reads those properties. The on state is a single rule that gives the same properties new values.

CSS
.rocker {
  --tilt: 15deg;        /* paddle leans right: O side pressed */
  --shift: 0.125rem;
  --pit-on: 0;          /* inner shading on the I half */
  --pit-off: 1;         /* inner shading on the O half */
  --ink-on: var(--muted);
  --ink-off: var(--fg);
  --ring: 0 0 0 0.125em var(--cast);
  --blur: 0 0 0.25em 0.125em var(--cast);
  --cast-on: var(--blur);
  --cast-off: var(--ring);
  --rot-on: -7.5deg;
  --rot-off: 0deg;
}

.rocker[aria-checked="true"] {
  --tilt: -15deg;
  --shift: -0.125rem;
  --pit-on: 1;
  --pit-off: 0;
  --ink-on: var(--fg);
  --ink-off: var(--muted);
  --cast-on: var(--ring);
  --cast-off: var(--blur);
  --rot-on: 0deg;
  --rot-off: 7.5deg;
}

Read the two blocks side by side and you have the full specification of the switch. Each pair is a mirror: the tilt flips sign, the pit opacities swap, the ink colours swap, the shadow shapes swap. Adding a third visual change later means adding one line to each block, not hunting through nested selectors.

04
Part Four
The Tilting Paddle

Step 4 — The Well, the Bezel, and a Paddle in Perspective

The button is a 3.1875em by 1.5em pill with three inset shadows: a dark line at the top, a light line at the bottom, and a soft dark fill from above. Together they read as a slot cut into the surface. The focus ring is a fourth, outer shadow that starts transparent and turns blue on focus-visible, so it appears for keyboard users only.

CSS
.rocker {
  width: 3.1875em;
  height: 1.5em;
  border-radius: 0.75em;
  isolation: isolate;
  box-shadow:
    0 0 0 0.125em transparent,
    0 0.0625em 0.0625em var(--well-top) inset,
    0 -0.0625em 0.0625em var(--well-bottom) inset,
    0 0.25em 0.25em var(--well-depth) inset;
}

.rocker__bezel {
  position: absolute;
  inset: 0.25em;
  border-radius: 0.5em;
  background: var(--face);
  perspective: 20em;
}

.rocker__paddle {
  position: absolute;
  inset: 0;
  display: flex;
  justify-content: space-around;
  transform: translateX(var(--shift)) rotateY(var(--tilt));
  transition: transform var(--dur) var(--ease);
}

The bezel sets perspective: 20em, which turns the paddle's rotateY into a real 3D lean: the near edge gets slightly bigger and the far edge slightly smaller. 15 degrees is enough to see it without the paddle looking broken. The small translateX pushes the paddle toward the pressed side, the same way a real rocker slides a little on its pivot.

The pressed half also needs to look sunk in. Two pseudo-elements cover 45% of the paddle each, one per side, with an inset shadow from the outer edge. A mask-image gradient fades each shadow toward the centre so there is no hard line in the middle. Their opacity reads --pit-on and --pit-off, so only the pressed half is shaded.

CSS
.rocker__paddle::before {
  left: 0;
  border-radius: 0.5rem 0 0 0.5rem;
  box-shadow:
    0 1px 0.0625rem hsl(var(--hue) 10% 10% / 0.7) inset,
    0.125rem 0.25rem 0.25rem hsl(var(--hue) 10% 10% / 0.3) inset;
  mask-image: linear-gradient(-90deg, transparent, #000);
  opacity: var(--pit-on);
}
Masks only care about alpha

A mask uses the alpha of its gradient and ignores the colour, so transparent to #000 is all you need. There is no reason to repeat the background colour inside the mask, which also means the mask keeps working in dark mode without a second definition.

05
Part Five
Two Contact Shadows

Step 5 — A Sharp Ring Where It Touches, a Blur Where It Lifts

Under each half of the paddle sits a shadow span behind the button's content, at z-index -1 inside the isolated button. The half that is pressed down touches the well, so its shadow is a tight ring with no blur. The half that is lifted floats above it, so its shadow is soft and spread out. Each span is also rotated slightly so the shadow follows the angle of the paddle.

CSS
.rocker__cast {
  position: absolute;
  top: 0.375em;
  width: 1.25em;
  height: 0.75em;
  z-index: -1;
  transition:
    box-shadow var(--dur) var(--ease),
    transform var(--dur) var(--ease);
}

.rocker__cast--on {
  left: 0.375em;
  border-radius: 0.375em 0.875em 0.375em 0.375em / 0.375em 0.625em 0.125em 0.375em;
  box-shadow: var(--cast-on);
  transform: rotate(var(--rot-on));
  transform-origin: 1em 0.375em;
}

The ring and the blur are written with the same four numbers, offset x, offset y, blur and spread: 0 0 0 0.125em and 0 0 0.25em 0.125em. Because they have the same shape, the browser can interpolate between them, and the transition smoothly grows or shrinks the blur instead of snapping. The uneven border-radius on each span gives the shadow a slightly squashed, wedge-like outline that matches a paddle seen from an angle.

Why isolation: isolate

A negative z-index normally drops an element behind its parent's background, or even behind the page. isolation: isolate makes the button its own stacking context, so the shadows sit behind the bezel but still inside the button, and the button's own background stays transparent.

06
Part Six
Drawing I and O in CSS

Step 6 — The IEC Power Symbols from Two Boxes

Real power switches are marked with the IEC symbols: a vertical bar for on and a circle for off. A letter I and a letter O would come close, but their shape depends on the font. Instead each mark holds one empty span that is drawn with CSS: a thin rounded bar filled with currentColor, and a circle with a currentColor border. Both are sized in em, so they stay sharp at any size and pick up the ink colour of their side automatically.

CSS
.rocker__mark--on  { color: var(--ink-on); }
.rocker__mark--off { color: var(--ink-off); }

.rocker__mark--on .rocker__glyph {
  width: 0.14em;
  height: 0.8em;
  border-radius: 0.07em;
  background: currentColor;
}

.rocker__mark--off .rocker__glyph {
  width: 0.72em;
  height: 0.72em;
  border: 0.14em solid currentColor;
  border-radius: 50%;
}

.rocker__glyph {
  display: inline-block;
  vertical-align: middle;
  filter:
    drop-shadow(0 -1px 0 var(--emboss-dark))
    drop-shadow(0 1px 0 var(--emboss-light));
}

Text marks get their embossed look from a text-shadow with a dark line above and a light line below. A drawn box has no text to shadow, so the glyphs use two drop-shadow filters with the same offsets and colours. drop-shadow follows the actual painted shape, so the circle gets a curved highlight instead of a square one.

07
Part Seven
A Small Rocker Class

Step 7 — Toggle, Arrow Keys, and a Change Event

The script has one job: keep aria-checked in sync. All of the drawing is already done by CSS. The class stores its element in a private field, reads the state straight from the attribute so the DOM is the single source of truth, and exposes checked and toggle() for any other code that wants to drive it.

JavaScript
class Rocker {
  #el;

  constructor(el, { checked = false } = {}) {
    this.#el = el;
    this.#el.addEventListener("click", () => this.toggle());
    this.#el.addEventListener("keydown", (e) => this.#onKey(e));
    this.#apply(checked, false);
  }

  get checked() {
    return this.#el.getAttribute("aria-checked") === "true";
  }

  set checked(value) {
    this.#apply(Boolean(value), true);
  }

  toggle() {
    this.checked = !this.checked;
  }

  #onKey(e) {
    // Left side is "I" (on), right side is "O" (off)
    const map = { ArrowLeft: true, ArrowRight: false };
    if (!(e.key in map)) return;
    e.preventDefault();
    this.checked = map[e.key];
  }

  #apply(next, notify) {
    if (notify && next === this.checked) return;
    this.#el.setAttribute("aria-checked", String(next));
    if (notify) {
      this.#el.dispatchEvent(
        new CustomEvent("rocker:change", { bubbles: true, detail: { checked: next } })
      );
    }
  }
}

document.querySelectorAll("[data-rocker]").forEach((el) => new Rocker(el));

Space and Enter already fire click on a button, so they need no handler. The arrow keys are an extra: left presses the I side and right presses the O side, which matches where the marks are. Pressing the side that is already down does nothing and sends no event, because #apply returns early when the value has not changed.

Each change dispatches a bubbling rocker:change event with the new state in detail.checked. The rest of the page can listen once on document and react to any switch, without holding a reference to the class. Because the setup runs over every element with data-rocker, putting three switches on one page is just three buttons.

JavaScript
document.addEventListener("rocker:change", (e) => {
  console.log(e.detail.checked ? "Power on" : "Power off");
});

Last, a prefers-reduced-motion query sets --dur to 0.01s. Every transition in the file reads that one token, so the switch snaps between states for people who have asked for less motion, while the visuals of each state stay exactly the same.

Tuning Reference

ValueExampleEffect
--hue223Tint of every grey in the switch; try 30 for warm or 160 for mint
--tilt15degHow far the paddle leans; above 25deg starts to look broken
--shift0.125remSideways slide of the paddle toward the pressed side
perspective20emLower values exaggerate the 3D lean, higher values flatten it
--ring0 0 0 0.125emShadow under the pressed half: tight, no blur
--blur0 0 0.25em 0.125emShadow under the lifted half: soft and spread
--rot-on / --rot-off±7.5degAngle of each shadow so it follows the paddle
--dur0.3sLength of every transition; 0.01s under reduced motion
--easecubic-bezier(0.65, 0, 0.35, 1)Ease-in-out curve that gives the rock a click
Root font sizeclamp(60px, …, 120px)Overall size of the switch; everything is in em

Full Source Code

The whole project is a single self-contained HTML file with its markup, styles and script together and no build step. The only external request is the Google Fonts stylesheet for DM Sans, and the page falls back to the system font if it does not load. It needs a browser that supports CSS masks, private class fields and the space-separated hsl() syntax, which means any current Chrome, Edge, Safari or Firefox.

HTML — power-rocker-switch.html (complete)
// Full source available in the Aduok GitHub repository
// https://github.com/aduok/aduok-code-snippets/blob/main/blog/power-rocker-switch.html
Want to try it yourself?Open the live power-rocker-switch.html file