We have some tabs on our page with the following structure:
<ul role="tablist">
<li>
<div role="tab" tabindex="0" aria-selected="true" aria-controls="id1">First tab content...</div>
</li>
<li>
<div role="tab">Second tab content...</div>
</li>
</ul>
This gives rise to two violations when running Axe accessibility tests, namely:
<li> elements must be contained in a <ul> or <ol>, and Certain ARIA roles must contain particular children (also the converse that certain children need particular parent roles).
I understand that the first violation is due to the tablist role meaning that the <ul> is no longer seen as a <ul>. I don't understand the second violation as the spec does not enforce that elements with role="tab" are immediate children of tablist.
One possible fix, which prevents these violations, would be to move role="tab" up to the <li> elements. The problem then though is a different violation: Nested interactive controls are not announced by screen readers, due to the contained <div> being focusable presumably. Changing this to the outer <li> would require a whole load of js and css changes, so is not a simple fix.
To what extent does this really need fixing and what is the best approach?
The answer with your current setup is to remove the semantic meaning from the
<li>.role="none presentation"removes all semantic meaning from the<li>(so think of it like a<div>now).It would be preferable to move everything "up one level" to keep the DOM tidy though, so a better solution might be:
This would work as you have now changed the semantic meaning of both the
<ul>and the<li>, although it might need a minor adjustment to CSS.Also Axe can sometimes complain with the first example, even though it is valid, the second example will have no complaints from automated checkers.
Alternatively you may want to look at using
<button>elements to create tabs, see the tabs example on W3 on how to structure that.It has the advantage that the
clickhandler can be used for both mouse and keyboard interactions because you are using a<button>.