Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aria-expanded html attribute

I am currently making a navbar and came accross the aria-expanded html attribute. I know of course that the element is used to let an element either expand or collapse. I could find very little information with regard to this and the W3C docs aren't that clear IMO.

Question

How does this attribute work exactly?

like image 867
Willem van der Veen Avatar asked Feb 03 '18 17:02

Willem van der Veen


People also ask

What is aria-expanded in CSS?

aria-expanded – indicates that the button controls another component in the interface, and relays that component's current state. aria-pressed – indicates that the button behaves similarly to a checkbox, in that it has its state toggles between being pressed or unpressed.

How do I expand my aria value?

Access the attributes array of the element and find the position of the wanted attribute - in this case that will be 4, because aria-expanded is the 5th attribute of the tag. From there you just get the value, and that should give you "false" (in this case).

Is aria-expanded necessary?

For example, if a navigation is made of nested lists of links, and clicking a link would open/close a sub list using JavaScript, aria-expanded should be used. However, if clicking a link would open/close a sub list via a page refresh, aria-expanded is not needed.

How does aria-expanded work?

The aria-expanded attribute provides information about whether an element is in an expanded or collapsed state. For example, if you have a collapsible element which contains a text, then a screen reader would know that the text is not currently displayed on the screen when the aria-expanded attribute is set to true.


2 Answers

The aria-expanded attribute is simply a flag for the user agent. It

Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed.

...where that indication is for the element's contents, or if aria-controls is also specified, for the target element.

You set its value to indicate what you've done to the page (e.g., you've expanded or contracted a section). It doesn't have any particular behavior associated with it, it's for letting the user agent know what's going on so it can interpret that for its audience (who may be vision-impaired and need some other indication that a section is/isn't expanded).

like image 122
T.J. Crowder Avatar answered Sep 28 '22 03:09

T.J. Crowder


ARIA attributes are related to accessibility. In this particular case, it's to let users with assistive technology (e.g. screen readers) know whether an element is expanded or not. It does not have any native effect on the styling of the element, but you can target that with CSS attribute selectors: [aria-expanded="true"]. You can toggle the value with JavaScript as needed.

like image 40
chriskirknielsen Avatar answered Sep 28 '22 03:09

chriskirknielsen