Search code examples
cssfirefoxaccessibilitytabbing

How to prevent a box with overflow-y: scroll from stealing the focus on tab on Firefox?


Consider a page with the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css">
            .steal-focus { overflow-y: scroll }
        </style>
    </head>
    <body>
        <form action="/">
            <input type="text" value="First">
            <div class="steal-focus">Content</div>
            <input type="text" value="Second">
        </form>
    </body>
</html>
  1. Load this page on Firefox.
  2. Hit tab a first time: the focus goes to the first text field.
  3. Hit tab a second time: the focus goes to the <div> instead of the second text field, because of the overflow-y: scroll.

This behavior is unique to Firefox: this doesn't happen on IE, Safari, or Chrome. How can I get around this behavior, which sounds like a Firefox bug to me? I'd like the tab to skip over the <div> even if it has an overflow-y: scroll.


Solution

  • Use the tabIndex attribute to control the order of items that Tab jumps through. Like this:

    <body>
        <form action="/">
            <input type="text" value="First" tabIndex="1">
            <div class="steal-focus">Content</div>
            <input type="text" value="Second" tabIndex="2">
        </form>
    </body>