Home / Learn / How do you design an animated toggle switch that works in Arabic and English?

How do you design an animated toggle switch that works in Arabic and English?

A toggle is a tiny element the user touches every day, so it earns a smooth, deliberate motion. Here we build it with logical properties that flip automatically with the page direction.

Steps

  1. Use a real checkbox inside a label; you get keyboard support and accessibility for free.
  2. Build the track and knob from two spans, positioning the knob with inset-inline-start instead of left.
  3. On :checked, move the knob with translateX, with a mirrored direction inside [dir="rtl"].
  4. Change the track color with the same timing, so the state reads from color and position together.
  5. Under prefers-reduced-motion, remove the transitions so the switch changes instantly.

The code

HTML

<label class="toggle">
  <input type="checkbox" id="modeToggle">
  <span class="toggle-track"><span class="toggle-knob"></span></span>
  <span class="toggle-label">الوضع الليلي</span>
</label>

<style>
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}
.toggle input {
  position: absolute;
  opacity: 0;
}
.toggle-track {
  position: relative;
  width: 52px;
  height: 28px;
  border-radius: 999px;
  background: #cbd5e1;
  transition: background 0.3s ease;
}
.toggle-knob {
  position: absolute;
  top: 3px;
  inset-inline-start: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  transition: transform 0.3s cubic-bezier(.22, .61, .36, 1);
}
.toggle input:checked + .toggle-track {
  background: #6d5ef0;
}
.toggle input:checked + .toggle-track .toggle-knob {
  transform: translateX(24px);
}
[dir="rtl"] .toggle input:checked + .toggle-track .toggle-knob {
  transform: translateX(-24px);
}
.toggle input:focus-visible + .toggle-track {
  outline: 2px solid #6d5ef0;
  outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
  .toggle-track, .toggle-knob { transition: none; }
}
</style>

RTL & Arabic notes: inset-inline-start places the knob at the start edge in any direction, and the [dir="rtl"] rule mirrors the slide direction so the design never breaks in Arabic.

Didn't find the motion you need? Request it from the Vellox team.