Skip to main contentSkip to docs navigation

Chassis

Form Layout

View on GitHub

Organize form fields with spacing utilities, the grid system, and flex layouts for horizontal, auto-sized, and inline forms.

Overview

Form inputs are display: block and width: 100% by default, so the fields of a form stack vertically. Spacing utilities, the grid system, and flex utilities arrange them in other layouts: columns, horizontal forms, auto-sized fields, and inline forms.

Place every group of form fields in a <form> element. Chassis CSS applies no styles to the <form> element itself, so its semantics come from the markup:

  • Always specify button types within forms (type="submit", type="button", etc.) as browsers default to type="submit"
  • Use the <fieldset> and <legend> elements to group related form controls
  • Include appropriate ARIA attributes and labels for accessibility
  • For advanced form requirements, consult the MDN form documentation

Spacing utilities

The simplest way to add structure to forms is with margin and padding utilities. These utilities provide consistent spacing between form elements, including labels, controls, help text, and validation messages.

For consistency, Chassis CSS recommends:

  • Using margin-bottom utilities (.mb-*) for vertical spacing
  • Applying the same spacing size throughout a single form
  • Using the md spacing value as a default starting point
HTML
<div class="mb-md">
  <label for="formGroupExampleInput" class="form-label">Example label</label>
  <input type="text" class="form-input" id="formGroupExampleInput" placeholder="Example input placeholder">
</div>
<div class="mb-md">
  <label for="formGroupExampleInput2" class="form-label">Another label</label>
  <input type="text" class="form-input" id="formGroupExampleInput2" placeholder="Another input placeholder">
</div>

Grid-based layouts

The grid system lays form fields out in columns: .grid on the form or on a wrapper, and a span class on each field. A grid-based form has:

  • Responsive layouts, with a span per breakpoint on each field
  • Equal columns from one class on the grid, or a span on each field
  • Consistent spacing from the gutter, or from a gap utility
  • Vertical alignment of the fields in a row
  • Nesting for groups of fields inside a field

Grid-based form layouts need the $enable-grid-system Sass variable, which is on by default.

Basic grid layout

.grid with a column count creates equal columns, with no class on the fields:

HTML
<div class="grid grid-cols-2">
  <div>
    <input type="text" class="form-input" placeholder="First name" aria-label="First name">
  </div>
  <div>
    <input type="text" class="form-input" placeholder="Last name" aria-label="Last name">
  </div>
</div>

Grid gaps

The gap between fields is the gutter of the current breakpoint, which grows with the viewport. A gap utility sets one gap for every width:

  • Both directions with .gap-*
  • Between columns with .column-gap-*
  • Between rows with .row-gap-*
  • Responsive variants of all three

For form layouts, the md gap (.gap-md) suits most forms.

HTML
<div class="grid grid-cols-2 gap-md">
  <div>
    <input type="text" class="form-input" placeholder="First name" aria-label="First name">
  </div>
  <div>
    <input type="text" class="form-input" placeholder="Last name" aria-label="Last name">
  </div>
</div>

Complex grid layouts

Spans of different widths build a form with fields of different sizes. .col-span-full makes a field full width, alone or below the breakpoint of a prefixed span:

HTML
<form class="grid gap-md">
  <div class="col-span-full md:col-span-6">
    <label for="inputEmail4" class="form-label">Email</label>
    <input type="email" class="form-input" id="inputEmail4">
  </div>
  <div class="col-span-full md:col-span-6">
    <label for="inputPassword4" class="form-label">Password</label>
    <input type="password" autocomplete="current-password" class="form-input" id="inputPassword4">
  </div>
  <div class="col-span-full">
    <label for="inputAddress" class="form-label">Address</label>
    <input type="text" class="form-input" id="inputAddress" placeholder="1234 Main St">
  </div>
  <div class="col-span-full">
    <label for="inputAddress2" class="form-label">Address 2</label>
    <input type="text" class="form-input" id="inputAddress2" placeholder="Apartment, studio, or floor">
  </div>
  <div class="col-span-full md:col-span-6">
    <label for="inputCity" class="form-label">City</label>
    <input type="text" class="form-input" id="inputCity">
  </div>
  <div class="col-span-full md:col-span-4">
    <label for="inputState" class="form-label">State</label>
    <select id="inputState" class="form-input">
      <option value="" selected>Choose...</option>
      <option>...</option>
    </select>
  </div>
  <div class="col-span-full md:col-span-2">
    <label for="inputZip" class="form-label">Zip</label>
    <input type="text" class="form-input" id="inputZip">
  </div>
  <div class="col-span-full">
    <div class="form-check">
      <input class="check-input" type="checkbox" id="gridCheck">
      <label for="gridCheck">
        Check me out
      </label>
    </div>
  </div>
  <div class="col-span-full">
    <button type="submit" class="button primary">Sign in</button>
  </div>
</form>

Horizontal form

A horizontal form puts each label beside its control: a .grid per field, with a span on the label and a span on the control. .col-form-label on the <label> gives it the vertical padding of an input, so its text sits on the line of the text of the control. .col-span-full stacks label and control below the breakpoint, and .row-gap-0 keeps them together there.

A control with no label starts on the line of the other controls with a start class (.sm:col-start-3 after a label of two tracks). Margin or padding utilities adjust the alignment where needed: in the example below, padding-top is removed from the radio group label to align its text baseline.

Radios
HTML
<form>
  <div class="grid row-gap-0 mb-md">
    <label for="inputEmail3" class="col-span-full sm:col-span-2 col-form-label">Email</label>
    <div class="col-span-full sm:col-span-10">
      <input type="email" class="form-input" id="inputEmail3">
    </div>
  </div>
  <div class="grid row-gap-0 mb-md">
    <label for="inputPassword3" class="col-span-full sm:col-span-2 col-form-label">Password</label>
    <div class="col-span-full sm:col-span-10">
      <input type="password" autocomplete="current-password" class="form-input" id="inputPassword3">
    </div>
  </div>
  <div class="grid row-gap-0 mb-md">
    <div class="col-span-full sm:col-span-10 sm:col-start-3">
      <div class="form-check">
        <input class="check-input" type="checkbox" id="gridCheck1">
        <label for="gridCheck1">
          Example checkbox
        </label>
      </div>
    </div>
  </div>
  <div class="grid row-gap-0 mb-md" role="group" aria-labelledby="gridRadiosLabel">
    <div class="col-form-label col-span-full sm:col-span-2 pt-0" id="gridRadiosLabel">Radios</div>
    <div class="col-span-full sm:col-span-10 stack-container">
    <div class="vstack sm:hstack gap-md row-gap-xs">
      <div class="form-check">
        <input class="check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
        <label for="gridRadios1">
          First radio
        </label>
      </div>
      <div class="form-check">
        <input class="check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
        <label for="gridRadios2">
          Second radio
        </label>
      </div>
      <div class="form-check disabled">
        <input class="check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
        <label for="gridRadios3">
          Third disabled radio
        </label>
      </div>
    </div>
    </div>
  </div>
  <div class="grid row-gap-0 mb-md">
    <div class="col-span-full sm:col-span-10 sm:col-start-3">
      <button type="submit" class="button primary">Sign in</button>
    </div>
  </div>
</form>

Horizontal label sizing

Add the size modifier of the input to its .col-form-label to match the two:

  • .col-form-label.sm aligns with small inputs (.form-input.sm)
  • .col-form-label.lg aligns with large inputs (.form-input.lg)

The classes set the font size, line height, and padding of the label to the ones of an input of that size, so the two stay aligned.

HTML
<div class="grid row-gap-0 mb-md">
  <label for="colFormLabelSm" class="col-span-full sm:col-span-2 col-form-label sm">Email</label>
  <div class="col-span-full sm:col-span-10">
    <input type="email" class="form-input sm" id="colFormLabelSm" placeholder=".col-form-label.sm">
  </div>
</div>
<div class="grid row-gap-0 mb-md">
  <label for="colFormLabel" class="col-span-full sm:col-span-2 col-form-label">Email</label>
  <div class="col-span-full sm:col-span-10">
    <input type="email" class="form-input" id="colFormLabel" placeholder=".col-form-label">
  </div>
</div>
<div class="grid row-gap-0">
  <label for="colFormLabelLg" class="col-span-full sm:col-span-2 col-form-label lg">Email</label>
  <div class="col-span-full sm:col-span-10">
    <input type="email" class="form-input lg" id="colFormLabelLg" placeholder=".col-form-label.lg">
  </div>
</div>

Column sizing

A span on each field sets its share of the row. The spans of a row add up to the twelve tracks of the grid: below, six for the city and three each for the state and the zip code, from the sm breakpoint.

HTML
<div class="grid gap-md">
  <div class="col-span-full sm:col-span-6">
    <input type="text" class="form-input" placeholder="City" aria-label="City">
  </div>
  <div class="col-span-full sm:col-span-3">
    <input type="text" class="form-input" placeholder="State" aria-label="State">
  </div>
  <div class="col-span-full sm:col-span-3">
    <input type="text" class="form-input" placeholder="Zip" aria-label="Zip">
  </div>
</div>

Auto-sizing

Fields as wide as their content are a flex layout, not a grid: the tracks of a grid take their width from the container. .d-flex with .flex-wrap on the form sizes each field to its content and wraps the fields that do not fit; gap utilities space them. This suits:

  • Forms with varying field lengths
  • Layouts where field sizes should match their expected content
  • Compact forms in a toolbar or a filter bar
  • Input groups with add-ons or buttons beside them

.align-items-center centers the fields of a row vertically.

@
HTML
<form class="d-flex flex-wrap row-gap-sm column-gap-md align-items-center">
  <div>
    <label class="visually-hidden" for="autoSizingInput">Name</label>
    <input type="text" class="form-input" id="autoSizingInput" placeholder="Jane Doe">
  </div>
  <div>
    <label class="visually-hidden" for="autoSizingInputGroup">Username</label>
    <div class="input-group">
      <div class="input-addon">@</div>
      <input type="text" class="form-input" id="autoSizingInputGroup" placeholder="Username">
    </div>
  </div>
  <div>
    <label class="visually-hidden" for="autoSizingSelect">Preference</label>
    <select class="form-input" id="autoSizingSelect">
      <option value="" selected>Choose...</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
  </div>
  <div>
    <div class="form-check">
      <input class="check-input" type="checkbox" id="autoSizingCheck">
      <label for="autoSizingCheck">
        Remember me
      </label>
    </div>
  </div>
  <div>
    <button type="submit" class="button primary">Submit</button>
  </div>
</form>

Fraction widths give some fields of the flex row a share of its width, and the others keep the width of their content. Below, three fields share the row in equal parts, a quarter each at most, from the sm breakpoint, where .sm:flex-nowrap keeps the row on one line:

@
HTML
<form class="d-flex flex-wrap sm:flex-nowrap row-gap-sm column-gap-md align-items-center">
  <div class="w-100 sm:w-3/12">
    <label class="visually-hidden" for="specificSizeInputName">Name</label>
    <input type="text" class="form-input" id="specificSizeInputName" placeholder="Jane Doe">
  </div>
  <div class="w-100 sm:w-3/12">
    <label class="visually-hidden" for="specificSizeInputGroupUsername">Username</label>
    <div class="input-group">
      <div class="input-addon">@</div>
      <input type="text" class="form-input" id="specificSizeInputGroupUsername" placeholder="Username">
    </div>
  </div>
  <div class="w-100 sm:w-3/12">
    <label class="visually-hidden" for="specificSizeSelect">Preference</label>
    <select class="form-input" id="specificSizeSelect">
      <option value="" selected>Choose...</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
  </div>
  <div class="flex-shrink-0">
    <div class="form-check">
      <input class="check-input" type="checkbox" id="autoSizingCheck2">
      <label for="autoSizingCheck2">
        Remember me
      </label>
    </div>
  </div>
  <div class="flex-shrink-0">
    <button type="submit" class="button primary">Submit</button>
  </div>
</form>

Inline forms

An inline form stacks its fields on small screens and sets them on one line on larger ones, each as wide as its content. It is a flex container that changes direction at a breakpoint:

  • .d-flex with .flex-column stacks the fields at full width
  • .lg:flex-row sets them on one line from the lg breakpoint
  • A gap utility spaces the fields in both directions
  • .lg:align-items-center centers them vertically once they share a line
@
HTML
<form class="d-flex flex-column lg:flex-row gap-md lg:align-items-center">
  <div>
    <label class="visually-hidden" for="inlineFormInputGroupUsername">Username</label>
    <div class="input-group">
      <div class="input-addon">@</div>
      <input type="text" class="form-input" id="inlineFormInputGroupUsername" placeholder="Username">
    </div>
  </div>

  <div>
    <label class="visually-hidden" for="inlineFormSelectPref">Preference</label>
    <select class="form-input" id="inlineFormSelectPref">
      <option value="" selected>Choose...</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
  </div>

  <div>
    <div class="form-check">
      <input class="check-input" type="checkbox" id="inlineFormCheck">
      <label for="inlineFormCheck">
        Remember me
      </label>
    </div>
  </div>

  <div>
    <button type="submit" class="button primary">Submit</button>
  </div>
</form>

Inline form help

For compact layouts, use inline form help by applying the .form-help class to any inline element such as <span> or <small>.

Must be 8-20 characters long.
HTML
<div class="d-flex flex-column lg:flex-row lg:gap-md lg:align-items-center">
<div>
  <label for="inlineFormHelpInput" class="col-form-label">Password</label>
</div>
<div>
  <input type="password" autocomplete="current-password" id="inlineFormHelpInput" class="form-input" aria-describedby="inlineFormHelpText" placeholder="Password">
</div>
<div>
  <span id="inlineFormHelpText" class="form-help lg:my-auto">
    Must be 8-20 characters long.
  </span>
</div>
</div>

Accessibility

A layout changes where the fields are, not how they are announced or reached. Keep the form accessible in every layout:

  • Always include visible labels, or use .visually-hidden for labels that must be announced by screen readers but not shown visually
  • Maintain a logical tab order that follows the visual layout of the form
  • Use semantic HTML elements like <fieldset> and <legend> to group related form controls
  • Include appropriate ARIA attributes where needed (e.g., aria-describedby for form validation messages)
  • Test keyboard navigation flow to ensure all form controls are accessible without a mouse
  • Ensure sufficient color contrast for form elements and labels
  • For complex layouts, test with screen readers to verify the form is navigable

See the Form validation documentation for more information on creating accessible validation feedback.