Fork me on GitHub

Required files

SCSS files required: _forms.controls.scss, _forms.selects.scss, _forms.radios.scss, _forms.checkboxes.scss, _forms.labels.scss

SCSS files optional: _objects.form-layout.scss, _forms.validation.scss

Form layout

Note: some variables are in the settings file settings.forms.scss and can be reassigned after the include of the file if required

Use the class .form on the element that contains all the form elements

Form group

Use the class .form__group to wrap each form field or group of fields. You could apply the class to a <fieldset> as it has a similar meaning.

Form group includes a bottom margin of 30px (variable: $form-group-base-spacing) to give space between fields

Note: global spacing helper classes can also be applied .m-b-none, .m-b-xs, .m-b-sm, .m-b-md etc.

Form field

Use the class .form__field for each field (placement of class varies across elements).

The form field includes the max-width style (variable: $form-field-max-width)

Note: Form fields can be used for the placement of error messages directly underneath, therefore it is best not to add a bottom margin

Form label

Use the class .form__label for a form field label - which is styled as small, bold font.

The form label includes the max-width style (variable: $form-field-max-width)

Disabled fields

All fields can use the disabled attribute which will apply the disabled styles, however the select element needs a class .is-disabled added to the .select

Form validation

The following modifying classes can be applied the form group .form__group--error and .form__group--valid, which will apply validation styling to the field inside.

Error messages can be wrapped in the class .form__error and placed directly following the form field

Inputs

Use the class .form__control directly on a textual input

Note: the code example below uses other wrappers and classes from the above general guidelines which helps with layout and validation etc.

<div class="form__group">
    <label class="form__label">Input field</label>
    <input class="form__control form__field" type="text" placeholder="Sample placeholder text">
</div>
This is an error message.

Text areas

Use the class .form__control directly on a textarea

Note: the code example below uses other wrappers and classes from the above general guidelines which helps with layout and validation etc.

<div class="form__group">
    <label class="form__label">Text area</label>
    <textarea class="form__control form__field" type="text"></textarea>
</div>
This is an error message.

Selects

Select elements are styled via a wrapping <div> which requires the class .select

Use the class .form__control directly on a select

The chevron icon is a background image applied to the .select:after element. See here for info on how to deal with background images in the UI Toolkit.

Note: the code example below uses other wrappers and classes from the above general guidelines which helps with layout and validation etc.

<div class="form__group">
    <label class="form__label">Select dropdown</label>
    <div class="select form__field">
        <select class="form__control">
            <option value="00">00</option>
            <option selected value="01">01</option>
            <option value="02">02</option>
        </select>
    </div>
</div>
This is an error message.

Fields required

Add the modifying class to the form label .form__label--required

Alternative form controls

To apply the alternative form control styling use the modifying class .form__control--alt to any textual inputs, textareas or selects

Annoyingly the select wrapper .select also needs a class .select--alt to move the chevron flush to the right

Note: global helper classes are available to modify the styling and make a newsletter signup for example


Checkboxes

Checkbox elements are styled via a wrapping <label> which requires the class .checkbox.

Inside the label element should be the checkbox <input> followed by a <span> with the text required.

<label class="checkbox form__field" for="checkboxExample1">
    <input type="checkbox" name="checkboxExample1" id="checkboxExample1" checked="checked" value="value1">
    <span>This is an example of a checkbox</span>
</label>
This is an error message.

Radios

Radio elements are styled via a wrapping <label> which requires the class .radio.

Inside the label element should be the radio <input> followed by a <span> with the text required.

Radios that are functionally grouped together via the name attribute can have a wrapping div with the class .form__group for styling as a group.

Radios can also be made inline by applying the modifying class .radio--inline

<div class="form__group">
    <label class="radio form__field" for="radioExample1">
        <input type="radio" id="radioExample1" name="radioExample" value="radioExample1">
        <span>Radio example one</span>
    </label>
    <label class="radio form__field" for="radioExample2">
        <input type="radio" id="radioExample2" name="radioExample" value="radioExample2">
        <span>Radio example two</span>
    </label>
    <label class="radio form__field" for="radioExample3">
        <input type="radio" id="radioExampleDisabled" name="radioExample" value="radioExampleDisabled" disabled>
        <span>Radio example disabled</span>
    </label>
</div>

Fields inline

To display form fields inline, wrap all the form groups in a wrapper with a class .form__groups-inline

To include any other elements eg. button or text amongst the inline elements, they should also be wrapped with the class .form__group

Note that the fields are now inline-block and won't stretch to 100%, so you can add a min width helper class eg. .min-width-md

Fields in a grid

Fields can easily be put in a grid. When a .form__group is used, the margin needs to be removed as the grid takes care of spacing.

The grid below has a .grid--gutter-sm modifying class applied and a .m-b-none helper class on each form__group element.