OTP Input Field in HTML and CSS: 5 Verification Code Inputs

Updated 2026-10-28. 5 inputs.

One-time code fields usually show one box per digit, move focus forward as each digit is typed, step back on Backspace and fill every box when a whole code is pasted. Add autocomplete one-time-code and inputmode numeric so phones offer the code from an SMS, and label the group for screen readers.

The six-box layout is the most familiar and suits most login and signup checks. The grouped version splits those six boxes into two sets of three with a resend timer underneath, which is easier to read back from a text message. A four-digit PIN with masked dots belongs to things like a card PIN or an app lock, where the code should not be visible over someone's shoulder.

If you care most about autofill and password managers, use the single-field version. It is one real input with wide letter spacing drawn to look like boxes, so the browser and the phone keyboard treat it exactly like any other field. The version with error and success states shows how to reject a wrong code clearly with a short shake, a message and cleared boxes, and how to confirm a right one.

Compare them

VariantBuilt withSize (gzipped)Running costBrowser support
Six Box OTP Input With Auto Advance and PasteCSS and a small script2 KBSix inputs with input, keydown and paste handlers; nothing runs while idle.All current browsers
Four Digit PIN Input With Masked DotsCSS and a small script1.8 KBFour password inputs and a show toggle; work only happens on key presses.All current browsers
Grouped OTP Input With Separator and Resend TimerCSS and a small script2.2 KBA single one-second timeout drives the countdown and stops at zero.All current browsers
OTP Input With Shake Error and Success StatesCSS only2 KBA 380 ms transform keyframe on one row, played only after a wrong code.All current browsers
Single Field OTP Input With Letter SpacingCSS and a small script1.6 KBOne ordinary input; the drawn boxes are six spans that change class on typing.All current browsers

Six Box OTP Input With Auto Advance and Paste

A phone check card with six digit boxes. Each digit typed moves the caret to the next box, Backspace in an empty box clears the one before it, the arrow keys step left and right, and pasting 482917 into any box spreads it across the row. The button counts how many digits are missing and becomes Verify once the code is whole. On a phone the boxes narrow slightly so all six fit at 360 pixels.

<section class="sbox">
  <form class="sbox-card" novalidate>
    <div class="sbox-icon" aria-hidden="true">
      <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="6" y="2" width="12" height="20" rx="2.5"/><path d="M10.5 18h3"/></svg>
    </div>
    <h2 class="sbox-title">Check your phone</h2>
    <p class="sbox-sub">We sent a 6-digit code to <strong>+44 7700 900 182</strong></p>
    <fieldset class="sbox-set">
      <legend class="sbox-sr">Verification code</legend>
      <div class="sbox-row">
        <input class="sbox-digit" inputmode="numeric" autocomplete="one-time-code" pattern="[0-9]*" maxlength="6" aria-label="Digit 1 of 6" value="4">
        <input class="sbox-digit" inputmode="numeric" autocomplete="off" pattern="[0-9]*" maxlength="6" aria-label="Digit 2 of 6" value="8">
        <input class="sbox-digit" inputmode="numeric" autocomplete="off" pattern="[0-9]*" maxlength="6" aria-label="Digit 3 of 6" value="2">
        <input class="sbox-digit" inputmode="numeric" autocomplete="off" pattern="[0-9]*" maxlength="6" aria-label="Digit 4 of 6">
        <input class="sbox-digit" inputmode="numeric" autocomplete="off" pattern="[0-9]*" maxlength="6" aria-label="Digit 5 of 6">
        <input class="sbox-digit" inputmode="numeric" autocomplete="off" pattern="[0-9]*" maxlength="6" aria-label="Digit 6 of 6">
      </div>
    </fieldset>
    <input type="hidden" name="code">
    <button type="submit" class="sbox-btn" disabled>Enter 3 more digits</button>
    <p class="sbox-foot">Didn&rsquo;t get it? <a href="#">Send a new code</a></p>
    <p class="sbox-sr" aria-live="polite"></p>
  </form>
</section>
<style>
  .sbox { --sbox-accent: #4f46e5; background: linear-gradient(#eef2ff, #fff); padding: 72px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
  .sbox-card { max-width: 28rem; margin: 0 auto; padding: 32px 24px; text-align: center; background: #fff; border: 1px solid #e0e7ff; border-radius: 16px; box-shadow: 0 20px 50px -24px rgba(67,56,202,.35); }
  .sbox-icon { display: grid; place-items: center; width: 48px; height: 48px; margin: 0 auto; border-radius: 12px; background: var(--sbox-accent); color: #fff; }
  .sbox-title { margin: 16px 0 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
  .sbox-sub { margin: 4px 0 0; font-size: 14px; color: #475569; }
  .sbox-sub strong { color: #1e293b; font-weight: 500; }
  .sbox-set { margin: 24px 0 0; padding: 0; border: 0; }
  .sbox-row { display: flex; justify-content: center; gap: 8px; }
  .sbox-digit { width: 44px; height: 56px; padding: 0; border: 1px solid #cbd5e1; border-radius: 12px; background: #f8fafc; color: #0f172a; font: 600 24px ui-sans-serif, system-ui, sans-serif; text-align: center; font-variant-numeric: tabular-nums; caret-color: var(--sbox-accent); }
  .sbox-digit:not(:placeholder-shown), .sbox-digit[data-filled] { background: #fff; border-color: #94a3b8; }
  .sbox-digit:focus { outline: 0; border-color: var(--sbox-accent); box-shadow: 0 0 0 4px #e0e7ff; }
  .sbox-btn { width: 100%; height: 44px; margin-top: 24px; border: 0; border-radius: 12px; background: var(--sbox-accent); color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .sbox-btn:disabled { background: #c7d2fe; color: #312e81; cursor: default; }
  .sbox-btn:focus-visible, .sbox-foot a:focus-visible { outline: 2px solid var(--sbox-accent); outline-offset: 2px; }
  .sbox-foot { margin: 16px 0 0; font-size: 14px; color: #475569; }
  .sbox-foot a { color: #4338ca; font-weight: 500; text-decoration: none; }
  .sbox-foot a:hover { text-decoration: underline; }
  .sbox-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  @media (min-width: 640px) { .sbox-row { gap: 12px; } .sbox-digit { width: 48px; } }
</style>
<script>
  (function () {
    var root = document.querySelector(".sbox");
    if (!root) return;
    var boxes = [].slice.call(root.querySelectorAll(".sbox-digit"));
    var btn = root.querySelector(".sbox-btn"), hidden = root.querySelector("input[name=code]"), live = root.querySelector(".sbox-sr[aria-live]");
    var N = boxes.length;

    function focusAt(i) { boxes[Math.max(0, Math.min(N - 1, i))].focus(); }
    function sync() {
      var code = boxes.map(function (b) { return b.value; }).join("");
      boxes.forEach(function (b) { b.toggleAttribute("data-filled", !!b.value); });
      hidden.value = code;
      var left = N - code.length;
      btn.disabled = left > 0;
      btn.textContent = left ? "Enter " + left + " more " + (left === 1 ? "digit" : "digits") : "Verify";
      live.textContent = left ? "" : "All six digits entered";
    }
    function fill(start, text) {
      text.split("").slice(0, N - start).forEach(function (c, k) { boxes[start + k].value = c; });
      focusAt(start + text.length); sync();
    }
    boxes.forEach(function (box, i) {
      box.addEventListener("input", function () {
        var only = box.value.replace(/\D/g, "");
        if (only.length > 1) return fill(i, only);
        box.value = only;
        if (only) focusAt(i + 1);
        sync();
      });
      box.addEventListener("keydown", function (e) {
        if (e.key === "Backspace" && !box.value && i > 0) { e.preventDefault(); boxes[i - 1].value = ""; focusAt(i - 1); sync(); }
        else if (e.key === "ArrowLeft") { e.preventDefault(); focusAt(i - 1); }
        else if (e.key === "ArrowRight") { e.preventDefault(); focusAt(i + 1); }
      });
      box.addEventListener("paste", function (e) { e.preventDefault(); fill(i, e.clipboardData.getData("text").replace(/\D/g, "")); });
      box.addEventListener("focus", function () { box.select(); });
    });
    root.querySelector("form").addEventListener("submit", function (e) { e.preventDefault(); });
    sync();
  })();
</script>

With reduced motion: Nothing animates apart from a quick border colour change as each box fills.

Running cost: Six inputs with input, keydown and paste handlers; nothing runs while idle.

  • Change `LENGTH` for four or eight digit codes; the plain version follows however many `.sbox-digit` inputs you write.
  • Swap the indigo through `bg-indigo-600`, `focus:border-indigo-600` and `caret-indigo-600`, or `--sbox-accent` in the CSS.
  • Allow letters for alphanumeric codes by changing the `\D` pattern to `[^0-9a-z]` with the i flag and removing `inputMode="numeric"`.
  • Make the boxes larger with `h-14 w-11` to `h-16 w-14` on roomy layouts, keeping the text size in step.

Full page for Six Box OTP Input With Auto Advance and Paste

Four Digit PIN Input With Masked Dots

A card PIN prompt on a dark banking screen: four round password fields that show masked dots, an amber ring around each filled one and four small bars underneath that light as digits go in. A Show digits button swaps the fields to plain text for people who want to check what they typed, and paste still fills all four.

<section class="fpin">
  <div class="fpin-card">
    <div class="fpin-icon" aria-hidden="true">
      <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="4" y="10" width="16" height="11" rx="2.5"/><path d="M8 10V7a4 4 0 0 1 8 0v3"/></svg>
    </div>
    <h2 class="fpin-title">Enter your card PIN</h2>
    <p class="fpin-sub">Debit card ending 4417</p>
    <fieldset class="fpin-set">
      <legend class="fpin-sr">Four digit PIN</legend>
      <div class="fpin-row">
        <input class="fpin-d" type="password" inputmode="numeric" autocomplete="off" maxlength="4" aria-label="PIN digit 1 of 4" value="7">
        <input class="fpin-d" type="password" inputmode="numeric" autocomplete="off" maxlength="4" aria-label="PIN digit 2 of 4" value="3">
        <input class="fpin-d" type="password" inputmode="numeric" autocomplete="off" maxlength="4" aria-label="PIN digit 3 of 4">
        <input class="fpin-d" type="password" inputmode="numeric" autocomplete="off" maxlength="4" aria-label="PIN digit 4 of 4">
      </div>
    </fieldset>
    <div class="fpin-bars" aria-hidden="true"><span></span><span></span><span></span><span></span></div>
    <p class="fpin-left" aria-live="polite"></p>
    <button type="button" class="fpin-show" aria-pressed="false">Show digits</button>
  </div>
</section>
<style>
  .fpin { --fpin-accent: #fbbf24; background: #09090b; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #fff; }
  .fpin-card { max-width: 24rem; margin: 0 auto; padding: 40px 24px; text-align: center; background: #18181b; border: 1px solid #27272a; border-radius: 28px; }
  .fpin-icon { display: grid; place-items: center; width: 48px; height: 48px; margin: 0 auto; border-radius: 50%; background: rgba(251,191,36,.1); color: #fcd34d; }
  .fpin-title { margin: 16px 0 0; font-size: 18px; font-weight: 600; }
  .fpin-sub { margin: 4px 0 0; font-size: 14px; color: #d4d4d8; }
  .fpin-set { margin: 32px 0 0; padding: 0; border: 0; }
  .fpin-row { display: flex; justify-content: center; gap: 16px; }
  .fpin-d { width: 56px; height: 56px; padding: 0; border: 2px solid #3f3f46; border-radius: 50%; background: #09090b; color: #fff; font: 600 30px ui-sans-serif, system-ui, sans-serif; text-align: center; caret-color: #fcd34d; }
  .fpin-d[data-filled] { border-color: rgba(251,191,36,.7); }
  .fpin-d:focus { outline: 0; border-color: #fcd34d; box-shadow: 0 0 0 4px rgba(252,211,77,.2); }
  .fpin-bars { display: flex; justify-content: space-between; max-width: 12rem; margin: 24px auto 0; }
  .fpin-bars span { width: 36px; height: 4px; border-radius: 999px; background: #3f3f46; }
  .fpin-bars span[data-on] { background: var(--fpin-accent); }
  .fpin-left { margin: 12px 0 0; font-size: 14px; color: #d4d4d8; }
  .fpin-show { height: 40px; margin-top: 24px; padding: 0 16px; border: 1px solid #3f3f46; border-radius: 999px; background: none; color: #e4e4e7; font: 500 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .fpin-show:hover { background: #27272a; }
  .fpin-show:focus-visible { outline: 2px solid #fcd34d; outline-offset: 2px; }
  .fpin-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
</style>
<script>
  (function () {
    var root = document.querySelector(".fpin");
    if (!root) return;
    var boxes = [].slice.call(root.querySelectorAll(".fpin-d")), bars = root.querySelectorAll(".fpin-bars span");
    var left = root.querySelector(".fpin-left"), show = root.querySelector(".fpin-show"), N = boxes.length;

    function focusAt(i) { boxes[Math.max(0, Math.min(N - 1, i))].focus(); }
    function sync() {
      var n = 0;
      boxes.forEach(function (b, i) { var on = !!b.value; if (on) n++; b.toggleAttribute("data-filled", on); bars[i].toggleAttribute("data-on", on); });
      left.textContent = n === N ? "PIN complete" : (N - n) + " digits to go";
    }
    function put(start, text) {
      text.split("").slice(0, N - start).forEach(function (c, k) { boxes[start + k].value = c; });
      focusAt(start + text.length); sync();
    }
    boxes.forEach(function (b, i) {
      b.addEventListener("input", function () {
        var only = b.value.replace(/\D/g, "");
        if (only.length > 1) return put(i, only);
        b.value = only; if (only) focusAt(i + 1); sync();
      });
      b.addEventListener("keydown", function (e) {
        if (e.key === "Backspace" && !b.value && i > 0) { e.preventDefault(); boxes[i - 1].value = ""; focusAt(i - 1); sync(); }
      });
      b.addEventListener("paste", function (e) { e.preventDefault(); put(i, e.clipboardData.getData("text").replace(/\D/g, "")); });
    });
    show.addEventListener("click", function () {
      var on = show.getAttribute("aria-pressed") !== "true";
      show.setAttribute("aria-pressed", on);
      show.textContent = on ? "Hide digits" : "Show digits";
      boxes.forEach(function (b) { b.type = on ? "text" : "password"; });
    });
    sync();
  })();
</script>

With reduced motion: Nothing moves; borders and the bars under the circles change colour only.

Running cost: Four password inputs and a show toggle; work only happens on key presses.

  • Change `LENGTH` and add or remove inputs in the plain version for a six digit PIN.
  • Recolour the rings and bars with the `amber-300` and `amber-400` classes, or `--fpin-accent`.
  • Remove the Show digits button for a stricter checkout flow where the PIN must never be visible.

Grouped OTP Input With Separator and Resend Timer

A patient portal check on warm paper tones. The six digits sit in two joined trays of three with a short dash between them, which matches how people read a code aloud. Under the Continue button a monospace countdown and a thin bar run down from thirty seconds, then turn into a Send a new code link that clears the boxes and restarts the clock.

<section class="gotp" data-wait="30">
  <div class="gotp-wrap">
    <p class="gotp-kicker">Fernway Health</p>
    <h2 class="gotp-title">Confirm it&rsquo;s you</h2>
    <p class="gotp-sub">Enter the code from the text we sent to the number ending 52. It works for ten minutes.</p>
    <fieldset class="gotp-set">
      <legend class="gotp-sr">Six digit code, in two groups of three</legend>
      <div class="gotp-row">
        <div class="gotp-group">
          <input class="gotp-d" inputmode="numeric" autocomplete="one-time-code" maxlength="6" aria-label="Digit 1 of 6" value="4">
          <input class="gotp-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 2 of 6" value="1">
          <input class="gotp-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 3 of 6" value="8">
        </div>
        <span class="gotp-dash" aria-hidden="true"></span>
        <div class="gotp-group">
          <input class="gotp-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 4 of 6">
          <input class="gotp-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 5 of 6">
          <input class="gotp-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 6 of 6">
        </div>
      </div>
    </fieldset>
    <div class="gotp-actions">
      <button type="button" class="gotp-go" disabled>Continue</button>
      <p class="gotp-wait">New code in <b class="gotp-time">0:30</b></p>
      <button type="button" class="gotp-resend" hidden>Send a new code</button>
    </div>
    <div class="gotp-meter" aria-hidden="true"><div class="gotp-fill"></div></div>
    <p class="gotp-sr" aria-live="polite"></p>
  </div>
</section>
<style>
  .gotp { --gotp-accent: #065f46; background: #f3f1ea; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #1c1917; }
  .gotp-wrap { max-width: 28rem; margin: 0 auto; }
  .gotp-kicker { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .2em; text-transform: uppercase; color: #065f46; }
  .gotp-title { margin: 8px 0 0; font-size: 24px; font-weight: 600; letter-spacing: -.02em; }
  .gotp-sub { margin: 8px 0 0; font-size: 14px; color: #44403c; }
  .gotp-set { margin: 24px 0 0; padding: 0; border: 0; }
  .gotp-row { display: flex; align-items: center; gap: 12px; }
  .gotp-group { display: flex; overflow: hidden; border: 1px solid rgba(2,44,34,.25); border-radius: 8px; background: rgba(255,255,255,.6); }
  .gotp-d { width: 40px; height: 48px; padding: 0; border: 0; border-left: 1px solid rgba(2,44,34,.15); background: transparent; color: #022c22; font: 600 20px ui-monospace, Menlo, monospace; text-align: center; caret-color: var(--gotp-accent); }
  .gotp-d:first-child { border-left: 0; }
  .gotp-d:focus { outline: 0; background: #fff; box-shadow: inset 0 0 0 2px #059669; }
  .gotp-dash { width: 16px; height: 2px; border-radius: 2px; background: #a8a29e; }
  .gotp-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 16px; margin-top: 24px; }
  .gotp-go { height: 44px; padding: 0 24px; border: 0; border-radius: 8px; background: var(--gotp-accent); color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .gotp-go:disabled { background: #d6d3d1; color: #44403c; cursor: default; }
  .gotp-wait { margin: 0; font-size: 14px; color: #44403c; }
  .gotp-time { font-family: ui-monospace, Menlo, monospace; color: #1c1917; font-variant-numeric: tabular-nums; }
  .gotp-resend { height: 40px; padding: 0 8px; border: 0; background: none; color: var(--gotp-accent); font: 600 14px ui-sans-serif, system-ui, sans-serif; text-decoration: underline; text-underline-offset: 4px; cursor: pointer; }
  .gotp-go:focus-visible, .gotp-resend:focus-visible { outline: 2px solid #047857; outline-offset: 2px; }
  .gotp-meter { width: 160px; height: 4px; margin-top: 12px; border-radius: 999px; background: #d6d3d1; overflow: hidden; }
  .gotp-fill { height: 100%; border-radius: 999px; background: #047857; transform-origin: left; transition: transform 1s linear; }
  .gotp-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  @media (min-width: 640px) { .gotp-d { width: 48px; height: 56px; } }
  @media (prefers-reduced-motion: reduce) { .gotp-fill { transition: none; } }
</style>
<script>
  (function () {
    var root = document.querySelector(".gotp");
    if (!root) return;
    var WAIT = Number(root.getAttribute("data-wait")) || 30, secs = WAIT, timer = 0;
    var boxes = [].slice.call(root.querySelectorAll(".gotp-d")), N = boxes.length;
    var go = root.querySelector(".gotp-go"), wait = root.querySelector(".gotp-wait"), time = root.querySelector(".gotp-time");
    var resend = root.querySelector(".gotp-resend"), fill = root.querySelector(".gotp-fill"), live = root.querySelector(".gotp-sr[aria-live]");

    function focusAt(i) { boxes[Math.max(0, Math.min(N - 1, i))].focus(); }
    function sync() { go.disabled = boxes.some(function (b) { return !b.value; }); }
    function put(start, text) {
      text.split("").slice(0, N - start).forEach(function (c, k) { boxes[start + k].value = c; });
      focusAt(start + text.length); sync();
    }
    function tick() {
      time.textContent = "0:" + String(secs).padStart(2, "0");
      fill.style.transform = "scaleX(" + secs / WAIT + ")";
      var over = secs <= 0;
      wait.hidden = over; resend.hidden = !over;
      if (over) { live.textContent = "You can request a new code now"; return; }
      secs -= 1; timer = setTimeout(tick, 1000);
    }
    boxes.forEach(function (b, i) {
      b.addEventListener("input", function () {
        var only = b.value.replace(/\D/g, "");
        if (only.length > 1) return put(i, only);
        b.value = only; if (only) focusAt(i + 1); sync();
      });
      b.addEventListener("keydown", function (e) {
        if (e.key === "Backspace" && !b.value && i > 0) { e.preventDefault(); boxes[i - 1].value = ""; focusAt(i - 1); sync(); }
      });
      b.addEventListener("paste", function (e) { e.preventDefault(); put(i, e.clipboardData.getData("text").replace(/\D/g, "")); });
    });
    resend.addEventListener("click", function () {
      boxes.forEach(function (b) { b.value = ""; });
      live.textContent = "A new code is on its way";
      clearTimeout(timer); secs = WAIT; sync(); tick(); focusAt(0);
    });
    tick();
  })();
</script>

With reduced motion: The countdown bar steps down each second instead of gliding.

Running cost: A single one-second timeout drives the countdown and stops at zero.

  • Set `WAIT` in React, or `data-wait` on the section, to change how long people wait between codes.
  • Split the row differently, for example 2-2-2, by changing the index arrays passed to `box` and adding a second dash.
  • Replace the paper background `#f3f1ea` and the `emerald-800` button with your own brand colours.

OTP Input With Shake Error and Success States

An email check that shows what happens after Verify. A wrong code makes the row give a short sideways shake, turns every box rose, clears the digits and counts down the tries left; the right code turns the boxes green and locks them. The demo opens on a wrong attempt so both colours and the message are visible straight away.

<section class="oss" data-demo-code="260418">
  <form class="oss-card" data-status="error">
    <h2 class="oss-title">Verify your email</h2>
    <p class="oss-sub">We emailed a code to [email protected]. Try 260418 in this demo.</p>
    <fieldset class="oss-set">
      <legend class="oss-sr">Verification code</legend>
      <div class="oss-row">
        <input class="oss-d" inputmode="numeric" autocomplete="one-time-code" maxlength="6" aria-label="Digit 1 of 6" aria-describedby="oss-msg" value="2">
        <input class="oss-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 2 of 6" aria-describedby="oss-msg" value="6">
        <input class="oss-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 3 of 6" aria-describedby="oss-msg" value="0">
        <input class="oss-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 4 of 6" aria-describedby="oss-msg" value="4">
        <input class="oss-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 5 of 6" aria-describedby="oss-msg" value="1">
        <input class="oss-d" inputmode="numeric" autocomplete="off" maxlength="6" aria-label="Digit 6 of 6" aria-describedby="oss-msg" value="7">
      </div>
    </fieldset>
    <p id="oss-msg" class="oss-msg" role="status">That code didn&rsquo;t match. 4 tries left.</p>
    <button type="submit" class="oss-btn">Verify code</button>
  </form>
</section>
<style>
  .oss { background: #fff; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
  .oss-card { max-width: 28rem; margin: 0 auto; padding: 28px; border: 1px solid #e2e8f0; border-radius: 24px; }
  .oss-title { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
  .oss-sub { margin: 4px 0 0; font-size: 14px; color: #475569; }
  .oss-set { margin: 24px 0 0; padding: 0; border: 0; min-width: 0; }
  .oss-row { display: flex; gap: 8px; }
  .oss-d { flex: 1; min-width: 0; height: 56px; padding: 0; border: 2px solid #cbd5e1; border-radius: 12px; background: #fff; color: #0f172a; font: 600 20px ui-sans-serif, system-ui, sans-serif; text-align: center; font-variant-numeric: tabular-nums; transition: border-color .15s, background-color .15s; }
  .oss-d:focus { outline: 0; border-color: #0f172a; box-shadow: 0 0 0 4px #e2e8f0; }
  [data-status="error"] .oss-d { border-color: #f43f5e; background: #fff1f2; color: #881337; }
  [data-status="success"] .oss-d { border-color: #10b981; background: #ecfdf5; color: #064e3b; }
  [data-status="error"] .oss-row { animation: oss-shake 380ms cubic-bezier(.36,.07,.19,.97) both; }
  @keyframes oss-shake { 0%,100% { transform: translateX(0) } 20% { transform: translateX(-8px) } 40% { transform: translateX(7px) } 60% { transform: translateX(-5px) } 80% { transform: translateX(3px) } }
  .oss-msg { min-height: 24px; margin: 12px 0 0; font-size: 14px; font-weight: 500; color: #047857; }
  [data-status="error"] .oss-msg { color: #be123c; }
  .oss-btn { width: 100%; height: 44px; margin-top: 16px; border: 0; border-radius: 12px; background: #0f172a; color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .oss-btn:disabled { background: #e2e8f0; color: #334155; cursor: default; }
  .oss-btn:focus-visible { outline: 2px solid #0f172a; outline-offset: 2px; }
  .oss-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  @media (prefers-reduced-motion: reduce) { [data-status="error"] .oss-row { animation: none; } .oss-d { transition: none; } }
</style>
<script>
  (function () {
    var root = document.querySelector(".oss");
    if (!root) return;
    var DEMO = root.getAttribute("data-demo-code"), tries = 1;
    var form = root.querySelector(".oss-card"), row = root.querySelector(".oss-row"), msg = root.querySelector(".oss-msg"), btn = root.querySelector(".oss-btn");
    var boxes = [].slice.call(root.querySelectorAll(".oss-d")), N = boxes.length;

    function code() { return boxes.map(function (b) { return b.value; }).join(""); }
    function setStatus(s) {
      form.setAttribute("data-status", s);
      boxes.forEach(function (b) { if (s === "error") b.setAttribute("aria-invalid", "true"); else b.removeAttribute("aria-invalid"); b.readOnly = s === "success"; });
      if (s === "idle") msg.textContent = "";
      btn.disabled = code().length < N || s === "success";
    }
    function focusAt(i) { boxes[Math.max(0, Math.min(N - 1, i))].focus(); }
    function put(start, text) {
      text.split("").slice(0, N - start).forEach(function (c, k) { boxes[start + k].value = c; });
      setStatus("idle"); focusAt(start + text.length);
    }
    boxes.forEach(function (b, i) {
      b.addEventListener("input", function () {
        var only = b.value.replace(/\D/g, "");
        if (only.length > 1) return put(i, only);
        b.value = only; setStatus("idle"); if (only) focusAt(i + 1);
      });
      b.addEventListener("keydown", function (e) {
        if (e.key === "Backspace" && !b.value && i > 0) { e.preventDefault(); boxes[i - 1].value = ""; focusAt(i - 1); setStatus("idle"); }
      });
      b.addEventListener("paste", function (e) { e.preventDefault(); put(i, e.clipboardData.getData("text").replace(/\D/g, "")); });
    });
    form.addEventListener("submit", function (e) {
      e.preventDefault();
      if (code() === DEMO) { msg.textContent = "Email verified. You’re all set."; btn.textContent = "Verified"; setStatus("success"); return; }
      tries += 1;
      msg.textContent = "That code didn’t match. " + Math.max(0, 5 - tries) + " tries left.";
      boxes.forEach(function (b) { b.value = ""; });
      form.setAttribute("data-status", "idle");
      void row.offsetWidth; /* restart the shake */
      setStatus("error"); focusAt(0);
    });
    setStatus("error");
  })();
</script>

With reduced motion: The row does not shake; the boxes turn rose and the message explains the error.

Running cost: A 380 ms transform keyframe on one row, played only after a wrong code.

  • Replace the `code === DEMO_CODE` check in `verify` with a call to your server and set the status from its answer.
  • Tune the shake in `otp-shake-x`: smaller pixel values feel calmer, and 380 ms is long enough to notice without feeling slow.
  • Change the number of allowed tries in the message from 5 to whatever your back end enforces.

Single Field OTP Input With Letter Spacing

An authenticator sign-in step that looks like six boxes but is one real input. Wide monospace letter spacing places each digit in the centre of a drawn box behind the field, and the next empty box is outlined in sky blue. Because it is a single field with autocomplete one-time-code, phones, browsers and password managers fill it without any special code.

<section class="sfo">
  <form class="sfo-card">
    <h2 class="sfo-title">Two-step sign in</h2>
    <p class="sfo-sub">Open your authenticator app and type the code shown for Tidewater Books.</p>
    <label for="sfo-code" class="sfo-label">6-digit code</label>
    <div class="sfo-field">
      <!-- the boxes are drawn behind one real input -->
      <div class="sfo-cells" aria-hidden="true"><span></span><span></span><span></span><span></span><span></span><span></span></div>
      <input id="sfo-code" class="sfo-input" name="code" inputmode="numeric" autocomplete="one-time-code" pattern="\d{6}" maxlength="6" required spellcheck="false" aria-describedby="sfo-hint" value="90">
    </div>
    <p id="sfo-hint" class="sfo-hint">Codes refresh every 30 seconds. Password managers can fill this field.</p>
    <button type="submit" class="sfo-btn" disabled>Sign in</button>
    <p class="sfo-status" role="status"></p>
  </form>
</section>
<style>
  /* --sfo-cell is one digit box in ch; spacing and padding are worked out from it. */
  .sfo { --sfo-cell: 2.2; --sfo-gap: .4; background: #f0f9ff; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
  .sfo-card { max-width: 24rem; margin: 0 auto; padding: 28px; background: #fff; border-radius: 16px; box-shadow: 0 0 0 1px #e0f2fe, 0 1px 2px rgba(0,0,0,.05); }
  .sfo-title { margin: 0; font-size: 18px; font-weight: 600; }
  .sfo-sub { margin: 4px 0 0; font-size: 14px; color: #475569; }
  .sfo-label { display: block; margin-top: 24px; font-size: 14px; font-weight: 500; }
  .sfo-field { position: relative; width: calc((6 * var(--sfo-cell) + 5 * var(--sfo-gap)) * 1ch); margin-top: 8px; overflow: hidden; border-radius: 8px; font: 600 24px ui-monospace, Menlo, monospace; }
  .sfo-field:focus-within { box-shadow: 0 0 0 4px #fff, 0 0 0 6px #0284c7; }
  .sfo-cells { position: absolute; inset: 0; display: flex; gap: calc(var(--sfo-gap) * 1ch); pointer-events: none; }
  .sfo-cells span { width: calc(var(--sfo-cell) * 1ch); border: 2px solid #e2e8f0; border-radius: 8px; background: #f8fafc; }
  .sfo-cells span[data-on] { border-color: #cbd5e1; background: #fff; }
  .sfo-cells span[data-next] { border-color: #0ea5e9; background: #f0f9ff; }
  .sfo-input { position: relative; display: block; width: calc((6 * (var(--sfo-cell) + var(--sfo-gap)) + 1) * 1ch); height: 56px; padding: 0 0 0 calc((var(--sfo-cell) - 1) / 2 * 1ch); border: 0; outline: 0; background: transparent; color: #0f172a; font: inherit; letter-spacing: calc((var(--sfo-cell) + var(--sfo-gap) - 1) * 1ch); caret-color: transparent; }
  .sfo-hint { margin: 8px 0 0; font-size: 12px; color: #475569; }
  .sfo-btn { width: 100%; height: 44px; margin-top: 24px; border: 0; border-radius: 12px; background: #0369a1; color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .sfo-btn:disabled { background: #e2e8f0; color: #334155; cursor: default; }
  .sfo-btn:focus-visible { outline: 2px solid #0369a1; outline-offset: 2px; }
  .sfo-status { min-height: 20px; margin: 12px 0 0; text-align: center; font-size: 14px; font-weight: 500; color: #047857; }
</style>
<script>
  (function () {
    var root = document.querySelector(".sfo");
    if (!root) return;
    var input = root.querySelector(".sfo-input"), cells = root.querySelectorAll(".sfo-cells span");
    var btn = root.querySelector(".sfo-btn"), status = root.querySelector(".sfo-status");
    function sync() {
      input.value = input.value.replace(/\D/g, "").slice(0, 6);
      var n = input.value.length;
      cells.forEach(function (c, i) { c.toggleAttribute("data-on", i < n); c.toggleAttribute("data-next", i === n); });
      btn.disabled = n < 6;
      status.textContent = "";
    }
    input.addEventListener("input", sync);
    root.querySelector("form").addEventListener("submit", function (e) {
      e.preventDefault();
      if (input.value.length === 6) status.textContent = "Code accepted, signing you in";
    });
    sync();
  })();
</script>

With reduced motion: No motion at all; the next empty box just takes a sky blue border.

Running cost: One ordinary input; the drawn boxes are six spans that change class on typing.

  • Change `CELL` in React or `--sfo-cell` in CSS to make the boxes wider; spacing and padding are calculated from it.
  • Keep the font monospace: the letter spacing assumes every digit is exactly 1ch wide.
  • Remove the `caret-transparent` class if you would rather show the browser caret than rely on the outlined box.

Performance notes

Code inputs cost almost nothing at run time: a handful of inputs, one keydown and one paste handler each. Focus moves with a direct focus call on the next element, which does not trigger layout work beyond the caret itself. The resend countdown ticks once a second with a single timer that is cleared when the component unmounts.

The only animation in this set is the error shake, a few hundred milliseconds of transform on one row, and it is skipped for people who prefer reduced motion. Keep the boxes a fixed width and height so typing a digit never changes their size, which would cause the row to shift and the caret to jump on slower phones.

Questions

How do I make an OTP input auto-advance to the next box?

Listen for input on each box, keep only the last digit typed, then call focus on the next box when a digit was entered. On Backspace in an empty box, move focus to the previous box and clear it.

How do I let people paste the whole code?

Handle the paste event on any box, read the clipboard text, strip everything that is not a digit, then write one digit into each box from the first one onward and focus the box after the last filled digit.

Will SMS autofill work with separate boxes?

It works best on a single input, which is why one piece here uses one field with letter spacing. With separate boxes, set autocomplete to one-time-code on the first box and spread a multi-digit value across the rest when it arrives.

Should an OTP field use type number?

No. A number field drops leading zeros, shows spin buttons and accepts letters like e. Use type text with inputmode numeric and a pattern of digits, which gives phones a number pad and keeps every digit as typed.

How should screen readers hear six separate boxes?

Wrap the boxes in a fieldset with a legend or a group with an aria-label, and label each box with its position, such as Digit 3 of 6. Announce errors and success through a live region so the message is read once.

Also in the gallery: OTP Input Collection with SMS Autofill, Paste and a Resend Timer in React and HTML, Four-Wheel Brass Combination Cable Lock Code Input with Rings That Click Round and a Cable That Springs Free.

More collections: React Tag Input: 5 Chip Inputs With Real Keyboard Support, HTML Forms With CSS: 5 Styled Forms to Copy, Tailwind Progress Bars: 5 Progress Bars in React and HTML. All inputs. Build your site free to have a full site built around them.

Product

Who It's For

Features

Resources

Company

View Sitemap