# Day / Night Toggle

> A dark-mode switch where the sun slides across and turns into a crescent moon.

- Category: [CSS Form Elements](https://copy-css.com/category/forms)
- Tags: dark mode, theme, toggle, switch, sun, moon, checkbox
- Live preview: https://copy-css.com/snippets/theme-toggle
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<label class="theme-toggle">
  <input type="checkbox" aria-label="Dark mode">
  <span></span>
</label>
```

## CSS

```css
.theme-toggle {
  position: relative;
  display: inline-block;
  width: 64px;
  height: 34px;
}

.theme-toggle input {
  position: absolute;
  inset: 0;
  z-index: 1;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.theme-toggle span {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: #38bdf8;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15);
  transition: background 0.4s ease;
}

/* The sun */
.theme-toggle span::before {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fde047;
  box-shadow:
    inset 0 0 0 0 #f8fafc,
    0 0 10px 2px rgba(253, 224, 71, 0.6);
  transition: transform 0.4s cubic-bezier(0.4, 1.4, 0.6, 1), background 0.4s ease, box-shadow 0.4s ease;
}

.theme-toggle input:checked + span {
  background: #1e1b4b;
}

/* ...becomes the moon */
.theme-toggle input:checked + span::before {
  background: rgba(253, 224, 71, 0);
  box-shadow:
    inset -9px -3px 0 0 #f8fafc,
    0 0 10px 2px rgba(248, 250, 252, 0);
  transform: translateX(30px);
}

.theme-toggle input:focus-visible + span {
  outline: 2px solid #6366f1;
  outline-offset: 3px;
}
```

## Notes

- This CSS Day / Night Toggle is 58 lines of pure CSS (1.1 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and the :checked state (no JavaScript).
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
