mirror of
https://github.com/twbs/bootstrap.git
synced 2024-11-29 11:24:18 +01:00
Docs: forms accessibility cleanup (backport from v5) (#31234)
* Expand advice for anchor-based controls * Expand accessibility note in input group * Correct statement about validation, fix server example * Tweak label > accessible name Co-authored-by: XhmikosR <xhmikosr@gmail.com> Co-authored-by: Mark Otto <markd.otto@gmail.com>
This commit is contained in:
parent
38bcf108c9
commit
99013a5c54
@ -258,7 +258,7 @@ Group checkboxes or radios on the same horizontal row by adding `.form-check-inl
|
|||||||
|
|
||||||
### Without labels
|
### Without labels
|
||||||
|
|
||||||
Add `.position-static` to inputs within `.form-check` that don't have any label text. Remember to still provide some form of label for assistive technologies (for instance, using `aria-label`).
|
Add `.position-static` to inputs within `.form-check` that don't have any label text. Remember to still provide some form of accessible name for assistive technologies (for instance, using `aria-label`).
|
||||||
|
|
||||||
{% capture example %}
|
{% capture example %}
|
||||||
<div class="form-check">
|
<div class="form-check">
|
||||||
@ -732,7 +732,9 @@ Add the `disabled` attribute to a `<fieldset>` to disable all the controls withi
|
|||||||
{% capture callout %}
|
{% capture callout %}
|
||||||
##### Caveat with anchors
|
##### Caveat with anchors
|
||||||
|
|
||||||
By default, browsers will treat all native form controls (`<input>`, `<select>` and `<button>` elements) inside a `<fieldset disabled>` as disabled, preventing both keyboard and mouse interactions on them. However, if your form also includes `<a ... class="btn btn-*">` elements, these will only be given a style of `pointer-events: none`. As noted in the section about [disabled state for buttons]({{ site.baseurl }}/docs/{{ site.docs_version }}/components/buttons/#disabled-state) (and specifically in the sub-section for anchor elements), this CSS property is not yet standardized and isn't fully supported in Internet Explorer 10, and won't prevent keyboard users from being able to focus or activate these links. So to be safe, use custom JavaScript to disable such links.
|
Browsers treat all native form controls (`<input>`, `<select>`, and `<button>` elements) inside a `<fieldset disabled>` as disabled, preventing both keyboard and mouse interactions on them.
|
||||||
|
|
||||||
|
However, if your form also includes custom button-like elements such as `<a ... class="btn btn-*">`, these will only be given a style of `pointer-events: none`. As noted in the section about [disabled state for buttons]({{ site.baseurl }}/docs/{{ site.docs_version }}/components/buttons/#disabled-state) (and specifically in the sub-section for anchor elements), this CSS property is not yet standardized and isn't fully supported in Internet Explorer 10. The anchor-based controls will also still be focusable and operable using the keyboard. You must manually modify these controls by adding `tabindex="-1"` to prevent them from receiving focus and `aria-disabled="disabled"` to signal their state to assistive technologies.
|
||||||
{% endcapture %}
|
{% endcapture %}
|
||||||
{% include callout.html content=callout type="warning" %}
|
{% include callout.html content=callout type="warning" %}
|
||||||
|
|
||||||
@ -748,7 +750,7 @@ While Bootstrap will apply these styles in all browsers, Internet Explorer 11 an
|
|||||||
Provide valuable, actionable feedback to your users with HTML5 form validation–[available in all our supported browsers](https://caniuse.com/#feat=form-validation). Choose from the browser default validation feedback, or implement custom messages with our built-in classes and starter JavaScript.
|
Provide valuable, actionable feedback to your users with HTML5 form validation–[available in all our supported browsers](https://caniuse.com/#feat=form-validation). Choose from the browser default validation feedback, or implement custom messages with our built-in classes and starter JavaScript.
|
||||||
|
|
||||||
{% capture callout %}
|
{% capture callout %}
|
||||||
We currently recommend using custom validation styles, as native browser default validation messages are not consistently exposed to assistive technologies in all browsers (most notably, Chrome on desktop and mobile).
|
We are aware that currently the client-side custom validation styles and tooltips are not accessible, since they are not exposed to assistive technologies. While we work on a solution, we'd recommend either using the server-side option or the default browser validation method.
|
||||||
{% endcapture %}
|
{% endcapture %}
|
||||||
{% include callout.html content=callout type="warning" %}
|
{% include callout.html content=callout type="warning" %}
|
||||||
|
|
||||||
@ -912,6 +914,8 @@ While these feedback styles cannot be styled with CSS, you can still customize t
|
|||||||
|
|
||||||
We recommend using client-side validation, but in case you require server-side validation, you can indicate invalid and valid form fields with `.is-invalid` and `.is-valid`. Note that `.invalid-feedback` is also supported with these classes.
|
We recommend using client-side validation, but in case you require server-side validation, you can indicate invalid and valid form fields with `.is-invalid` and `.is-valid`. Note that `.invalid-feedback` is also supported with these classes.
|
||||||
|
|
||||||
|
For invalid fields, ensure that the invalid feedback/error message is associated with the relevant form field using `aria-describedby`. This attribute allows more than one `id` to be referenced, in case the field already points to additional form text.
|
||||||
|
|
||||||
{% capture example %}
|
{% capture example %}
|
||||||
<form>
|
<form>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
@ -933,36 +937,36 @@ We recommend using client-side validation, but in case you require server-side v
|
|||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<div class="col-md-6 mb-3">
|
<div class="col-md-6 mb-3">
|
||||||
<label for="validationServer03">City</label>
|
<label for="validationServer03">City</label>
|
||||||
<input type="text" class="form-control is-invalid" id="validationServer03" required>
|
<input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback" required>
|
||||||
<div class="invalid-feedback">
|
<div id="validationServer03Feedback" class="invalid-feedback">
|
||||||
Please provide a valid city.
|
Please provide a valid city.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-3 mb-3">
|
<div class="col-md-3 mb-3">
|
||||||
<label for="validationServer04">State</label>
|
<label for="validationServer04">State</label>
|
||||||
<select class="custom-select is-invalid" id="validationServer04" required>
|
<select class="custom-select is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback" required>
|
||||||
<option selected disabled value="">Choose...</option>
|
<option selected disabled value="">Choose...</option>
|
||||||
<option>...</option>
|
<option>...</option>
|
||||||
</select>
|
</select>
|
||||||
<div class="invalid-feedback">
|
<div id="validationServer04Feedback" class="invalid-feedback">
|
||||||
Please select a valid state.
|
Please select a valid state.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-3 mb-3">
|
<div class="col-md-3 mb-3">
|
||||||
<label for="validationServer05">Zip</label>
|
<label for="validationServer05">Zip</label>
|
||||||
<input type="text" class="form-control is-invalid" id="validationServer05" required>
|
<input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback" required>
|
||||||
<div class="invalid-feedback">
|
<div id="validationServer05Feedback" class="invalid-feedback">
|
||||||
Please provide a valid zip.
|
Please provide a valid zip.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="form-check">
|
<div class="form-check">
|
||||||
<input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" required>
|
<input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" aria-describedby="invalidCheck3Feedback" required>
|
||||||
<label class="form-check-label" for="invalidCheck3">
|
<label class="form-check-label" for="invalidCheck3">
|
||||||
Agree to terms and conditions
|
Agree to terms and conditions
|
||||||
</label>
|
</label>
|
||||||
<div class="invalid-feedback">
|
<div id="invalidCheck3Feedback" class="invalid-feedback">
|
||||||
You must agree before submitting.
|
You must agree before submitting.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -370,6 +370,15 @@ Input groups include support for custom selects and custom file inputs. Browser
|
|||||||
|
|
||||||
## Accessibility
|
## Accessibility
|
||||||
|
|
||||||
Screen readers will have trouble with your forms if you don't include a label for every input. For these input groups, ensure that any additional label or functionality is conveyed to assistive technologies.
|
Ensure that all form controls have an appropriate accessible name so that their purpose can be conveyed to users of assistive technologies. The simplest way to achieve this is to use a `<label>` element, or—in the case of buttons—to include sufficiently descriptive text as part of the `<button>...</button>` content.
|
||||||
|
|
||||||
The exact technique to be used (`<label>` elements hidden using the `.sr-only` class, or use of the `aria-label` and `aria-labelledby` attributes, possibly in combination with `aria-describedby`) and what additional information will need to be conveyed will vary depending on the exact type of interface widget you're implementing. The examples in this section provide a few suggested, case-specific approaches.
|
For situations where it's not possible to include a visible `<label>` or appropriate text content, there are alternative ways of still providing an accessible name, such as:
|
||||||
|
|
||||||
|
- `<label>` elements hidden using the `.visually-hidden` class
|
||||||
|
- Pointing to an existing element that can act as a label using `aria-labelledby`
|
||||||
|
- Providing a `title` attribute
|
||||||
|
- Explicitly setting the accessible name on an element using `aria-label`
|
||||||
|
|
||||||
|
If none of these are present, assistive technologies may resort to using the `placeholder` attribute as a fallback for the accessible name on `<input>` and `<textarea>` elements. The examples in this section provide a few suggested, case-specific approaches.
|
||||||
|
|
||||||
|
While using visually hidden content (`.sr-only`, `aria-label`, and even `placeholder` content, which disappears once a form field has content) will benefit assistive technology users, a lack of visible label text may still be problematic for certain users. Some form of visible label is generally the best approach, both for accessibility and usability.
|
||||||
|
Loading…
Reference in New Issue
Block a user