Form

Easily create nice looking forms with different styles and layouts.

Table of contents

Usage

Add one of the following classes to form controls inside a <form> element to define them.

ClassDescription
.z-inputAdd this class to <input> elements.
.z-selectAdd this class to <select> elements.
.z-textareaAdd this class to <textarea> elements.
.z-radioAdd this class to <input type="radio"> elements to create radio buttons.
.z-checkboxAdd this class to <input type="checkbox"> elements to create checkboxes.
.z-rangeAdd this class to <input type="range"> elements to create range forms.
.z-toggle-switchAdd this class to <input type="checkbox"> elements to create toggle switches.
.z-radio-cardAdd this class to a <label> to create a radio button as a selectable card.
.z-toggle-fieldAdd this class to a <label> to create a toggle switch row with a description.
<form>
  <input class="z-input" type="text" />
  <select class="z-select">
    <option></option>
    <option></option>
  </select>
  <textarea class="z-textarea"></textarea>
  <input class="z-radio" type="radio" />
  <input class="z-checkbox" type="checkbox" />
  <input class="z-range" type="range" />
</form>

Add the .z-fieldset class to a <fieldset> element and the .z-legend class to a <legend> element to define a fieldset and a form legend.

Destructive modifier

Add the .z-form-danger class to an <input>, <select> or <textarea> element to notify the user that the value is not validated.

Size modifiers

There are several size modifiers available. Just add one of the following classes to make the form smaller or larger.

ClassDescription
.z-form-xsmallApplies extra small size.
.z-form-smallApplies small size.
.z-form-mediumApplies medium size.
.z-form-largeApplies large size.

Width modifiers

You can use utility classes alongside form classes and the it will follow its width.

Blank modifier

Add the .z-form-blank class to minimize the styling of form controls.

Layout

Define labels and controls and apply a stacked layout to form elements. Layout modifiers can be added to any parent element like the <fieldset> element.

ClassDescription
.z-form-stackedAdd this class to display labels on top of controls.
.z-form-labelAdd this class to define form labels.
.z-form-label-requiredAdd this class to automatically add an asterisk to form labels.
.z-form-controlsAdd this class to define form controls.
<form class="z-form-stacked">
  <div>
    <label class="z-form-label block"></label>
    <div class="z-form-controls">…</div>
  </div>
  <div>
    <div class="z-form-label"></div>
    <div class="z-form-controls">…</div>
  </div>
</form>
  • Radio

Form and icons

You use an icon from the Icon component inside a form. Add the .z-form-icon class to a <span> element. Group it with an <input> element by adding the .z-inline class to a container element around both. The icon has to come first in the markup. By default, the icon will be placed on the left side of the form. To change the alignment, add the .z-form-icon-flip class.

<div class="z-inline">
  <span class="z-form-icon">
    <z-icon icon="user"></z-icon>
  </span>
  <input class="z-input" />
</div>

Clickable icons

To enable an action, for example opening a modal to pick an image or link, use an <a> or <button> element to create the icon.

<div class="z-inline">
  <a class="z-form-icon z-form-icon-flip" href="">
    <z-icon icon="user"></z-icon>
  </a>
  <input class="z-input" />
</div>

Custom controls

To replace a file input or select forms with your own HTML content, like a button or text, add the data-z-form-custom attribute to a container element.

File

Use a button or text as a file input.

<div data-z-form-custom>
  <input type="file" />
  <button type="button"></button>
</div>
  • Here is a text
    upload

Note The hover and focus state for z-form-custom are not styled by default, but you could use the adjacent sibling selector to do so.

Select

Use a button, text or a link as a select form. Just add the target: SELECTOR option to the z-form-custom attribute to select where the option value should be displayed. target: true will select the adjacent element in the markup.

<div data-z-form-custom="target: true">
  <select>
    <option></option>
    <option></option>
  </select>
  <button type="button"></button>
</div>

Help block

Form text can be created using .z-form-help and should be explicitly associated with the form control it relates to using the aria-describedby attribute. This will ensure that assistive technologies—such as screen readers—will announce this form text when the user focuses or enters the control.

  • This is your public display name. It can be your real name or a pseudonym. You can only change this once every 30 days.

Toggle Switch

Use the following toggle switch component to ask for a yes or no type of input from your users without the use of JavaScript. You can also add the the .z-toggle-switch-danger modifier to apply a danger style.

Radio card

Starting v0.7.12, the radio card component is available.

Use the radio card to present a small set of mutually exclusive options as selectable cards instead of plain radio buttons. Add the .z-radio-card class to a <label> element so the whole card is clickable, and place a .z-radio input inside it.

ClassDescription
.z-radio-cardAdd this class to a <label> element to define the card.
.z-radio-card-controlAdd this class to the element that wraps the radio input.
.z-radio-card-bodyAdd this class to the element that wraps the label and the description.
.z-radio-card-labelAdd this class to define the card title.
.z-radio-card-descriptionAdd this class to define the optional card description.
<label class="z-radio-card">
  <div class="z-radio-card-control">
    <input
      type="radio"
      class="z-radio"
      name="story-type"
      value="one-shot"
      checked
    />
  </div>
  <div class="z-radio-card-body">
    <div class="z-radio-card-label">One-shot</div>
    <div class="z-radio-card-description">A single, complete story</div>
  </div>
</label>

Give every radio in the group the same name attribute so only one card can be selected at a time and arrow key navigation works as expected.

Size modifiers

The radio card supports the same size modifiers as the other form controls. Add one of the .z-form-* size classes to the .z-radio-card element.

<label class="z-radio-card z-form-xsmall">…</label>
<label class="z-radio-card z-form-small">…</label>
<label class="z-radio-card z-form-medium">…</label>
<label class="z-radio-card z-form-large">…</label>

Destructive modifier

Add the .z-form-danger class to the .z-radio-card element to notify the user that the selection is not validated. It changes the focus ring and the selected border to the danger color.

<label class="z-radio-card z-form-danger">…</label>

Disabled

Add the disabled attribute to the radio input to dim the card and disable interaction.

<label class="z-radio-card">
  <div class="z-radio-card-control">
    <input
      type="radio"
      class="z-radio"
      name="story-type"
      value="series"
      disabled
    />
  </div>
  <div class="z-radio-card-body">
    <div class="z-radio-card-label">Series</div>
    <div class="z-radio-card-description">Written chapter by chapter</div>
  </div>
</label>

Note The .z-radio input is visible by default. If you hide it visually, use a screen-reader-only utility instead of display: none so the input stays focusable and keyboard users still get the focus ring.

Toggle field

Starting v0.7.12, the toggle field component is available.

Use the toggle field to pair a toggle switch with a title and an optional description, like a setting row. Add the .z-toggle-field class to a <label> element so the whole row is clickable.

ClassDescription
.z-toggle-fieldAdd this class to a <label> element to define the row.
.z-toggle-field-bodyAdd this class to the element that wraps the label and the description.
.z-toggle-field-labelAdd this class to define the row title.
.z-toggle-field-descriptionAdd this class to define the optional description.
.z-toggle-field-controlAdd this class to the element that wraps the toggle switch.
<label class="z-toggle-field">
  <div class="z-toggle-field-body">
    <div class="z-toggle-field-label" id="published-label">Publish now</div>
    <div class="z-toggle-field-description" id="published-help">
      Make your page live and accessible to everyone.
    </div>
  </div>
  <div class="z-toggle-field-control">
    <input type="hidden" name="published" value="0" />
    <input
      class="z-toggle-switch z-toggle-switch-primary"
      name="published"
      type="checkbox"
      value="1"
      role="switch"
      aria-labelledby="published-label"
      aria-describedby="published-help"
      checked
    />
  </div>
</label>

The hidden input placed before the checkbox makes sure the field is always submitted. An unchecked checkbox sends nothing, so the hidden input provides the 0 value.

Without description

The description is optional. Remove the description element and the aria-describedby attribute. The switch stays vertically aligned with the title.

<label class="z-toggle-field">
  <div class="z-toggle-field-body">
    <div class="z-toggle-field-label" id="featured-label">Featured</div>
  </div>
  <div class="z-toggle-field-control">
    <input
      class="z-toggle-switch z-toggle-switch-primary"
      type="checkbox"
      role="switch"
      aria-labelledby="featured-label"
    />
  </div>
</label>

Disabled

Add the disabled attribute to the toggle switch to dim the title and description and disable interaction.

<label class="z-toggle-field">
  <div class="z-toggle-field-body">
    <div class="z-toggle-field-label" id="locked-label">Publish now</div>
    <div class="z-toggle-field-description">
      Publishing is locked for this page.
    </div>
  </div>
  <div class="z-toggle-field-control">
    <input
      class="z-toggle-switch z-toggle-switch-primary"
      type="checkbox"
      role="switch"
      aria-labelledby="locked-label"
      disabled
    />
  </div>
</label>

Note Don’t put interactive content, such as a link, inside the description. Interactive elements inside a <label> behave inconsistently across browsers. If you need a link, use a regular <label> on the title only and place the toggle field’s description outside of it.

Accessibility

Set the appropriate WAI-ARIA roles, states and properties to the Form component.

  • If no <label> element is associated with the form control, set the aria-label property to the form control to describe its meaning.
<input class="z-input" type="text" aria-label="..." />
  • Give every radio in a radio card group the same name attribute, and wrap the group in a <fieldset> with a <legend> to describe it.
<fieldset class="z-fieldset">
  <legend class="z-legend">Story type</legend>
  <label class="z-radio-card">…</label>
  <label class="z-radio-card">…</label>
</fieldset>
  • For a toggle field, set aria-labelledby on the switch to the title element so the accessible name stays short, and aria-describedby to the description element so it is announced separately. Add role="switch" to announce on/off instead of checked/unchecked.