Skip to content

Tabs that turn into an accordion in HTML and CSS

Accessible tabs on large screens that turn into an accordion on phones, with the right ARIA roles in each mode.

Here is an excerpt of this component's code in HTML and CSS. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in HTML and CSS

index.html
<!doctype html>
<html lang="fr">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Onglets qui deviennent un accordéon</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <main class="demo">
      <section class="oaa" data-oaa aria-labelledby="oaa-titre">
        <h2 class="oaa-title" id="oaa-titre">Nos cours de cuisine</h2>
script.js
document.querySelectorAll("[data-oaa]").forEach((root) => {
  const on = (el, type, fn) => el.addEventListener(type, fn);
  const mq = window.matchMedia("(min-width: 40rem)");
  const tabs = [...root.querySelectorAll("[data-oaa-tab]")];
  const accs = [...root.querySelectorAll("[data-oaa-acc]")];
  const panels = [...root.querySelectorAll("[data-oaa-panel]")];
  let selected = 0;
  const open = new Set([0]);
  root.querySelector("[data-oaa-tablist]").hidden = false;
  root.setAttribute("data-oaa-ready", "");

  const render = () => {
style.css
:root {
  --oaa-card: #ffffff;
  --oaa-text: #2b1a12;
  --oaa-muted: #5f4b40;
  --oaa-line: #e8dbd2;
  --oaa-soft: #fdf4ee;
  --oaa-accent: #9a3412;
  --oaa-accent-text: #ffffff;
  --demo-bg: #f3ece6;
  --demo-text: #2b1a12;
}

Free component: copy it freely, including for commercial projects.

Tabs components in HTML and CSS
  • Indicative prices sorted by trade in tabs, with the unit of each service, a VAT-inclusive / excl. switch and a free-quote notice.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Understated tabs with an underlined active tab, to organize the information on a product page.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Two tabs to show different opening hours by season, in a compact card.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A dark code block with one tab per package manager, for technical documentation.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component