Skip to content

Field ​

Groups form controls with labels, descriptions and errors, in stacked, side-by-side and horizontal layouts.

Installation ​

bash
npx bootcn add field
bash
pnpm dlx bootcn add field
bash
yarn dlx bootcn add field
bash
bunx --bun bootcn add field

The CLI also installs the design tokens and these components: input, select, switch, label. It prints the <link> and <script> tags to add to your layout.

Usage ​

Wrap related fields in a <fieldset class="bootcn-fieldset"> with a legend. Each .bootcn-field stacks a label, a control, help text (.bootcn-field__help) and an error (.bootcn-field__error). Put fields side by side in .bootcn-field-row, and use .bootcn-field--horizontal for a setting with its switch on the right.

Link help and error text to the control with aria-describedby, and set aria-invalid="true" on a control with an error.

Accessibility ​

  • The legend names the group, so screen readers announce "Payment method" when focus enters it.
  • Help and error text are announced with the control (aria-describedby).
  • Errors are text, not color alone, and use --bootcn-destructive-text (4.5:1 in both themes).

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.
  • The input component.
  • The select component.
  • The switch component.
  • The label component.

Source ​

The exact files the CLI installs (field@1.0.0). View on GitHub · Report an issue

html
<fieldset class="bootcn-fieldset">
  <legend class="bootcn-fieldset__legend">Payment method</legend>
  <p class="bootcn-fieldset__description">All transactions are secure and encrypted.</p>

  <div class="bootcn-field">
    <label class="bootcn-label" for="bootcn-field-name">Name on card</label>
    <input
      class="form-control bootcn-input"
      type="text"
      id="bootcn-field-name"
      name="cc-name"
      autocomplete="cc-name"
    />
  </div>

  <div class="bootcn-field">
    <label class="bootcn-label" for="bootcn-field-number">Card number</label>
    <input
      class="form-control bootcn-input"
      type="text"
      id="bootcn-field-number"
      name="cc-number"
      inputmode="numeric"
      autocomplete="cc-number"
      aria-describedby="bootcn-field-number-help"
    />
    <p class="bootcn-field__help" id="bootcn-field-number-help">
      Enter the 16 digits without spaces.
    </p>
  </div>

  <div class="bootcn-field-row">
    <div class="bootcn-field">
      <label class="bootcn-label" for="bootcn-field-month">Month</label>
      <select
        class="form-select bootcn-select"
        id="bootcn-field-month"
        name="cc-exp-month"
        autocomplete="cc-exp-month"
      >
        <option value="">MM</option>
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
      </select>
    </div>
    <div class="bootcn-field">
      <label class="bootcn-label" for="bootcn-field-year">Year</label>
      <select
        class="form-select bootcn-select"
        id="bootcn-field-year"
        name="cc-exp-year"
        autocomplete="cc-exp-year"
      >
        <option value="">YYYY</option>
        <option value="2027">2027</option>
        <option value="2028">2028</option>
      </select>
    </div>
    <div class="bootcn-field">
      <label class="bootcn-label" for="bootcn-field-cvc">CVC</label>
      <input
        class="form-control bootcn-input"
        type="text"
        id="bootcn-field-cvc"
        name="cc-csc"
        inputmode="numeric"
        autocomplete="cc-csc"
        aria-invalid="true"
        aria-describedby="bootcn-field-cvc-error"
      />
      <p class="bootcn-field__error" id="bootcn-field-cvc-error">Enter the 3 digits on the back.</p>
    </div>
  </div>

  <div class="bootcn-field bootcn-field--horizontal">
    <div class="bootcn-field__text">
      <label class="bootcn-label" for="bootcn-field-save">Save card</label>
      <p class="bootcn-field__help" id="bootcn-field-save-help">Use it for future payments.</p>
    </div>
    <div class="form-check form-switch bootcn-switch">
      <input
        class="form-check-input bootcn-switch__input"
        type="checkbox"
        role="switch"
        id="bootcn-field-save"
        name="save"
        aria-describedby="bootcn-field-save-help"
      />
    </div>
  </div>
</fieldset>
css
/* Fieldset: a titled group of fields. */
.bootcn-fieldset {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  min-width: 0;
  max-width: 28rem;
  margin: 0;
  padding: 0;
  border: 0;
}

/* The legend is not a flex item, so it gets its own spacing. */
.bootcn-fieldset__legend {
  float: none;
  width: auto;
  margin: 0 0 0.25rem;
  padding: 0;
  font-size: 1rem;
  font-weight: 600;
}

.bootcn-fieldset__description {
  margin: 0;
  color: var(--bootcn-muted-foreground);
  font-size: 0.875rem;
}

/* Fields side by side, wrapping on narrow screens. */
.bootcn-field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 6.5rem), 1fr));
  gap: 1rem;
}

.bootcn-field__error {
  margin: 0;
  color: var(--bootcn-destructive-text);
  font-size: 0.875rem;
}

.bootcn-field > .bootcn-label {
  line-height: 1.25rem;
}

/* Horizontal: text on the left, control on the right. */
.bootcn-field.bootcn-field--horizontal {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: var(--bootcn-radius-lg);
}

.bootcn-field__text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.bootcn-field--horizontal .bootcn-switch {
  display: block;
  margin: 0;
}

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.