--- url: "https://web.dev/learn/html/focus" titulek: "Focus | web.dev" h1: "Focus Stay organized with collections Save and categorize content based on your preferences." popis: "How to manage focus order in your HTML documents." jazyk: "en" kanonicka: "https://web.dev/learn/html/focus" drobecky: ["web.dev", "Resources", "HTML"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:47:19" znaku: 13909 slov: 1853 kodovani: "iso-8859-2" sha256: "28fc487553551375aafa3877146f77b0b8637978c9d3fda75e49e027c5e6d427" --- Focus Stay organized with collections Save and categorize content based on your preferences. Interactive elements, including [form controls](/learn/html/forms), [links](/learn/html/links), and buttons, are by default focusable and tabbable. Tabbable elements are part of the document's sequential focus navigation order. Other elements are inert, meaning they are not interactive. With HTML attributes, it's possible to make interactive elements inert and to make inert elements interactive. By default, the navigation focus order is the same as the visual order, which is the source code order. There are HTML attributes that can alter this order and CSS properties that can alter the visual order of content. Changing the tabbing order with HTML or visual rendering order with CSS can harm user experience. Don't alter the perceived and actual tabbing order with CSS and HTML. As the following two examples demonstrate, tab orders that differ from the visually expected order are confusing to users and bad for user experience. In the following example, the value of the [`tabindex`](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/tabindex) attribute has made the tab order chaotic: In this example, CSS has created a divergence between the tabbing order and the visual order of the content: The [`flex-flow: row-reverse;`](https://developer.mozilla.org/docs/Web/CSS/flex-flow) declaration has reversed the visual order. In addition, the CSS [order](https://developer.mozilla.org/docs/Web/CSS/order) property was applied to the sixth word, "This", which visually moved that one word. The tabbing sequence is the order of the code, which no longer matches the visual order. This creates a disconnect for keyboard users. Some CSS features, including [flexbox](https://developer.mozilla.org/docs/Learn/CSS/CSS_layout/Flexbox), [grid](https://developer.mozilla.org/docs/Web/CSS/CSS_Grid_Layout), [positioning](https://developer.mozilla.org/docs/Learn/CSS/CSS_layout/Positioning), [transforms](https://developer.mozilla.org/docs/Web/CSS/translate), and [multi-column](https://developer.mozilla.org/docs/Learn/CSS/CSS_layout/Multiple-column_Layout), can alter the visual order of content. Test your content by tabbing through it with a keyboard: with shift + tab to move backwards through the content. Use CSS to indicate which element has focus. Don't reorder focusable elements with CSS. It's important you maintain a logical tabbing order on all viewport sizes. Make inert elements interactive The `contenteditable` and `tabindex` attributes, being global attributes, can be added to any element, making them focusable in the process. Focusable elements can also be focused with a mouse or pointer, by having the [`autofocus`](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/autofocus) attribute set, or by script, such as with [`element.focus()`](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus). The `tabindex` attribute The global [`tabindex`](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/tabindex) attribute, introduced in [attributes](/learn/html/attributes#tabindex), enables elements that otherwise wouldn't be able to receive focus to get focus, usually with Tab, hence the name. The `tabindex` attribute takes as its value an integer. A negative value makes an element focusable but not tabbable. A `tabindex` value of `0` makes the element focusable and tabbable, adding the element on which it's applied to the sequential focus navigation order in source code order. A value of 1 or greater makes the element focusable and tabbable, but adds it to a prioritized tabbing sequence and should be avoided. On this page, the share button, ``, is a [custom element](/learn/html/template). The `tabindex="0"` adds this element into the keyboard default tabbing order: ` Share ` Additionally, the [local navigation](/learn/html/navigation#local_navigation) has a custom element with a negative `tabindex` value: `` A `tabindex` attribute with a negative value makes the element focusable, but not tabbable. The element can receive focus, such as using [`HTMLElement.focus()`](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus), but it's not part of the sequential focus navigation order. The convention for non-tabbable, focusable elements is to use `tabindex="-1"`. If you add `tabindex="-1"` to an interactive element, it will no longer be tabbable. The [`element.focus()`](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus) method can be used to set focus to focusable elements. Browsers scroll focused elements into view. For this reason, avoid the use of `element.focus({preventScroll:true})`, as focusing on a non-visible element is a bad user experience. If you want to query the document to find out which element has focus, use the read-only [`Document.activeElement`](https://developer.mozilla.org/docs/Web/API/Document/activeElement) property. Elements with a `tabindex` of `1` or greater are included in a separate tab sequence. As in the following Codepen, tabbing begins in a separate sequence, in order of lowest value to highest value, before going through those in the regular sequence (no `tabindex` set, or `tabindex="0"`) in source order: `tabindex` with a positive value puts the element into a prioritized focus sequence, which can lead to focus order chaos. Avoid [modifying the DOM order with `tabindex`](/articles/using-tabindex). Altered tabbing order can create bad user experiences and are difficult to maintain. The `contenteditable` attribute The [`contenteditable`](/learn/html/attributes#contenteditable) attribute was discussed earlier. Setting `contenteditable="true"` on any element makes it editable, focusable, and part of the tab order. The focus behavior is similar to setting `tabindex="0"`, but not the same. Nested `contenteditable` elements are focusable but not tabbable. To make a nested `contenteditable` element tabbable, add `tabindex="0"`, which adds it to the sequential focus navigation order. Give `autofocus` to interactive elements While the boolean [`autofocus`](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/autofocus) is a global attribute that can be set on any element, it doesn't make an inert element interactive. When the page loads, the first focusable element with the `autofocus` attributes receives focus, as long as that element is displayed and not nested in a [``](/learn/html/dialog). Automatically setting focus on content may be confusing to users. When you set `autofocus` on a form control, that form control scrolls into view on page load. Your users, including screen reader users and users with small viewports, may miss the form's instructions or even the form control's label. Further, the `autofocus` attribute doesn't alter the document's sequential focus navigation order. The elements in the sequence that come prior to auto-focused element are skipped. For these reasons, you should avoid using the `autofocus` attribute. The exception to the "don't use `autofocus`" recommendation is including the `autofocus` attribute within `` elements. When a dialog is opened, the browser automatically focuses on the first focusable interactive element within the ``, which means it's unnecessary to add `autofocus` to an element. If you want to be sure a specific interactive element within the dialog receives focus when the dialog opens, add the `autofocus` attribute to that element. `
` The `autofocus` attribute set on the close `