doc/development/fe_guide/accessibility/best_practices.md
Since no ARIA is better than bad ARIA,
review the following recommendations before using aria-*, role, and tabindex.
Use semantic HTML, which has accessibility semantics baked in, and ideally test with
relevant combinations of screen readers and browsers.
In WebAIM's accessibility analysis of the top million home pages,
they found that "ARIA correlated to higher detectable errors".
It is likely that misuse of ARIA is a big cause of increased errors,
so when in doubt don't use aria-*, role, and tabindex and stick with semantic HTML.
By default, macOS limits the <kbd>tab</kbd> key to Text boxes and lists only. To enable full keyboard navigation:
You can read more about enabling browser-specific keyboard navigation on a11yproject.
aria-label.<gl-button icon="close" /> is used and not <gl-icon />.aria-label.role, tabindex or aria-* attributes unnecessary?div or span elements be replaced with a more semantic HTML element like p, button, or time?Headings are the primary mechanism used by screen reader users to navigate content. Therefore, the structure of headings on a page should make sense, like a good table of contents. We should ensure that:
h1 element on the page.To provide markup with accessible names, ensure every:
label.aria-label when there is no visible text, such as for an icon button with no content.alt attribute.fieldset has legend as its first child.figure has figcaption as its first child.table has caption as its first child.Remember that an alt attribute should not be longer than approximately 150 characters. While there's no official guidelines on the length, some screen readers will not read longer strings inside the alt attribute.
An accessible name can be provided in multiple ways and is decided with accessible name calculation. Here is the simplified order of different techniques taking precedence:
aria-labelledbyaria-labelalt, legend, figcaption or captiontitle.The following subsections contain examples of markup that render HTML elements with accessible names.
Note that when using GlFormGroup:
label prop renders a fieldset with a legend containing the label value.label and a label-for prop renders a label that points to the form input with the same label-for ID.Groups of checkboxes and radio inputs should be grouped together in a fieldset with a legend.
legend gives the group of checkboxes and radio inputs a label.
If the label, child text, or child element is not visually desired,
use the class name gl-sr-only to hide the element from everything but screen readers.
File input examples:
<!-- File input with a label -->
<label for="attach-file">{{ __('Attach a file') }}</label>
<input id="attach-file" type="file" />
<!-- File input with a hidden label -->
<label for="attach-file" class="gl-sr-only">{{ __('Attach a file') }}</label>
<input id="attach-file" type="file" />
Image examples:
<!-- SVGs implicitly have a graphics role so if it is semantically an image we should apply `role="img"` -->
<svg role="img" :alt="__('A description of the image')" />
<!-- A decorative image, hidden from screen readers -->
Buttons and links should have accessible names that are descriptive enough to be understood in isolation.
<!-- bad -->
<gl-button @click="handleClick">{{ __('Submit') }}</gl-button>
<gl-link :href="url">{{ __('page') }}</gl-link>
<!-- good -->
<gl-button @click="handleClick">{{ __('Submit review') }}</gl-button>
<gl-link :href="url">{{ __("GitLab's accessibility page") }}</gl-link>
In general, avoid using role.
Use semantic HTML elements that implicitly have a role instead.
| Bad | Good |
|---|---|
<div role="button"> | <button> |
<div role="img"> | `` |
<div role="link"> | <a> |
<div role="header"> | <h1> to <h6> |
<div role="textbox"> | <input> or <textarea> |
<div role="article"> | <article> |
<div role="list"> | <ol> or <ul> |
<div role="listitem"> | <li> |
<div role="table"> | <table> |
<div role="rowgroup"> | <thead>, <tbody>, or <tfoot> |
<div role="row"> | <tr> |
<div role="columnheader"> | <th> |
<div role="cell"> | <td> |
Keyboard users rely on focus outlines to understand where they are on the page. Therefore, if an element is interactive you must ensure:
Use semantic HTML, such as a (GlLink) and button (GlButton), which provides these behaviours by default.
Keep in mind that:
:hover styles, in most cases you should add :focus styles too so that the styling is applied for both mouse and keyboard users.outline, make sure you maintain visual focus state in another way such as with box-shadow.See the Pajamas Keyboard-only page for more detail.
tabindexPrefer no tabindex to using tabindex, since:
button (GlButton) implicitly provides tabindex="0".tabindexs interfere with this.tabindex="0" to make an element interactiveUse interactive elements instead of div and span tags.
For example:
button (GlButton).input or textarea.Once the markup is semantically complete, use CSS to update it to its desired visual state.
<!-- bad -->
<div role="button" tabindex="0" @click="expand">Expand</div>
<!-- good -->
<gl-button class="gl-p-0!" category="tertiary" @click="expand">Expand</gl-button>
tabindex="0" on interactive elementsInteractive elements are already tab accessible so adding tabindex is redundant.
<!-- bad -->
<gl-link href="help" tabindex="0">Help</gl-link>
<gl-button tabindex="0">Submit</gl-button>
<!-- good -->
<gl-link href="help">Help</gl-link>
<gl-button>Submit</gl-button>
tabindex="0" on elements for screen readers to readScreen readers can read text that is not tab accessible.
The use of tabindex="0" is unnecessary and can cause problems,
as screen reader users then expect to be able to interact with it.
<!-- bad -->
<p tabindex="0" :aria-label="message">{{ message }}</p>
<!-- good -->
<p>{{ message }}</p>
tabindexAlways avoid using tabindex="1"
or greater.
Icons can be split into three different types:
Icons are decorative when there's no loss of information to the user when they are removed from the UI.
As the majority of icons within GitLab are decorative, GlIcon automatically hides its rendered icons from screen readers.
Therefore, you do not need to add aria-hidden="true" to GlIcon, as this is redundant.
<!-- unnecessary: gl-icon hides icons from screen readers by default -->
<gl-icon name="rocket" aria-hidden="true" />
<!-- good -->
<gl-icon name="rocket" />
Icons convey information if there is loss of information to the user when they are removed from the UI.
An example is a confidential icon that conveys the issue is confidential, and does not have the text "Confidential" next to it.
Icons that convey information must have an accessible name so that the information is conveyed to screen reader users too.
<!-- bad -->
<gl-icon name="eye-slash" />
<!-- good -->
<gl-icon name="eye-slash" :aria-label="__('Confidential issue')" />
Icons that are clickable are semantically buttons, so they should be rendered as buttons, with an accessible name.
<!-- bad -->
<gl-icon name="close" :aria-label="__('Close')" @click="handleClick" />
<!-- good -->
<gl-button icon="close" category="tertiary" :aria-label="__('Close')" @click="handleClick" />
Use the following table to hide elements from users, when appropriate.
| Hide from sighted users | Hide from screen readers | Hide from both sighted and screen reader users |
|---|---|---|
.gl-sr-only | aria-hidden="true" | display: none, visibility: hidden, or hidden attribute |
To reduce noise for screen reader users, hide decorative images using alt="".
If the image is not an img element, such as an inline SVG, you can hide it by adding both role="img" and alt="".
gl-icon components automatically hide their icons from screen readers so aria-hidden="true" is
unnecessary when using gl-icon.
<!-- good - decorative images hidden from screen readers -->
<svg role="img" alt="" />
<gl-icon name="work-item-epic" />
No ARIA is required when using semantic HTML, because it already incorporates accessibility.
However, there are some UI patterns that do not have semantic HTML equivalents. General examples of these are dialogs (modals) and tabs. GitLab-specific examples are assignee and label dropdowns. Building such widgets require ARIA to make them understandable to screen readers. Proper research and testing should be done to ensure compliance with WCAG.