Skip to content

OTP Inputs

A copy-paste toggles & inputs component in pure HTML, CSS & vanilla JS. Zero dependencies, framework-agnostic, MIT-licensed.

Toggles & InputsHTMLCSSJavaScriptany framework

Copy into your project

HTML
<!-- OTP Inputs — four single-digit boxes that glow + advance on entry -->
<div class="nuda-otp" role="group" aria-label="One-time code">
  <input class="nuda-otp__box" type="text" inputmode="numeric" maxlength="1" aria-label="Digit 1" />
  <input class="nuda-otp__box" type="text" inputmode="numeric" maxlength="1" aria-label="Digit 2" />
  <input class="nuda-otp__box" type="text" inputmode="numeric" maxlength="1" aria-label="Digit 3" />
  <input class="nuda-otp__box" type="text" inputmode="numeric" maxlength="1" aria-label="Digit 4" />
</div>
CSS
/* ── OTP Inputs ──────────────────────────────────────────────
   Each box glows and lifts on focus.
   Customize: --nuda-otp-clr for the focus accent.
   ──────────────────────────────────────────────────────────── */
.nuda-otp {
  --nuda-otp-clr: #e4ff54;
  display: inline-flex;
  gap: 10px;
}

.nuda-otp__box {
  width: 46px;
  height: 54px;
  text-align: center;
  background: #0c0c10;
  color: #fafafa;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  font-size: 20px;
  font-weight: 700;
  outline: none;
  transition: border-color 0.25s ease, box-shadow 0.25s ease,
    transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.nuda-otp__box:focus {
  border-color: var(--nuda-otp-clr);
  box-shadow: 0 0 0 3px rgba(228, 255, 84, 0.2);
  transform: translateY(-2px);
}

/* ── Accessibility ──────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .nuda-otp__box {
    transition: border-color 0.25s ease, box-shadow 0.25s ease;
  }
}
JavaScript
/* ── OTP Inputs — vanilla JS ─────────────────────────────────
   Auto-advances on entry and steps back on Backspace.
   ──────────────────────────────────────────────────────────── */
(function () {
  document.querySelectorAll(".nuda-otp").forEach(function (group) {
    var boxes = group.querySelectorAll(".nuda-otp__box");
    boxes.forEach(function (box, i) {
      box.addEventListener("input", function () {
        box.value = box.value.replace(/[^0-9]/g, "").slice(0, 1);
        if (box.value && boxes[i + 1]) boxes[i + 1].focus();
      });
      box.addEventListener("keydown", function (e) {
        if (e.key === "Backspace" && !box.value && boxes[i - 1]) {
          boxes[i - 1].focus();
        }
      });
    });
  });
})();

How to use OTP Inputs

Paste the HTML where you need it and the CSS into a global stylesheet (or a <style> tag). Every class is prefixed nuda- so it never collides with Tailwind or your own styles. Tweak the CSS custom properties to match your design system.

Works in React, Vue, Svelte, Astro, Next.js, Nuxt, Laravel Blade, Django, Rails — or a single .html file. No npm install, no build step.

More toggles & inputs components

← Browse all NudaUI components