Table of Contents

2.1.3. Tabs

Purpose

Tabs are dynamic modules that enable you to optimize the display of page content in a small space. They are usually shown in the form of a list of side-by-side links that display the corresponding content of the selected tab. You can only activate one tab at a time.

Basic HTML code

The HTML code includes internal links that point to the corresponding panels.

<ul>
    <li><a href="#panel-01">First tab</a></li>
    <li><a href="#panel-02">Second tab</a></li>
    <li><a href="#panel-03">Third tab</a></li>
</ul>
 
<div id="panel-01">
    Content of the first panel.
</div>
 
<div id="panel-02">
    Content of the second panel.
</div>
 
<div id="panel-03">
    Content of the third panel.
</div>

Without JavaScript, all the panels are displayed, and the tabs are used as links to directly access the content of the corresponding panel.

Behaviour with JavaScript

The scripts are then added to the basic HTML code to handle the following points:

The resulting code with JavaScript is as follows:

<ul role="tablist">
    <li role="tab" aria-selected="true" aria-controls="panel-01"><a href="#panel-01"><strong>First tab</strong></a></li>
    <li role="tab" aria-selected="false" aria-controls="panel-02"><a href="#panel-02">Second tab</a></li>
    <li role="tab" aria-selected="false" aria-controls="panel-03"><a href="#panel-03">Third tab</a></li>
</ul>
 
<div id="panel-01" role="tabpanel" tabindex="0">
    Content of the first panel.
</div>
 
<div id="panel-02" role="tabpanel" tabindex="0">
    Content of the second panel.
</div>
 
<div id="panel-03" role="tabpanel" tabindex="0">
    Content of the third panel.
</div>

Démonstration