Back to the library

Field — float label

Variant: Float label

The placeholder that becomes the label. It is one element that moves, not two that crossfade, so the eye follows the word from the line up to the top edge of the field instead of watching one word vanish and another appear. It scales to 0.76 from its left edge, where the reading line starts, and it lifts on focus, before the first keystroke: a label still sitting in the line when the first letter lands collides with it. Up is 200ms on a strong ease-out, because it answers a click. Down is 180ms on ease-in-out, because by then it is an element moving between two places, and leaving should never take longer than arriving; the ink and ring fade on the same 180ms, so focus is gone the frame the label lands. The second half of the loop is the case a JavaScript `value ? up : down` gets wrong. The field is refocused, selected and cleared, and the label stays up. The rule is focused-or-filled, which `:focus` with `:placeholder-shown` gives you for free; key it on the value alone and the label drops back into the line under a live caret. Only on blur does it fall. Focus inks the label clay and rings the field, and blur returns both to quiet in 160ms. Under reduced motion the in-line label fades out as a resting small copy fades in.

textcontrolkeyboardfocustransitionloop

The source

A complete HTML document. Paste it into an empty .html file and it plays — no stylesheet, script, font, or image to fetch.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Field — float label</title>
    <style>
      :root {
        /* Neutrals — fixed, never remixed. */
        --ivory: #faf9f5;
        --paper: #ffffff;
        --oat: #e3dacc;
        --slate: #141413;

        /* Knobs — the remix API. A knob exists iff declared here and used below. */
        --hue: 38.8;
        --lift: 0;
        --chroma: 1;
        --round: 1;
        --speed: 1;

        /* Accents — derived from the knobs with the family's offsets baked in,
           so one hue roll moves them together. Defaults reproduce the original hex. */
        --clay: oklch(calc(0.6724 + var(--lift)) calc(0.1308 * var(--chroma)) var(--hue));
        --clay-d: oklch(calc(0.5797 + var(--lift)) calc(0.127 * var(--chroma)) var(--hue));
        --olive: oklch(
          calc(0.6118 + var(--lift)) calc(0.0713 * var(--chroma)) calc(var(--hue) + 88.3)
        );
        --rust: oklch(
          calc(0.5408 + var(--lift)) calc(0.1357 * var(--chroma)) calc(var(--hue) - 10.3)
        );

        /* Clock — 5.2s. Focus at 500ms lifts the label in 200ms before a key is
           pressed; sixteen keys land 65ms apart from 800ms; blur at 2.6s;
           refocus at 3.5s, select-all at 3.7s, delete at 3.9s — the label
           stays up, because the field is still focused; blur at 4.5s lets
           it fall in 180ms. */
        --cycle: 5.2s;
        --t: calc(var(--cycle) / var(--speed));

        --_quiet: color-mix(in srgb, var(--slate) 55%, transparent);
        --_halo: color-mix(in srgb, var(--clay) 16%, transparent);
        --_wash: color-mix(in srgb, var(--clay) 24%, transparent);
      }

      /* Curves are written out inside @keyframes, not as tokens: a var() in a
         keyframe's animation-timing-function is dropped and falls back to
         ease, which would quietly flatten every curve below. */

      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }

      body {
        min-height: 100vh;
        display: grid;
        place-items: center;
        padding: clamp(12px, 3vw, 28px);
        background: var(--ivory);
        color: var(--slate);
        font-family:
          system-ui,
          -apple-system,
          'Segoe UI',
          Roboto,
          sans-serif;
      }

      .field {
        position: relative;
        width: min(300px, 86vw);
        height: 58px;
        border: 1.5px solid var(--oat);
        border-radius: calc(12px * var(--round));
        background: var(--paper);
        font-size: 16px;
        line-height: 20px;
      }

      .ring {
        position: absolute;
        inset: -1.5px;
        border: 1.5px solid var(--clay);
        border-radius: calc(12px * var(--round));
        box-shadow: 0 0 0 3px var(--_halo);
        opacity: 0;
        animation: focus var(--t) infinite;
      }

      /* The label is the placeholder and the title at once, so it is one
         element that moves, never two that crossfade — the eye follows the
         word up rather than watching one vanish and another appear. It
         scales from its left edge, which is where the reading line starts,
         and it lifts on focus, not on the first keystroke: a label still
         sitting in the line when the first letter lands collides with it. */
      .label,
      .label-up {
        position: absolute;
        top: 18px;
        left: 15px;
        white-space: nowrap;
        color: var(--_quiet);
        transform-origin: 0 50%;
      }

      .label {
        animation:
          lift var(--t) infinite,
          ink var(--t) infinite;
      }

      .label-up {
        display: none;
        transform: translateY(-11px) scale(0.76);
      }

      .value {
        position: absolute;
        top: 27px;
        left: 15px;
        white-space: pre;
        border-radius: calc(3px * var(--round));
        animation: select var(--t) step-end infinite;
      }

      /* Each character is laid out from the start and inks on its own frame;
         the caret is a 2px shadow on the right edge of whichever one was
         typed last, so it sits exactly where the next one will go. */
      .value span {
        color: transparent;
        animation-duration: var(--t);
        animation-timing-function: step-end;
        animation-iteration-count: infinite;
      }

      .value span:nth-child(1) {
        animation-name: k0;
      }

      .value span:nth-child(2) {
        animation-name: k1;
      }

      .value span:nth-child(3) {
        animation-name: k2;
      }

      .value span:nth-child(4) {
        animation-name: k3;
      }

      .value span:nth-child(5) {
        animation-name: k4;
      }

      .value span:nth-child(6) {
        animation-name: k5;
      }

      .value span:nth-child(7) {
        animation-name: k6;
      }

      .value span:nth-child(8) {
        animation-name: k7;
      }

      .value span:nth-child(9) {
        animation-name: k8;
      }

      .value span:nth-child(10) {
        animation-name: k9;
      }

      .value span:nth-child(11) {
        animation-name: k10;
      }

      .value span:nth-child(12) {
        animation-name: k11;
      }

      .value span:nth-child(13) {
        animation-name: k12;
      }

      .value span:nth-child(14) {
        animation-name: k13;
      }

      .value span:nth-child(15) {
        animation-name: k14;
      }

      .value span:nth-child(16) {
        animation-name: k15;
      }

      .lead {
        position: absolute;
        top: 28px;
        left: 15px;
        width: 2px;
        height: 19px;
        margin-left: -1px;
        border-radius: 999px;
        background: var(--clay);
        opacity: 0;
        animation: lead var(--t) step-end infinite;
      }

      /* Up on ease-out because it answers a click. Down on ease-in-out,
         because by then it is not a response but an element moving between
         two places on screen — and 20ms faster, because leaving should
         never take longer than arriving. The ink fades on the same 180ms,
         so focus is gone the frame the label lands. */
      @keyframes lift {
        0% {
          transform: none;
        }
        9.615% {
          transform: none;
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        13.462% {
          transform: translateY(-11px) scale(0.76);
        }
        86.538% {
          transform: translateY(-11px) scale(0.76);
          animation-timing-function: cubic-bezier(0.77, 0, 0.175, 1);
        }
        90% {
          transform: none;
        }
        100% {
          transform: none;
        }
      }

      @keyframes ink {
        0% {
          color: var(--_quiet);
        }
        9.615% {
          color: var(--_quiet);
          animation-timing-function: ease;
        }
        11.923% {
          color: var(--clay-d);
        }
        50% {
          color: var(--clay-d);
          animation-timing-function: ease;
        }
        53.077% {
          color: var(--_quiet);
        }
        67.308% {
          color: var(--_quiet);
          animation-timing-function: ease;
        }
        69.615% {
          color: var(--clay-d);
        }
        86.538% {
          color: var(--clay-d);
          animation-timing-function: ease;
        }
        90% {
          color: var(--_quiet);
        }
        100% {
          color: var(--_quiet);
        }
      }

      @keyframes focus {
        0% {
          opacity: 0;
        }
        9.615% {
          opacity: 0;
          animation-timing-function: ease;
        }
        11.923% {
          opacity: 1;
        }
        50% {
          opacity: 1;
          animation-timing-function: ease;
        }
        53.077% {
          opacity: 0;
        }
        67.308% {
          opacity: 0;
          animation-timing-function: ease;
        }
        69.615% {
          opacity: 1;
        }
        86.538% {
          opacity: 1;
          animation-timing-function: ease;
        }
        90% {
          opacity: 0;
        }
        100% {
          opacity: 0;
        }
      }

      @keyframes select {
        0% {
          background: transparent;
        }
        71.154% {
          background: var(--_wash);
        }
        75% {
          background: transparent;
        }
        100% {
          background: transparent;
        }
      }

      @keyframes lead {
        0% {
          opacity: 0;
        }
        9.615% {
          opacity: 1;
        }
        15.385% {
          opacity: 0;
        }
        75% {
          opacity: 1;
        }
        84.615% {
          opacity: 0;
        }
        86.538% {
          opacity: 0;
        }
        100% {
          opacity: 0;
        }
      }

      @keyframes k0 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        15.385% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        16.635% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k1 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        16.635% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        17.885% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k2 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        17.885% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        19.135% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k3 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        19.135% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        20.385% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k4 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        20.385% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        21.635% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k5 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        21.635% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        22.885% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k6 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        22.885% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        24.135% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k7 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        24.135% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        25.385% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k8 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        25.385% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        26.635% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k9 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        26.635% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        27.885% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k10 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        27.885% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        29.135% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k11 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        29.135% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        30.385% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k12 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        30.385% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        31.635% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k13 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        31.635% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        32.885% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k14 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        32.885% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        34.135% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @keyframes k15 {
        0% {
          color: transparent;
          box-shadow: none;
        }
        34.135% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        43.75% {
          color: var(--slate);
          box-shadow: none;
        }
        50% {
          color: var(--slate);
          box-shadow: none;
        }
        67.308% {
          color: var(--slate);
          box-shadow: 2px 0 0 var(--clay);
        }
        71.154% {
          color: var(--slate);
          box-shadow: none;
        }
        75% {
          color: transparent;
          box-shadow: none;
        }
        100% {
          color: transparent;
          box-shadow: none;
        }
      }

      @media (prefers-reduced-motion: reduce) {
        /* No travel: the in-line label fades out as a resting small copy
           fades in, on the same 200ms, and both still ink with focus. The
           whole clock runs 1.6x slower. */
        .ring,
        .label,
        .label-up,
        .value,
        .value span,
        .lead {
          animation-duration: calc(var(--t) * 1.6);
        }

        .label {
          animation-name: fade-down, ink;
        }

        .label-up {
          display: block;
          opacity: 0;
          animation:
            fade-up calc(var(--t) * 1.6) infinite,
            ink calc(var(--t) * 1.6) infinite;
        }

        @keyframes fade-down {
          0% {
            opacity: 1;
          }
          9.615% {
            opacity: 1;
            animation-timing-function: ease;
          }
          13.462% {
            opacity: 0;
          }
          86.538% {
            opacity: 0;
            animation-timing-function: ease;
          }
          90% {
            opacity: 1;
          }
          100% {
            opacity: 1;
          }
        }

        @keyframes fade-up {
          0% {
            opacity: 0;
          }
          9.615% {
            opacity: 0;
            animation-timing-function: ease;
          }
          13.462% {
            opacity: 1;
          }
          86.538% {
            opacity: 1;
            animation-timing-function: ease;
          }
          90% {
            opacity: 0;
          }
          100% {
            opacity: 0;
          }
        }
      }
    </style>
  </head>
  <body>
    <div
      class="field"
      role="img"
      aria-label="An email field: on focus the placeholder lifts into a small label above the line, an address is typed, the field blurs with the label still up, then it is refocused, selected and cleared — the label stays up while focused and only falls back into the line on blur."
    >
      <div class="ring"></div>
      <span class="label">Email address</span>
      <span class="label-up" aria-hidden="true">Email address</span>
      <span class="value"
        ><span>a</span><span>d</span><span>a</span><span>@</span><span>l</span><span>o</span
        ><span>v</span><span>e</span><span>l</span><span>a</span><span>c</span><span>e</span
        ><span>.</span><span>d</span><span>e</span><span>v</span></span
      >
      <span class="lead"></span>
    </div>
  </body>
</html>