Monday June 14, 2021 By David Quintanilla
JavaScript Requirements For Accessible Components — Smashing Magazine

Spoiler alert: tooltips, modals, tabs, carousels, and dropdown menus are among the person interface parts that require greater than CSS. To make sure accessibility of your interface, JavaScript is a obligatory addition to perform focus administration, reply to keyboard occasions, and toggle ARIA attributes.

Because the writer of ModernCSS.dev, I’m a giant proponent of CSS options. And, I really like seeing the intelligent methods folks use CSS for actually out-of-the-box designs and interactivity! Nonetheless, I’ve seen a development towards selling “CSS-only” parts utilizing strategies just like the “checkbox hack”. Sadly, hacks like these depart a big quantity of customers unable to make use of your interface.

This articles covers a number of widespread parts and why CSS isn’t enough for protecting accessibility by detailing the JavaScript necessities. These necessities are primarily based on the Web Content Accessibility Guidelines (WCAG) and extra analysis from accessibility consultants. I received’t prescribe JavaScript options or demo CSS, however fairly study what must be accounted for when creating every part. A JavaScript framework can definitely be used however is just not obligatory to be able to add the occasions and options mentioned.

The necessities listed are by and enormous not non-compulsory — they’re obligatory to assist make sure the accessibility of your parts.

In case you’re utilizing a framework or part library, you should utilize this text to assist consider if the offered parts meet accessibility necessities. It’s necessary to know that lots of the gadgets famous will not be going to be absolutely coated by automated accessibility testing instruments like aXe, and subsequently want some guide testing. Or, you should utilize a testing framework like Cypress to create assessments for the required performance.

Needless to say this text is targeted on informing you of JavaScript issues for every interface part. This isn’t a complete useful resource for all of the implementation particulars for creating absolutely accessible parts, equivalent to obligatory aria and even markup. Sources are included for every sort to help you in studying extra concerning the wider issues for every part.

Figuring out If CSS-Solely Is An Acceptable Resolution

Listed here are a number of inquiries to ask earlier than you proceed with a CSS-only resolution. We’ll cowl among the phrases introduced right here in additional context alongside their associated parts.

  • Is that this to your personal enjoyment?
    Then completely go all in on CSS, push the boundaries, and study what the language can do! 🎉
  • Does the function embody displaying and hiding of content material?
    Then you definately want JS to at minimal toggle aria and to allow closing on Esc. For sure sorts of parts that additionally change state, you might also want to speak adjustments by triggering updates inside an ARIA reside area.
  • Is the pure focus order probably the most ultimate?
    If the pure order looses the connection between a set off and the ingredient it triggered, or a keyboard person can’t even entry the content material through pure tab order, you then want JS to help in focus administration.
  • Does the stylized management provide the right details about the performance?
    Customers of assistive know-how like display screen readers obtain data primarily based on semantics and ARIA that helps them decide what a management does. And, customers of speech recognition want to have the ability to establish the part’s label or sort to work out the phrase to make use of to function the controls. For instance, in case your part is styled like tabs however makes use of radio buttons to “work” like tabs, a display screen reader might hear “radio button” and a speech person might attempt to use the phrase “tab” to function them. In these circumstances, you’ll want JS to allow utilizing the suitable controls and semantics to realize the specified performance.
  • Does the impact depend on hover and/or focus?
    Then you could want JS to help in an alternate resolution for offering equal entry or persistent entry to the content material particularly for contact display screen customers and people utilizing desktop zoom of 200%+ or magnification software program.

Fast tip: One other reference if you’re creating any type of custom-made management is the Custom Control Accessible Development Checklist from the W3 “Utilizing ARIA” information. This mentions a number of factors above, with a number of further design and semantic issues.


Narrowing the definition of a tooltip is a bit difficult, however for this part we’re speaking about small textual content labels that seem on mouse hover close to a triggering ingredient. They overlay different content material, don’t require interplay, and disappear when a person removes hover or focus.

Example tooltips from GitHub, Whimsical, and Notion
Instance tooltips from GitHub, Whimsical, and Notion. (Large preview)

The CSS-only resolution right here could appear utterly high quality, and may be completed with one thing like:

<button class="tooltip-trigger">I've a tooltip</button>
<span class="tooltip">Tooltip</span>

.tooltip {
show: none;

.tooltip-trigger:hover + .tooltip,
.tooltip-trigger:focus + .tooltip {
show: block;

Nonetheless, this ignores fairly an inventory of accessibility considerations and excludes many customers from accessing the tooltip content material.

A big group of excluded customers are these utilizing contact screens the place :hover will presumably not be triggered since on contact screens, a :hover occasion triggers in sync with a :focus occasion. Which means any associated motion related to the triggering ingredient — equivalent to a button or hyperlink — will fireplace alongside the tooltip being revealed. This implies the person might miss the tooltip, or not have time to learn its contents.

Within the case that the tooltip is connected to an interactive ingredient with no occasions, the tooltip might present however not be dismissible till one other ingredient good points focus, and within the meantime might block content material and stop a person from doing a activity.

Moreover, customers who want to make use of zoom or magnification software program to navigate additionally expertise fairly a barrier to utilizing tooltips. Since tooltips are revealed on hover, if these customers want to alter their discipline of view by panning the display screen to learn the tooltip it might trigger it to vanish. Tooltips additionally take away management from the person as there’s usually nothing to inform the person a tooltip will seem forward of time. The overlay of content material might stop them from doing a activity. In some circumstances equivalent to a tooltip tied to a type discipline, cell or different on-screen keyboards can obscure the tooltip content material. And, if they don’t seem to be appropriately related to the triggering ingredient, some assistive know-how customers might not even know a tooltip has appeared.

Steering for the conduct of tooltips comes from WCAG Success Criterion 1.4.13 — Content on Hover or Focus. This criterion is meant to assist low imaginative and prescient customers and people utilizing zoom and magnification software program. The guiding rules for tooltip (and different content material showing on hover and focus) embody:

  • Dismissible
    The tooltip may be dismissed with out shifting hover or focus
  • Hoverable
    The revealed tooltip content material may be hovered with out it disappearing
  • Persistent
    The extra content material doesn’t disappear primarily based on a timeout, however waits for a person to take away hover or focus or in any other case dismiss it

To totally meet these pointers requires some JavaScript help, notably to permit for dismissing the content material.

  • Customers of assistive know-how will assume that dismissal conduct is tied to the Esc key, which requires a JavaScript listener.
  • Based on Sarah Higley’s analysis described within the subsequent part, including a visual “shut” button inside the tooltip would additionally require JavaScript to deal with its shut occasion.
  • It’s doable that JavaScript might have to reinforce your styling resolution to make sure a person can hover over the tooltip content material with out it dismissing through the person shifting their mouse.

Options To Tooltips

Tooltips ought to be a final resort. Sarah Higley — an accessibility skilled who has a specific ardour for dissuading the usage of tooltips — affords this easy check:

“Why am I including this textual content to the UI? The place else might it go?”

— Sarah Higley from the presentation “Tooltips: Investigation Into Four Parts

Based mostly on analysis Sarah was concerned with for her function at Microsoft, an alternate resolution is a devoted “toggletip”. Primarily, this implies offering a further ingredient to permit a person to deliberately set off the displaying and hiding of additional content material. Not like tooltips, toggletips can retain semantics of parts inside the revealed content material. Additionally they give the person again the management of toggling them, and retain discoverability and operability by extra customers and particularly contact display screen customers.

In case you’ve remembered the title attribute exists, simply know that it suffers all the identical points we famous from our CSS-only resolution. In different phrases — don’t use title underneath the belief it’s an appropriate tooltip resolution.

For extra data, try Sarah’s presentation on YouTube in addition to her extensive article on tooltips. To study extra about tooltips versus toggletips and a bit extra data on why to not use title, assessment Heydon Pickering’s article from Inclusive Elements: Tooltips and Toggletips.


Modals — aka lightboxes or dialogs — are in-page home windows that seem after a triggering motion. They overlay different web page content material, might comprise structured data together with further actions, and infrequently have a semi-transparent backdrop to assist distinguish the modal window from the remainder of the web page.

Example modals from GitHub and Material Design
Instance modals from GitHub and Materials Design. (Large preview)

I’ve seen a number of variations of a CSS-only modal (and am responsible of constructing one for an older model of my portfolio). They could use the “checkbox hack”, make use of the conduct of :goal, or attempt to style it off of :focus (which might be actually an overlarge tooltip in disguise).

As for the HTML dialog ingredient, remember that it isn’t thought-about to be comprehensively accessible. So, whereas I completely encourage people to make use of native HTML earlier than customized options, sadly this one breaks that concept. You’ll be able to study extra about why the HTML dialog isn’t accessible.

Not like tooltips, modals are meant to permit structured content material. This implies doubtlessly a heading, some paragraph content material, and interactive parts like hyperlinks, buttons and even kinds. To ensure that probably the most customers to entry that content material, they have to have the ability to use keyboard occasions, notably tabbing. For longer modal content material, arrow keys must also retain the flexibility to scroll. And like tooltips, they need to be dismissible with the Esc key — and there’s no solution to allow that with CSS-only.

JavaScript is required for focus administration inside modals. Modals ought to entice focus, which suggests as soon as focus is inside the modal, a person ought to not have the ability to tab out of it into the web page content material behind it. However first, focus has to get inside of the modal, which additionally requires JavaScript for a completely accessible modal resolution.

Right here’s the sequence of modal associated occasions that should be managed with JavaScript:

  1. Occasion listener on a button opens the modal
  2. Focus is positioned inside the modal; which ingredient varies primarily based on modal content material (see resolution tree)
  3. Focus is trapped inside the modal till it’s dismissed
  4. Ideally, a person is ready to shut a modal with the Esc key along with a devoted shut button or a damaging button motion equivalent to “Cancel” if acknowledgement of modal content material is required
    1. If Esc is allowed, clicks on the modal backdrop must also dismiss the modal
  5. Upon dismissal, if no navigation occurred, focus is positioned again on the triggering button ingredient

Based mostly on the WAI-ARIA Authoring Practices Modal Dialog Example, here’s a simplified resolution tree for the place to put focus as soon as a modal is opened. Context will all the time dictate the selection right here, and ideally focus is managed additional than merely “the primary focusable ingredient”. In truth, typically non-focusable parts should be chosen.

  • Major topic of the modal is a type.
    Focus first type discipline.
  • Modal content material is critical in size and pushes modal actions out of view.
    Focus a heading if current, or first paragraph.
  • Objective of the modal is procedural (instance: affirmation of motion) with a number of out there actions.
    Give attention to the “least damaging” motion primarily based on the context (instance: “OK”).
  • Objective of the modal is procedural with one motion.
    Give attention to the primary focusable ingredient

Fast tip: Within the case of needing to focus a non-focusable ingredient, equivalent to a heading or paragraph, add tabindex="-1" which permits the ingredient to turn out to be programmatically focusable with JS however doesn’t add it to the DOM tab order.

Check with the WAI-ARIA modal demo for extra data on different necessities for organising ARIA and extra particulars on choose which ingredient so as to add focus to. The demo additionally consists of JavaScript to exemplify do focus administration.

For a ready-to-go resolution, Kitty Giraudel has created a11y-dialog which incorporates the function necessities we mentioned. Adrian Roselli additionally has researched managing focus of modal dialogs and created a demo and compiled data on how completely different browser and display screen reader combos will talk the targeted ingredient.


Tabbed interfaces contain a sequence of triggers that show corresponding content material panels one after the other. The CSS “hacks” you could discover for these contain utilizing stylized radio buttons, or :goal, which each enable solely revealing a single panel at a time.

Example tabs from Shopify Polaris and IBM Carbon
Instance tabs from Shopify Polaris and IBM Carbon. (Large preview)

Listed here are the tab options that require JavaScript:

  1. Toggling the aria-selected attribute to true for the present tab and false for unselected tabs
  2. Making a roving tabindex to differentiate tab choice from focus
  3. Transfer focus between tabs by responding to arrow key occasions (and optionally Residence and Finish)

Optionally, you may make tab choice comply with focus — that means when a tab is targeted its additionally then chosen and exhibits its related tab panel. The WAI-ARIA Authoring Practices affords this guide to making a choice for whether selection should follow focus.

Whether or not or not you select to have choice comply with focus, additionally, you will use JavaScript to hear for arrow key occasions to maneuver focus between tab parts. That is an alternate sample to permit navigation of tab choices since the usage of a roving tabindex (described subsequent) alters the pure keyboard tab focus order.

About Roving tabindex

The idea of a roving tabindex is that the worth of the tabindex worth is programmatically managed to handle the main target order of parts. With regard to tabs, which means solely the chosen tab is a part of the main target order by the use of setting tabindex="0", and unselected tabs are set to tabindex="-1" which removes them from the pure keyboard focus order.

The rationale for that is in order that when a tab is chosen, the subsequent tab will land a person’s focus inside the related tab panel. It’s possible you’ll select to make the ingredient that’s the tab panel focusable by assigning it tabindex="0", or that will not be obligatory if there’s a assure of a focusable ingredient inside the tab panel. In case you tab panel content material will probably be extra variable or complicated, you could contemplate managing focus in keeping with the choice tree we reviewed for modals.

Instance Tab Patterns

Listed here are some reference patterns for creating tabs:


Additionally referred to as slideshows or sliders, carousels contain a sequence of rotating content material panels (aka “slides”) that embody management mechanisms. You’ll discover these in lots of configurations with a variety of content material. They’re considerably notoriously thought-about a bad design pattern.

An example carousel demo created with bxSlider
An instance carousel demo created with bxSlider. (Large preview)

The difficult half about CSS-only carousels is that they could not provide controls, or they could use sudden controls to control the carousel motion. For instance, you may once more use the “checkbox hack” to trigger the carousel to transition, however checkboxes impart the mistaken sort of data concerning the interplay to customers of assistive tech. Moreover, in case you type the checkbox labels to visually seem as ahead and backward arrows, you’re probably to offer customers of speech recognition software program the mistaken impression of what they need to say to regulate the carousel.

Extra not too long ago, native CSS support for scroll snap has landed. At first, this looks as if the right CSS-only resolution. However, even automated accessibility checking will flag these as un-navigable by keyboard customers in case there’s there no solution to navigate them through interactive parts. There are different accessibility and person expertise considerations with the default conduct of this function, a few of which I’ve included in my scroll snap demo on SmolCSS.

Regardless of the big selection in how carousels look, there are some widespread traits. One choice is to create a carousel using tab markup since successfully it’s the identical underlying interface with an altered visible presentation. In comparison with tabs, carousels might provide further controls for earlier and subsequent, and in addition pause if the carousel is auto-playing.

The next are JavaScript issues relying in your carousel options:

  • Utilizing Paginated Controls
    Upon collection of a numbered merchandise, programmatically focus the related carousel slide. This may contain organising slide containers utilizing roving tabindex as a way to focus the present slide, however stop entry to off-screen slides.
  • Utilizing Auto-Play
    Embrace a pause management, and in addition allow pausing when the slide is hovered or an interactive ingredient inside it’s targeted. Moreover, you may check for prefers-reduced-motion within JavaScript to load the slideshow in a paused state to respect person preferences.
  • Utilizing Earlier/Subsequent Controls
    Embrace a visually hidden ingredient marked as aria-live="well mannered" and upon these controls being activated, populate the reside area with a sign of the present place, equivalent to “Slide 2 of 4”.

Sources For Constructing Accessible Carousels

This refers to a part the place a button toggles open an inventory of hyperlinks, sometimes used for navigation menus. CSS implementations that cease at displaying the menu on :hover or :focus solely miss some necessary particulars.

Example dropdown menus from Dribbble, Google search, and GitHub
Instance dropdown menus from Dribbble, Google search, and GitHub. (Large preview)

I’ll admit, I even thought that through the use of the newer :focus-within property we might safely implement a CSS-only resolution. You’ll see that my article on CSS dropdown menus was amended to incorporate notes and sources on the required JavaScript (I stored the title in order that others in search of that resolution will hopefully full the JS implementation, too). Particularly, counting on solely CSS means violating WCAG Success Criterion 1.4.13: Content on Hover or Focus which we realized about with tooltips.

We have to add in JavaScript for some methods that ought to sound acquainted at this level:

  • Toggling aria-expanded on the menu button between true and false by listening to click on occasions
  • Closing an open menu upon use of the Esc key, and returning focus to the menu toggle button
  • Ideally, closing open menus when focus is moved outdoors of the menu
  • Non-obligatory: Implement arrow keys in addition to Residence and Finish keys for keyboard navigation between menu toggle buttons and hyperlinks inside the dropdowns

Fast tip: Guarantee the right implementation of the dropdown menu by associating the menu show to the selector of .dropdown-toggle[aria-expanded="true"] + .dropdown fairly than basing the menu show on the presence of a further JS-added class like energetic. This removes some complexity out of your JS resolution, too!

That is additionally known as a “disclosure sample” and you’ll find extra particulars within the WAI-ARIA Authoring Practices’s Example Disclosure Navigation Menu.

Further sources on creating accessible parts

Smashing Editorial
(vf, il)

Source link