See what's coming soon with Carbon V10-beta.

Skip to main content
Forms are used for submitting data.

Form

Vanilla JS

<!-- 
  Copyright IBM Corp. 2016, 2018

  This source code is licensed under the Apache-2.0 license found in the
  LICENSE file in the root directory of this source tree.
-->

<div class="bx--form-item">
    <input id="text-input-3" type="text" class="bx--text-input" placeholder="Optional placeholder text">
    <label for="text-input-3" class="bx--label">Text Input label</label>
</div>
<div class="bx--form-item">
    <textarea id="text-area-2" class="bx--text-area" rows="4" cols="50" placeholder="Placeholder text."></textarea>
    <label for="text-area-2" class="bx--label">Text Area label</label>
</div>
<div class="bx--form-item">
  <div class="bx--select">
    <select id="select-id" class="bx--select-input">
          <option class="bx--select-option" value="" disabled selected hidden>Choose an option</option>
          <option class="bx--select-option" value="solong">A much longer option that is worth having around to check how text flows</option>
          <optgroup class="bx--select-optgroup" label="Category 1">
              <option class="bx--select-option" value="option1">Option 1</option>
              <option class="bx--select-option" value="option2">Option 2</option>
          </optgroup>
          <optgroup class="bx--select-optgroup" label="Category 2">
              <option class="bx--select-option" value="option1">Option 1</option>
              <option class="bx--select-option" value="option2">Option 2</option>
          </optgroup>
    </select>
      <svg class="bx--select__arrow" width="10" height="5" viewBox="0 0 10 5">
        <path d="M0 0l5 4.998L10 0z" fill-rule="evenodd" />
      </svg>
      <label for="select-id" class="bx--label">Select label</label>
  </div>
</div>
<div class="bx--form-item">
  <button class="bx--btn bx--btn--primary" type="button">Submit</button>
</div>

Documentation

SCSS

Modifiers

Modifiers are used with various form-related classes.

Selector Description
.bx--label--disabled Applies disabled styles for a label

FAQ

Using Form Requirement

Bluemix Components provides HTML attribtues and CSS to enable form validations for each input or control.

For example, here's a Form Item with a required text input.

<div class="bx--form-item">
  <label for="text1" class="bx--label">Username</label>
  <input required id="text1" type="text" class="bx--text__input" placeholder="Enter username here" />
  <div class="bx--form-requirement">Username is taken.</div>
</div>

The bx--form-requirement element will be hidden until data-invalid attribute gets added to the input. Validate the text input on your own and then use JavaScript to add the attribute if the input value is invalid.

<div class="bx--form-item">
  <label for="text1" class="bx--label">Username</label>
  <input data-invalid required id="text1" type="text" class="bx--text__input" placeholder="Enter username here" />
  <div class="bx--form-requirement">Username is taken.</div>
</div>

Now that data-invalid is added to the input, the bx--form-requirement will appear.

HTML

Bluemix Components provides inputs (checkboxes, text-inputs, etc.) and some default styles for forms:

  • .bx--form-item
  • .bx--fieldset
  • .bx--label
  • .bx--form-requirement

Make use of HTML to compose and structure forms appropriate to your project's needs.

For example, here's a simple form for a login page that uses a mix of HTML and Bluemix Components.

<form>
  <section>
    <div class="bx--form-item">
      <label for="text1" class="bx--label">Username</label>
      <input data-invalid id="your-username-id" type="text" class="bx--text__input" placeholder="Enter username here" />
      <div class="bx--form-requirement">Username is taken.</div>
    </div>
    <div class="bx--form-item">
      <label for="text1" class="bx--label">Password</label>
      <input data-invalid id="your-password-id" type="password" class="bx--text__input" placeholder="Enter username here" />
      <div class="bx--form-requirement">Password must rhyme with Batman.</div>
    </div>
  </section>
  <fieldset>
    <legend>Click Register when you're ready!</legend>
    <button class="bx--btn bx--btn--primary" type="submit">Register</button>
  </fieldset>
</form>

You can use any appropriate HTML for structuring and grouping your forms. If you want, those <section> elements could be <div> elements. Or you can change the <fieldset> element to be a <section> if that's what you want.

Fieldset and Legend

It's best practice to wrap any groups of checkboxes or radio inputs with <fieldset> and use <legend> to label the group. This best practice applies mainly to composing forms where users are submitting data.

Here's an example from MDN that explains why this is a best practice.

The <legend> element formally describes the purpose of the <fieldset> element. Many assistive technologies will use the <legend> element as if it is a part of the label of each widget inside the corresponding <fieldset> element.

<form>
  <fieldset>
    <legend>Fruit juice size</legend>
    <p><input type="radio" name="size" id="size_1" value="small" /> <label for="size_1">Small</label></p>
    <p><input type="radio" name="size" id="size_2" value="medium" /> <label for="size_2">Medium</label></p>
    <p><input type="radio" name="size" id="size_3" value="large" /> <label for="size_3">Large</label></p>
  </fieldset>
</form>

With this example, a screen reader will pronounce "Fruit juice size small" for the first widget, "Fruit juice size medium" for the second, and "Fruit juice size large" for the third.