Skip to main content

Testing

Help us to improve the ONS Design System. Take part in a short study

Tabs

Tabs let the user navigate between sections of content, showing one section at a time.

<section role="region" class="tabs">
  <h2 class="tabs__title">Tabs</h2>
  <ul class="tabs__list">
    <li id="tabId1Item" class="tab__list-item"><a href="#tabId1" class="tab" data-ga="click" data-ga-category="tabs" data-ga-action="Show: UKIS" data-ga-label="Show: UKIS">UKIS</a></li>
    <li id="tabId2Item" class="tab__list-item"><a href="#tabId2" class="tab" data-ga="click" data-ga-category="tabs" data-ga-action="Show: Vacancy survey" data-ga-label="Show: Vacancy survey">Vacancy survey</a></li>
    <li id="tabId3Item" class="tab__list-item"><a href="#tabId3" class="tab" data-ga="click" data-ga-category="tabs" data-ga-action="Show: QBS" data-ga-label="Show: QBS">QBS</a></li>
  </ul>
  <section id="tabId1" class="tabs__panel">
    <h3>Aim of this survey</h3>
    <p class='u-fs-r'>The aim of the UK Innovation Survey (UKIS) is to collect data from businesses about various aspects of their innovation related activities. Using this data we can measure the level, types and trends in innovation.</p>
    <h3>How we’ll use this data</h3>
    <p class='u-fs-r'>The UKIS data is a major source of evidence to inform government policy. It is used to promote innovation activities among businesses to boost economic growth. It is an important contribution to the European-wide Community Innovation Survey (CIS). The CIS is used for international benchmarking and comparison purposes.</p>
    <p><a href='https://www.ons.gov.uk/surveys/informationforbusinesses/businesssurveys/ukinnovationsurvey'>More information on the UKIS survey</a> can be found on the ONS website.
  </section>
  <section id="tabId2" class="tabs__panel">
    <h3>Purpose</h3>
    <p class='u-fs-r'>The Vacancy Survey is a regular survey of businesses, which provides an accurate and comprehensive measure of the total number of vacancies across the economy and fills a gap in the information available regarding the demand for labour. Before the Vacancy Survey was introduced, the only information available nationally about vacancies was from records of vacancies notified to Job Centres by employers. This provided only a partial picture, possibly less than half of all vacancies, because employers are under no obligation to notify vacancies to Job Centres. This business based survey has a more complete coverage and is included in the monthly ONS Labour Market Statistical Bulletin.</p>
    <p><a href='https://www.ons.gov.uk/surveys/informationforbusinesses/businesssurveys/vacancysurvey'>More information on the Vacancy Survey</a> can be found on the ONS website.
  </section>
  <section id="tabId3" class="tabs__panel">
    <h3>Aim of this survey</h3>
    <p class='u-fs-r'>The Quarterly Business Survey (QBS) collects quarterly information on employment of businesses in Great Britain. Your response contributes to Labour Market Statistics.</p>
    <h4>What you need to know</h4>
    <p class='u-fs-r'>To complete the survey, you will need the following information to answer the survey questions:</p>
    <ul>
      <li>number of full-time/part-time male employees</li>
      <li>number of full-time/part-time female employees</li>
      <li>total number of employees for the business</li>
    </ul>
    <p><a href='https://www.ons.gov.uk/surveys/informationforbusinesses/businesssurveys/vacancysurvey'>More information on the Vacancy Survey</a> can be found on the ONS website.
  </section>
</section>
Nunjucks macro options
Name Type Required Description
title string true The descriptive title for a tab set.
tabs Array<tab> true An array of tabs to render.

Tab

Name Type Required Description
title string true The title for the tab
content string true The content for the tab
{% from "components/tabs/_macro.njk" import onsTabs %}
{{
    onsTabs({
        "title": "Tabs",
        "tabs": [
            {
                "title": "UKIS",
                "content": "<h3>Aim of this survey</h3>
        <p class='u-fs-r'>The aim of the UK Innovation Survey (UKIS) is to collect data from businesses about various aspects of their innovation related activities. Using this data we can measure the level, types and trends in innovation.</p>
        <h3>How we’ll use this data</h3>
        <p class='u-fs-r'>The UKIS data is a major source of evidence to inform government policy. It is used to promote innovation activities among businesses to boost economic growth. It is an important contribution to the European-wide Community Innovation Survey (CIS). The CIS is used for international benchmarking and comparison purposes.</p>
        <p><a href='https://www.ons.gov.uk/surveys/informationforbusinesses/businesssurveys/ukinnovationsurvey'>More information on the UKIS survey</a> can be found on the ONS website."
            },
            {
                "title": "Vacancy survey",
                "content": "<h3>Purpose</h3>
        <p class='u-fs-r'>The Vacancy Survey is a regular survey of businesses, which provides an accurate and comprehensive measure of the total number of vacancies across the economy and fills a gap in the information available regarding the demand for labour. Before the Vacancy Survey was introduced, the only information available nationally about vacancies was from records of vacancies notified to Job Centres by employers. This provided only a partial picture, possibly less than half of all vacancies, because employers are under no obligation to notify vacancies to Job Centres. This business based survey has a more complete coverage and is included in the monthly ONS Labour Market Statistical Bulletin.</p>
        <p><a href='https://www.ons.gov.uk/surveys/informationforbusinesses/businesssurveys/vacancysurvey'>More information on the Vacancy Survey</a> can be found on the ONS website."
            },
            {
                "title": "QBS",
                "content": "<h3>Aim of this survey</h3>
        <p class='u-fs-r'>The Quarterly Business Survey (QBS) collects quarterly information on employment of businesses in Great Britain. Your response contributes to Labour Market Statistics.</p>
        <h4>What you need to know</h4>
        <p class='u-fs-r'>To complete the survey, you will need the following information to answer the survey questions:</p>
        <ul>
        <li>number of full-time/part-time male employees</li>
        <li>number of full-time/part-time female employees</li>
        <li>total number of employees for the business</li>
        </ul>
        <p><a href='https://www.ons.gov.uk/surveys/informationforbusinesses/businesssurveys/vacancysurvey'>More information on the Vacancy Survey</a> can be found on the ONS website."
            }
        ]
    })
}}

{% macro onsTabs(params) %}
    <section role="region" class="tabs">
        <h2 class="tabs__title">{{params.title}}</h2>
        <ul class="tabs__list">
            {% for tab in params.tabs %}
                <li id="tabId{{loop.index}}Item" class="tab__list-item"><a href="#tabId{{loop.index}}" class="tab" data-ga="click" data-ga-category="tabs" data-ga-action="Show: {{tab.title}}" data-ga-label="Show: {{tab.title}}">{{tab.title}}</a></li>
            {% endfor %}
        </ul>
        {% for tab in params.tabs %}
            <section id="tabId{{loop.index}}" class="tabs__panel">
                {{tab.content | safe}}
            </section>
        {% endfor %}
    </section>
{% endmacro %}

.tabs {
  margin-bottom: 1rem;
}
// Tabs - list
.tabs__list {
  border-bottom: 0;
  margin: 0 0 1rem;
  overflow: visible;
  padding: 0;
  &--row {
    border-bottom: 1px solid $color-borders;
    margin: 0;
  }
}
.tab__list-item {
  box-sizing: border-box;
  display: list-item;
  list-style: none;
  margin: 0 0 0.3rem;
  &--row {
    display: inline;
  }
}
.tab--row {
  background: $color-button-secondary;
  border: 1px solid $color-button-secondary;
  border-radius: 3px 3px 0 0;
  color: $color-text;
  display: inline-block;
  height: 2.5rem;
  margin: 0 0.1rem 0 0;
  overflow: visible;
  padding: 0.5rem 1rem;
  position: relative;
  text-decoration: underline;
  &:hover {
    background-color: darken($color-button-secondary, 5%);
    border-color: darken($color-button-secondary, 5%);
    color: $color-text;
    text-decoration: underline solid $color-text 2px;
  }
  &[aria-selected='true'] {
    background-color: $color-white;
    border-color: $color-borders;
    // Tab when selected
    border-radius: 3px 3px 0 0;
    font-weight: 700;
    text-decoration: none;
    &::after {
      background: $color-white;
      bottom: -2px;
      box-shadow: none;
      content: '';
      height: 0.09rem;
      left: 0;
      // hides the lower border of the active tab.
      position: absolute;
      right: 0;
      z-index: 3;
    }
  }
  &:focus {
    background-color: $color-focus;
    color: $color-text-link-focus;
    outline: none;
  }
}
// Tabs - Panels
.tabs__panel {
  margin-bottom: 1rem;
  padding-top: 1rem;
  position: relative;
  z-index: 10;
  &--hidden {
    display: none;
  }
  &:focus {
    outline: 4px solid $color-focus;
  }
}

When to use this component

Use tabs to collate facets of content within a closely related context. The provide a way for users to quickly scan across the subject areas of content.

When not to use this component

Tabs hide content from users and are not easily noticed and understood by everyone. So they should not be used if the content inside each tab is critial to the user being able to progress with their task.

Test your content considering the following alternatives before using tabs:

  • simpify the content to reduce down the amount
  • seperate the content with headings
  • use page contents links to enable users to quickly to navigate to sections of content
  • divide the content across multiple pages

Alternative components

Accordions, tabs and collapsible all hide sections of content which a user can show or hide.

Tabs may work better for users who need to switch quickly between sections because the position of their headings remains static, whereas the accordion headings move down the page as you open others.

Consider the number of sections you need to display. Tabs display horizontally so cannot display as many sections as accordions which display vertically. Also consider the use of your service on mobile devices. Tabs only display on viewports > 640px before their content is expanded and stacked vertically under headings.

Use the collapsible component if there is only one section of content that needs to be condensed.

How to use this component

Tabs work well when the primary content block is displayed in the first tab. Subsequent tabs can show further detail and/or deeper levels that relate to the primary content (but this isn’t a strict rule).

The typical tab display will show on viewports that are equal to or greater than 640px.

For viewports >= 640px the following aria attributes will be attached to the DOM via javascript on load:

Element ARIA attribute Description
ul.tab__list role="tablist" Convey to screenreaders that the element serves as a container for a set of tabs.
li.tab__list-item role="presentation" Convey to screenreaders that this element does not have any functional, interactive, or structural relevance implied.
a.tab role="tab" Indicates the element serves as a tab control.
aria-controls="NAME" Refers to the tab panel element associated with the tab.
aria-selected="true/false" Indicates if the current tab is selected.
.tab__panel aria-labelledby="TITLE_ID" Indicates the associated tab with the current content.
role="tabpanel" Indicates that the element is a content panel with associated tab.

For viewports < 640px tabs are displayed as a list and function as a table of contents (TOC). All panels are displayed in the order they are structured. Clicking a TOC link will jump the user to the associated panel. Tabs use JavaScript so when it’s turned off, the content is also displayed in this way. A visually hidden <h2> should be added above the tabs list to provide further information and context to screenreader users. This will also be displayed for non javascript users.

Use clear and short labels

Tabs hide content and are displayed horizontally, so it’s important to use tab labels which are clear and succinct. To avoid the tab layout breaking, consider to using single words or reducing the number of tabs.

Help improve this component

Let us know how we could improve this component or share your user research findings.

Discuss the ‘Tabs’ component on GitHub