Example of Tabs with Automatic Activation
- -About This Example
-- This example section demonstrates a tabs widget that implements the Tabs Pattern. - In this example, a tab is automatically activated and its associated panel is displayed when the tab receives focus. - It is recommended that authors consider implementing automatic activation of tabs only in circumstances where panels can be displayed instantly, i.e., all panel content is present in the DOM. - For additional guidance, see Deciding When to Make Selection Automatically Follow Focus. -
-Similar examples include:
--
-
- Example of Tabs with Manual Activation: A tabs widget where users activate a tab and display its panel by pressing Space or Enter. -
Example
-Danish Composers
@@ -96,268 +58,5 @@Accessibility Features
--
-
-
- To make it easy for screen reader users to navigate from a tab to the beginning of content in the active
tabpanel, thetabpanelelement hastabindex="0"to include the panel in the page Tab sequence. - It is recommended that alltabpanelelements in a tab set are focusable if there are any panels in the set that contain content where the first element in the panel is not focusable. -
- -
- To ensure people who rely on browser or operating system high contrast settings can both distinguish the active (selected) tab from other tabs and perceive keyboard focus:
-
-
-
- - The active tab has a 2 pixel border on its left and right sides and a 4 pixel border on top, while the names of inactive tabs have 1 pixel borders. - The active tab is also 4 pixels higher than the inactive tabs. - -
-
- The focus ring is drawn with a CSS border on a child
spanelement of the tab element. - This focus span is separated from the tab border by 2 pixels of space to ensure focus and selection are separately perceivable. - Note that when a tab element is focused, the outline of the tab element itself is set to 0 so that only one focus ring is displayed. -
- -
- Because transparent borders are visible on some systems when high contrast settings are enabled, only the focused
spanelement has a visible border. - Whenspanelements are not indicating focus, they have a 0-width border and additional padding equal in width to the border that is used to indicate focus. -
-
- - - To ensure the tab content remains visible when the screen is magnified, the width of the tabs and tab panels are defined using a percentage of the screen width. As the page is magnified the height of the tab increases and the tab content re-flows to the new dimensions of the tab. - -
Keyboard Support
-| Key | -Function | -
|---|---|
| Tab | -
-
|
-
| Right Arrow | -
-
|
-
| Left Arrow | -
-
|
-
| Home | -Moves focus to the first tab and activates it. | -
| End | -Moves focus to the last tab and activates it. | -
Role, Property, State, and Tabindex Attributes
-| Role | -Attribute | -Element | -Usage | -
|---|---|---|---|
- tablist
- |
- - |
- div
- |
- Indicates that the element serves as a container for a set of tabs. | -
| - |
- aria-labelledby="ID_REFERENCE"
- |
-
- div
- |
- Provides a label that describes the purpose of the set of tabs. | -
- tab
- |
- - |
- button
- |
-
-
|
-
| - |
- aria-selected="true"
- |
-
- button
- |
-
-
|
-
| - |
- aria-selected="false"
- |
-
- button
- |
-
-
|
-
| - |
- tabindex="-1"
- |
-
- button
- |
-
-
|
-
| - |
- aria-controls="ID_REFERENCE"
- |
-
- button
- |
- Refers to the element with role=tabpanel associated with the tab. |
-
- tabpanel
- |
- - |
- div
- |
-
-
|
-
| - |
- aria-labelledby="ID_REFERENCE"
- |
-
- div
- |
-
-
|
-
| - |
- tabindex="0"
- |
-
- div
- |
-
-
|
-
JavaScript and CSS Source Code
--
-
- CSS: tabs.css -
- JavaScript: tabs-automatic.js -
HTML Source Code
-To copy the following HTML code, please open it in CodePen.
- -
-
-
-