CSS Labeled Toggle Switch

A toggle switch with ON and OFF text inside the track and a visible label, built on a role="switch" checkbox.

  • toggle
  • switch
  • on off
  • role switch
  • checkbox
  • settings
HTML
<label class="tgl-text">
  <span>Wi-Fi</span>
  <input type="checkbox" role="switch" checked>
  <span class="tgl-text-track" aria-hidden="true"></span>
</label>
CSS
.tgl-text {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: #0f172a;
  font: 600 15px system-ui, sans-serif;
  cursor: pointer;
}

.tgl-text input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.tgl-text-track {
  position: relative;
  width: 64px;
  height: 30px;
  border-radius: 999px;
  background: #cbd5e1;
  transition: background 0.25s ease;
}

.tgl-text-track::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.25s ease;
}

.tgl-text-track::after {
  content: "OFF";
  position: absolute;
  top: 50%;
  right: 9px;
  color: #475569;
  font-size: 10px;
  font-weight: 800;
  transform: translateY(-50%);
}

.tgl-text input:checked + .tgl-text-track {
  background: #2563eb;
}

.tgl-text input:checked + .tgl-text-track::before {
  transform: translateX(34px);
}

.tgl-text input:checked + .tgl-text-track::after {
  content: "ON";
  right: auto;
  left: 11px;
  color: #fff;
}

.tgl-text input:focus-visible + .tgl-text-track {
  outline: 2px solid #2563eb;
  outline-offset: 3px;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-text-track,
  .tgl-text-track::before {
    transition: none;
  }
}

About this CSS Labeled Toggle Switch

This CSS Labeled Toggle Switch is 75 lines of pure CSS (1.3 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and Flexbox. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

The colors in this snippet. Search and replace them to match your brand:

  • #0f172a
  • #cbd5e1
  • #fff
  • #475569
  • #2563eb

How to use this labeled toggle switch

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .tgl-text class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback