Forms are notoriously challenging to style and make accessible. Natura11y provides basic style and behavior for building user friendly, accessible forms.
Accessibility Spotlight
Form elements include a consistent visible focus for keyboard users.
intuitively mark required fields. When doing so, Natura11y adds required and data-required attributes automatically.
Checkbox and radio groups use semantic fieldset and legend tags.
Form Entries
The form entry parent selector (.form-entry) wraps each entry within a form. Child elements within form entries vary depending on their input types. Such types include text inputs, textareas, selects, checkboxes, and radios.
In Figure 1, the .form-entry selector contains an <input> field with the type="text" attribute.
The .form-entry__field__input grandchild selector is required.
It holds the actual <textarea> tag.
File Upload
Natura11y offers a simple file upload component, as shown in Figure 4 below.
Checkboxes
Checkbox (and radio) groups use semantic <fieldset> and <legend> tags. They stack vertically, with a healthy amount of padding. This helps to ensure an accessible target size.
Figure 5 provides the markup required to create a checkbox group.
For a single checkbox, do not use a <fieldset> and <legend>. Instead, provide an id on the .form-entry__field__label selector (Figure 6, line 5). Then associate it with aria-labeledby placed on the surrounding <label> tag (Figure 6, lines 13).
Checkbox Switch
Checkboxes can transform into switches with just a couple of of changes to the markup.
First, change the .form-entry__option__check to .form-entry__option__switch (Figure 7, line 11). Then, above the switch label, add an empty <span> with a .switch__slider class (Figure 7, line 19).
Radios
Like checkboxes, radio groups use semantic <fieldset> and <legend> tags.
Natura11y makes it easy to mark required fields. Add data-required="true" to the parent .form-group element of the required field. On the same .form-group element, include a custom error message using the data-error-message={x} attribute. Here x equals the message displayed to the user during the validation process.
The two data attributes mentioned above are shown in Figure 9, lines 3 and 4. When present on the .form group selector, Naturally's Javascript adds the necessary required and data-required attributes to the form entry's child <input> element automatically.
With the previous example, notice the red asterisk placed before the label. This too, is added automatically to required fields (via Natura11y's CSS file). The asterisk has become the standard identifier for required fields.
Form Validation
Natura11y provides superficial client-side form validation. Include the novalidate attribute on the <form> tag.
Floating Labels
For short forms (such as a sign in form) floating labels would be fine to use. Add the .form-entry__field--float modifier class to the .form-entry__field selector (see Figure 11, line 6).
Related CSS Variables
Further customize Forms by redefining any of their related CSS variables as shown below.