tabindex -1 not working for child elements

Adarsh Konchady picture Adarsh Konchady · Mar 13, 2017 · Viewed 16k times · Source

I have a div tag with some content getting loaded inside it. The content inside can have buttons, anchor elements, etc. which are focusable. I do not have control over the content but I can modify the 'div' tag attributes.

My problem is the focus still goes to the content (anchor, buttons, etc.) even if I specify the tabIndex -1 to the div tag.

Is there a way to skip the entire content while tabbing ? It's certainly not working with the above code.

Answer

Nordling Art picture Nordling Art · Jun 26, 2019

Not sure why nobody has mentioned visibility: hidden yet. Setting display: none can in some cases mess up logic when dealing with dimensions of non-visual elements. visibility will persist the dimensions just like opacity: 0 would do, but also disable any tabbable children.

Example:

<div style="visibility: hidden;">
    <a href="#">I'm only tabbable if my parent is visible!</a>
</div>