16 CSS Floating Label Inputs16 / 16

Pure CSSMIT licensed

Multi-Column Form Grid with Floating Labels (CSS Grid)

A full checkout-style address form laying floating label fields into a responsive CSS grid: paired columns on desktop, asymmetric spans for city/state/ZIP, and a single column under 560px — proving the floating label pattern composes cleanly when fields share rows, gaps and baselines. Variation: floated labels snap to UPPERCASE MICRO-LEGENDS with wide tracking — reading as engraved field headings that unify the whole grid.

Published

Live Demo
Try it

The code

<section class="flb-16" aria-label="Multi-column form grid floating labels demo">
  <form class="flb-16__form" novalidate>
    <h3 class="flb-16__title">Shipping address</h3>
    <div class="flb-16__grid">
      <div class="flb-16__field" style="--span:3">
        <input class="flb-16__input" type="text" id="flb-16-first" name="given-name" placeholder=" " autocomplete="given-name">
        <label class="flb-16__label" for="flb-16-first">First name</label>
      </div>
      <div class="flb-16__field" style="--span:3">
        <input class="flb-16__input" type="text" id="flb-16-last" name="family-name" placeholder=" " autocomplete="family-name">
        <label class="flb-16__label" for="flb-16-last">Last name</label>
      </div>
      <div class="flb-16__field" style="--span:6">
        <input class="flb-16__input" type="text" id="flb-16-street" name="street-address" placeholder=" " autocomplete="street-address">
        <label class="flb-16__label" for="flb-16-street">Street address</label>
      </div>
      <div class="flb-16__field" style="--span:2">
        <input class="flb-16__input" type="text" id="flb-16-city" name="address-level2" placeholder=" " autocomplete="address-level2">
        <label class="flb-16__label" for="flb-16-city">City</label>
      </div>
      <div class="flb-16__field" style="--span:2">
        <input class="flb-16__input" type="text" id="flb-16-state" name="address-level1" placeholder=" " autocomplete="address-level1">
        <label class="flb-16__label" for="flb-16-state">State</label>
      </div>
      <div class="flb-16__field" style="--span:2">
        <input class="flb-16__input" type="text" id="flb-16-zip" name="postal-code" placeholder=" " inputmode="numeric" autocomplete="postal-code">
        <label class="flb-16__label" for="flb-16-zip">ZIP</label>
      </div>
    </div>
    <button class="flb-16__btn" type="button">Continue to payment</button>
  </form>
</section>
.flb-16,
.flb-16 *,
.flb-16 *::before,
.flb-16 *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.flb-16 {
  --accent: oklch(0.52 0.16 264);
  font-family: 'Segoe UI',system-ui,sans-serif;
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f1f5;
  padding: 40px 20px;
  container-type: inline-size;
}

.flb-16__form {
  width: min(560px,100%);
  background: #fff;
  border-radius: 20px;
  padding: 34px 32px;
  box-shadow: 0 24px 60px -32px rgba(24,28,60,.35);
  display: flex;
  flex-direction: column;
  gap: 22px;
  container-type: inline-size;
  container-name: flb16form;
}

.flb-16__title {
  font-size: 23px;
  font-weight: 800;
  color: #191c2b;
  letter-spacing: -.02em;
}

.flb-16__grid {
  display: grid;
  grid-template-columns: repeat(6,1fr);
  gap: 16px;
}

.flb-16__field {
  position: relative;
  grid-column: span var(--span,6);
}

.flb-16__input {
  width: 100%;
  height: 56px;
  padding: 22px 16px 6px;
  font-size: 16px;
  color: #191c2b;
  background: #fff;
  border: 1.5px solid #d6d9e4;
  border-radius: 12px;
  outline: none;
  transition: border-color .2s,box-shadow .2s;
}

.flb-16__input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px oklch(0.52 0.16 264 / .13);
}

.flb-16__label {
  position: absolute;
  left: 16px;
  top: 50%;
  translate: 0 -50%;
  font-size: 15px;
  color: #7a7f93;
  pointer-events: none;
  transform-origin: left center;
  transition: transform .18s,color .18s;
  max-width: calc(100% - 28px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.flb-16__input:focus ~ .flb-16__label,
.flb-16__input:not(:placeholder-shown) ~ .flb-16__label {
  transform: translateY(-14px) scale(.7);
  text-transform: uppercase;
  letter-spacing: .12em;
  font-weight: 700;
}

.flb-16__input:focus ~ .flb-16__label {
  color: var(--accent);
}

.flb-16__btn {
  height: 52px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: filter .15s;
}

.flb-16__btn:hover {
  filter: brightness(1.12);
}

.flb-16__btn:focus-visible,
.flb-16__input:focus-visible {
  outline: 3px solid oklch(0.52 0.16 264 / .5);
  outline-offset: 2px;
}

@container flb16form (max-width: 559px) {
  .flb-16__field {
    grid-column: span 6;
  }
}

@media (prefers-reduced-motion: reduce) {
  .flb-16__label,
    .flb-16__input {
    transition: none;
  }
}
/* No JavaScript — a 6-track CSS grid with per-field --span variables handles all column layouts; a container query collapses to one column based on the form's own width, not the viewport's. */
Paste this into ChatGPT, Claude, Cursor, or any coding assistant. The block below is pre-framed with everything the AI needs to integrate this demo into your project — markup, styles, scoping notes, and the source URL. Hit Copy and paste straight into your chat.
Here's a working CSS Floating Label Input from CodeFronts. Use it as-is or adapt to your framework. All classes are scoped under a unique prefix so the code won't collide with your existing styles. MIT licensed.
Demo: Multi-Column Form Grid with Floating Labels (CSS Grid)
Source: https://codefronts.com/components/css-floating-label-inputs/multi-column-form-grid-with-floating-labels-css-grid/

A full checkout-style address form laying floating label fields into a responsive CSS grid: paired columns on desktop, asymmetric spans for city/state/ZIP, and a single column under 560px — proving the floating label pattern composes cleanly when fields share rows, gaps and baselines. Variation: floated labels snap to UPPERCASE MICRO-LEGENDS with wide tracking — reading as engraved field headings that unify the whole grid.
## HTML
```html
<section class="flb-16" aria-label="Multi-column form grid floating labels demo">
  <form class="flb-16__form" novalidate>
    <h3 class="flb-16__title">Shipping address</h3>
    <div class="flb-16__grid">
      <div class="flb-16__field" style="--span:3">
        <input class="flb-16__input" type="text" id="flb-16-first" name="given-name" placeholder=" " autocomplete="given-name">
        <label class="flb-16__label" for="flb-16-first">First name</label>
      </div>
      <div class="flb-16__field" style="--span:3">
        <input class="flb-16__input" type="text" id="flb-16-last" name="family-name" placeholder=" " autocomplete="family-name">
        <label class="flb-16__label" for="flb-16-last">Last name</label>
      </div>
      <div class="flb-16__field" style="--span:6">
        <input class="flb-16__input" type="text" id="flb-16-street" name="street-address" placeholder=" " autocomplete="street-address">
        <label class="flb-16__label" for="flb-16-street">Street address</label>
      </div>
      <div class="flb-16__field" style="--span:2">
        <input class="flb-16__input" type="text" id="flb-16-city" name="address-level2" placeholder=" " autocomplete="address-level2">
        <label class="flb-16__label" for="flb-16-city">City</label>
      </div>
      <div class="flb-16__field" style="--span:2">
        <input class="flb-16__input" type="text" id="flb-16-state" name="address-level1" placeholder=" " autocomplete="address-level1">
        <label class="flb-16__label" for="flb-16-state">State</label>
      </div>
      <div class="flb-16__field" style="--span:2">
        <input class="flb-16__input" type="text" id="flb-16-zip" name="postal-code" placeholder=" " inputmode="numeric" autocomplete="postal-code">
        <label class="flb-16__label" for="flb-16-zip">ZIP</label>
      </div>
    </div>
    <button class="flb-16__btn" type="button">Continue to payment</button>
  </form>
</section>
```
## CSS
```css
.flb-16,
.flb-16 *,
.flb-16 *::before,
.flb-16 *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.flb-16 {
  --accent: oklch(0.52 0.16 264);
  font-family: 'Segoe UI',system-ui,sans-serif;
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f1f5;
  padding: 40px 20px;
  container-type: inline-size;
}

.flb-16__form {
  width: min(560px,100%);
  background: #fff;
  border-radius: 20px;
  padding: 34px 32px;
  box-shadow: 0 24px 60px -32px rgba(24,28,60,.35);
  display: flex;
  flex-direction: column;
  gap: 22px;
  container-type: inline-size;
  container-name: flb16form;
}

.flb-16__title {
  font-size: 23px;
  font-weight: 800;
  color: #191c2b;
  letter-spacing: -.02em;
}

.flb-16__grid {
  display: grid;
  grid-template-columns: repeat(6,1fr);
  gap: 16px;
}

.flb-16__field {
  position: relative;
  grid-column: span var(--span,6);
}

.flb-16__input {
  width: 100%;
  height: 56px;
  padding: 22px 16px 6px;
  font-size: 16px;
  color: #191c2b;
  background: #fff;
  border: 1.5px solid #d6d9e4;
  border-radius: 12px;
  outline: none;
  transition: border-color .2s,box-shadow .2s;
}

.flb-16__input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px oklch(0.52 0.16 264 / .13);
}

.flb-16__label {
  position: absolute;
  left: 16px;
  top: 50%;
  translate: 0 -50%;
  font-size: 15px;
  color: #7a7f93;
  pointer-events: none;
  transform-origin: left center;
  transition: transform .18s,color .18s;
  max-width: calc(100% - 28px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.flb-16__input:focus ~ .flb-16__label,
.flb-16__input:not(:placeholder-shown) ~ .flb-16__label {
  transform: translateY(-14px) scale(.7);
  text-transform: uppercase;
  letter-spacing: .12em;
  font-weight: 700;
}

.flb-16__input:focus ~ .flb-16__label {
  color: var(--accent);
}

.flb-16__btn {
  height: 52px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: filter .15s;
}

.flb-16__btn:hover {
  filter: brightness(1.12);
}

.flb-16__btn:focus-visible,
.flb-16__input:focus-visible {
  outline: 3px solid oklch(0.52 0.16 264 / .5);
  outline-offset: 2px;
}

@container flb16form (max-width: 559px) {
  .flb-16__field {
    grid-column: span 6;
  }
}

@media (prefers-reduced-motion: reduce) {
  .flb-16__label,
    .flb-16__input {
    transition: none;
  }
}
```

## JavaScript
```js
/* No JavaScript — a 6-track CSS grid with per-field --span variables handles all column layouts; a container query collapses to one column based on the form's own width, not the viewport's. */
```

How this works

The form is one display:grid with grid-template-columns:repeat(6,1fr) and a single gap; each field wrapper spans 6, 3, or 2 tracks (--span) so full-width, half-width and third-width fields all align to the same rhythm. Because every field is the same self-contained wrapper (input + label, position:relative), the float behaves identically at any span — nothing about the label logic knows the grid exists.

A container query (@container) — not a viewport media query — collapses everything to one column when the FORM (not the screen) is under 560px, so the form stays correct when embedded in a sidebar or modal. Fields keep equal heights, so rows never rag.

Techniques used: CSS Grid with grid-column spans · nested 2fr/1fr grid row · container-type:inline-size + @container query (element-width responsive) · text-transform:uppercase + letter-spacing float restyle · ellipsis-guarded labels (max-width + text-overflow)

Make it yours

  • Change any field's width by editing its --span (out of 6 tracks).
  • Adjust the collapse threshold in the @container rule.
  • The gap is one value — grid handles all alignment.
  • Add fields freely; wrappers are order-independent.

Gotchas — read before shipping

  • Give every field wrapper the same height; mixed heights make shared rows rag and floats misalign across columns.
  • Use container queries, not media queries, so the form adapts to ITS width (sidebars, modals, split layouts).
  • Keep autocomplete attributes on every address field — grid layouts are exactly where browser autofill shines (see demo 02 for autofill-proof labels).
  • Don't shrink gaps below ~14px; adjacent floated labels start reading as one line.

Browser support

ChromeSafariFirefoxEdge
111+16+113+111+

Floor set by oklch(), @container as this demo is written. The core technique itself goes back further — Chrome 105+ (@container).

Container queries are baseline since 2023. Swap the @container block for a media query if you must support older browsers — the grid itself is universally supported.

Techniques used in this demo

Search CodeFronts

Loading…