EthicsComputerCourses
Join training +91 90532 10052

HTML Component Design Patterns

HTML · Lesson 184 of 234 Advanced 10 min read Updated

By the end of this lesson you will be able to build twelve common interface patterns with semantic HTML, say which accessibility details each one needs, and, just as important, recognise when a pattern is the wrong choice. Two runnable galleries show every pattern working.

What you should know first: Reusable HTML Components (portable inputs and generated ids) and Component-Based HTML Development. Several patterns have a deep-dive lesson of their own, linked below. This lesson is the catalogue: one page you can return to when a design lands on your desk.

What a design pattern is

A design pattern is a proven answer to a problem that keeps coming back. "Show one of several panels in the same space" is a problem; tabs are a pattern that solves it. A component is one team's implementation of a pattern.

Think of a house plan in a builder's catalogue in Pune. The 2BHK plan is tested: doors open the right way and the kitchen is near the dining area. You still choose your own tiles. Patterns work the same way: the structure and accessibility are fixed, the styling is yours.

Every pattern below answers four questions: what job it does, its markup skeleton, its accessibility needs, and when not to use it. The last one prevents the most bugs. Most broken interfaces use a correct pattern in the wrong place, such as tabs that hide content people need to compare.

Content patterns

PatternSkeletonAccessibilityDo not use when
Card<article> with <h3><a href> title, text, optional imageLink only the title; heading level fits the page; decorative image gets alt=""The item stands alone (use normal page content) or users compare values (use a table)
Media object<div class="media"> with <img> beside a body <div> holding a heading and textImage alt="" if the text already names it; keep text readable when it wraps under the imageThe image is the content (use <figure> with <figcaption>)
Empty stateA heading or paragraph that says why it is empty, plus the next action as a link or buttonIf it appears after a search or filter, announce it with a status messageSomething failed (show an error) or data is still loading (use a skeleton)
Skeleton loaderGrey shapes inside a container with aria-busy="true"Hide shapes with aria-hidden="true"; announce "Loading" in a status region; set aria-busy="false" when doneThe load is nearly instant, or you cannot predict the layout

Two of these need more explanation.

The card link. It is tempting to wrap the whole card in <a>. Screen readers then read the entire card as one long link name, and any button inside becomes invalid, because one interactive element cannot contain another. Link the title and, if the whole card must be clickable, stretch the title link's clickable area with CSS.

The skeleton loader. aria-busy="true" tells assistive technology that a region is being updated and it may wait before reading it. Support varies between screen readers, so pair it with a short text message in a role="status" region. Also stop any shimmer animation for users who set prefers-reduced-motion.

PatternSkeletonAccessibilityDo not use when
Breadcrumb<nav aria-label="Breadcrumb"> with an <ol> of linksLast item has aria-current="page"; separators come from CSS, not textThe site is one level deep, or as a replacement for the main menu
Pagination<nav aria-label="Pagination"> with a <ul> of real links (?page=2)Current page has aria-current="page"; write "Previous" and "Next" in wordsThe list fits on one page, or the user is completing steps in order (use a step indicator)
Form field group<label>, <input>, hint, error messageConnect hint and error with aria-describedby; set aria-invalid="true" only when there is an errorRadio buttons or checkboxes (group them with <fieldset> and <legend>)

Breadcrumbs and pagination for search engines are covered in Crawlable Links, Breadcrumbs & Pagination. The field group is the most reused pattern on any site, so here is its skeleton in full:

HTML
<div class="field">
  <label for="pin">PIN code</label>
  <p class="field__hint" id="pin-hint">6 digits, for example 124001 for Rohtak</p>
  <input id="pin" name="pin" inputmode="numeric" autocomplete="postal-code"
         aria-describedby="pin-hint pin-error" aria-invalid="true">
  <p class="field__error" id="pin-error">Enter a 6-digit PIN code</p>
</div>

The hint sits above the input, so screen magnifier users see it before they type. aria-describedby takes several ids, read in order. The error message says what to do, not just "Invalid". Built-in validation messages are covered in Native HTML Form Validation.

This page puts seven patterns side by side. Press "Load results" to watch the skeleton loader and the empty state do their work.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en-IN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pattern gallery 1</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 16px; max-inline-size: 46rem; }
    section { border-block-end: 1px solid #ddd; padding-block: 8px 16px; }
    .card { border: 1px solid #ccc; border-radius: 8px; padding: 12px; max-inline-size: 18rem; }
    .card h3 { margin: 0 0 4px; font-size: 1rem; }
    .media { display: flex; gap: 12px; align-items: start; flex-wrap: wrap; }
    .media h3 { margin: 0; font-size: 1rem; }
    .crumbs ol, .pages ul { list-style: none; display: flex; flex-wrap: wrap; gap: 6px; padding: 0; margin: 0; }
    .crumbs li + li::before { content: "/"; margin-inline-end: 6px; color: #888; }
    .pages a { display: inline-block; padding: 4px 10px; border: 1px solid #1565c0; border-radius: 4px; }
    .pages [aria-current] { background: #1565c0; color: white; }
    .field__hint { margin: 2px 0; color: #555; font-size: 0.9rem; }
    .field__error { margin: 2px 0; color: #b71c1c; font-weight: 600; }
    [aria-invalid="true"] { border: 2px solid #b71c1c; }
    .skeleton { block-size: 1rem; background: #e0e0e0; border-radius: 4px; margin-block: 8px; animation: pulse 1.2s infinite alternate; }
    @keyframes pulse { to { opacity: 0.4; } }
    @media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } }
  </style>
</head>
<body>
  <h1>Pattern gallery 1</h1>

  <section aria-labelledby="g-card">
    <h2 id="g-card">Card</h2>
    <article class="card">
      <h3><a href="#jee">JEE Main Foundation</a></h3>
      <p>Class 11, Rohtak. ₹48,000 per year.</p>
    </article>
  </section>

  <section aria-labelledby="g-media">
    <h2 id="g-media">Media object</h2>
    <div class="media">
      <img src="https://course.ethicscomputer.in/assets/samples/avatar.svg" alt="" width="64" height="64">
      <div>
        <h3>Farhan Qureshi</h3>
        <p>Physics faculty, 12 years of teaching in Jaipur.</p>
      </div>
    </div>
  </section>

  <section aria-labelledby="g-nav">
    <h2 id="g-nav">Breadcrumb and pagination</h2>
    <nav class="crumbs" aria-label="Breadcrumb">
      <ol>
        <li><a href="#home">Home</a></li>
        <li><a href="#courses">Courses</a></li>
        <li><a href="#jee" aria-current="page">JEE Main Foundation</a></li>
      </ol>
    </nav>
    <nav class="pages" aria-label="Pagination">
      <ul>
        <li><a href="?page=1">Previous</a></li>
        <li><a href="?page=1">1</a></li>
        <li><a href="?page=2" aria-current="page">2</a></li>
        <li><a href="?page=3">3</a></li>
        <li><a href="?page=3">Next</a></li>
      </ul>
    </nav>
  </section>

  <section aria-labelledby="g-field">
    <h2 id="g-field">Form field group</h2>
    <div class="field">
      <label for="pin">PIN code</label>
      <p class="field__hint" id="pin-hint">6 digits, for example 124001 for Rohtak</p>
      <input id="pin" name="pin" inputmode="numeric" autocomplete="postal-code" aria-describedby="pin-hint">
      <p class="field__error" id="pin-error" hidden>Enter a 6-digit PIN code</p>
    </div>
  </section>

  <section aria-labelledby="g-load">
    <h2 id="g-load">Skeleton loader and empty state</h2>
    <button type="button" id="load">Load results</button>
    <div id="results" aria-busy="false"></div>
    <p role="status" id="load-status"></p>
  </section>

  <script>
    // Form field: show the error only after the user leaves the field
    const pin = document.querySelector("#pin");
    const error = document.querySelector("#pin-error");
    pin.addEventListener("blur", () => {
      const bad = pin.value !== "" && !/^\d{6}$/.test(pin.value);
      error.hidden = !bad;
      pin.setAttribute("aria-invalid", String(bad));
      pin.setAttribute("aria-describedby", bad ? "pin-hint pin-error" : "pin-hint");
    });

    // Skeleton loader, then an empty state
    const results = document.querySelector("#results");
    const status = document.querySelector("#load-status");
    document.querySelector("#load").addEventListener("click", () => {
      results.setAttribute("aria-busy", "true");
      results.innerHTML = '<div aria-hidden="true"><div class="skeleton"></div><div class="skeleton"></div><div class="skeleton"></div></div>';
      status.textContent = "Loading batches";
      setTimeout(() => {
        results.innerHTML = '<h3>No evening batches in Kochi yet</h3><p>New batches open in June. <a href="#notify">Get a WhatsApp alert</a> or <a href="#all">see all centres</a>.</p>';
        results.setAttribute("aria-busy", "false");
        status.textContent = "Loaded: no batches found";
      }, 1500);
    });
  </script>
</body>
</html>

Type 12400 in the PIN field and press Tab: the red error appears and is added to aria-describedby. Press "Load results": three grey bars pulse for a moment, then the empty state explains why there are no results and offers two next steps.

Interactive patterns

PatternSkeletonAccessibilityDo not use when
Disclosure<details><summary>, or a <button aria-expanded aria-controls> plus a hidden panel<details> needs nothing extra; a button must update aria-expandedThe hidden content is essential, such as a price or eligibility rule
AccordionSeveral <details name="..."> with one shared nameNative keyboard support; each summary should read well on its ownUsers need two answers open to compare, or the content is short enough to show
Tabsrole="tablist" with role="tab" buttons, each controlling a role="tabpanel"aria-selected, one tab in the Tab order, arrow keys move between tabsContent is long or sequential, or the "tabs" are really site navigation (use links)
Modal<dialog> opened with showModal()Name it with aria-labelledby; autofocus on the safest button; focus returns on closeThe message is not urgent, the form is long (use a page), or on page load
ToastAn empty role="status" region already in the page; script changes its textNo focus change; role="alert" only for urgent errors; keep it visible long enoughThe message needs an action, or it is the only place the information appears

Tabs are the pattern people most often get wrong, so here is the skeleton. Only the selected tab has tabindex="0"; the others have -1, so one press of Tab enters the tab list and the next press goes into the panel. This is called a roving tabindex. Full keyboard details are in Tabs & Menus.

HTML
<div role="tablist" aria-label="Course details">
  <button type="button" role="tab" id="tab-fees" aria-selected="true" aria-controls="panel-fees">Fees</button>
  <button type="button" role="tab" id="tab-timing" aria-selected="false" aria-controls="panel-timing" tabindex="-1">Timings</button>
</div>
<div role="tabpanel" id="panel-fees" aria-labelledby="tab-fees" tabindex="0">...</div>
<div role="tabpanel" id="panel-timing" aria-labelledby="tab-timing" tabindex="0" hidden>...</div>

Modals and toasts have deep-dive lessons: Dialog Element and Accessible Modal & Dialog Systems, which also explains why a toast must never be a dialog.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en-IN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pattern gallery 2</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 16px; max-inline-size: 46rem; }
    section { border-block-end: 1px solid #ddd; padding-block: 8px 16px; }
    [role="tablist"] { display: flex; gap: 4px; border-block-end: 2px solid #1565c0; }
    [role="tab"] { font: inherit; padding: 6px 14px; border: 1px solid #1565c0; border-block-end: none; background: white; }
    [role="tab"][aria-selected="true"] { background: #1565c0; color: white; }
    [role="tabpanel"] { padding: 8px; }
    dialog { border: none; border-radius: 10px; padding: 20px; }
    dialog::backdrop { background: rgb(0 0 0 / 0.5); }
    .toast { position: fixed; inset-block-end: 16px; inset-inline-end: 16px; margin: 0; }
    .toast:not(:empty) { background: #263238; color: white; padding: 10px 16px; border-radius: 6px; }
  </style>
</head>
<body>
  <h1>Pattern gallery 2</h1>

  <section aria-labelledby="g-disclosure">
    <h2 id="g-disclosure">Disclosure</h2>
    <details><summary>Documents to bring</summary><p>Class 10 marksheet, Aadhaar copy, two photos.</p></details>
  </section>

  <section aria-labelledby="g-accordion">
    <h2 id="g-accordion">Accordion</h2>
    <details name="faq" open><summary>When do batches start?</summary><p>1 April and 1 July.</p></details>
    <details name="faq"><summary>Is there a demo class?</summary><p>Yes, every Sunday at 10 am.</p></details>
  </section>

  <section aria-labelledby="g-tabs">
    <h2 id="g-tabs">Tabs</h2>
    <div role="tablist" aria-label="Course details">
      <button type="button" role="tab" id="tab-fees" aria-selected="true" aria-controls="panel-fees">Fees</button>
      <button type="button" role="tab" id="tab-timing" aria-selected="false" aria-controls="panel-timing" tabindex="-1">Timings</button>
      <button type="button" role="tab" id="tab-faculty" aria-selected="false" aria-controls="panel-faculty" tabindex="-1">Faculty</button>
    </div>
    <div role="tabpanel" id="panel-fees" aria-labelledby="tab-fees" tabindex="0">₹48,000 per year, in three instalments.</div>
    <div role="tabpanel" id="panel-timing" aria-labelledby="tab-timing" tabindex="0" hidden>Monday to Friday, 4 pm to 7 pm.</div>
    <div role="tabpanel" id="panel-faculty" aria-labelledby="tab-faculty" tabindex="0" hidden>Meera Iyer (Maths), Farhan Qureshi (Physics).</div>
  </section>

  <section aria-labelledby="g-modal">
    <h2 id="g-modal">Modal and toast</h2>
    <button type="button" id="open">Cancel my seat</button>
    <dialog id="confirm" aria-labelledby="confirm-title">
      <h3 id="confirm-title">Cancel your seat?</h3>
      <p>Someone on the waiting list will get it.</p>
      <form method="dialog">
        <button value="keep" autofocus>Keep my seat</button>
        <button value="cancel">Cancel seat</button>
      </form>
    </dialog>
  </section>

  <p class="toast" role="status" id="toast"></p>

  <script>
    // Tabs: click or arrow keys select a tab (roving tabindex)
    const tabs = [...document.querySelectorAll('[role="tab"]')];
    function select(tab) {
      for (const t of tabs) {
        const on = t === tab;
        t.setAttribute("aria-selected", String(on));
        t.tabIndex = on ? 0 : -1;
        document.getElementById(t.getAttribute("aria-controls")).hidden = !on;
      }
      tab.focus();
    }
    for (const tab of tabs) {
      tab.addEventListener("click", () => select(tab));
      tab.addEventListener("keydown", (event) => {
        const i = tabs.indexOf(tab);
        const moves = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: tabs.length - 1 };
        if (!(event.key in moves)) return;
        event.preventDefault();
        select(tabs[(moves[event.key] + tabs.length) % tabs.length]);
      });
    }

    // Modal, then a toast that reports the result without taking focus
    const dialog = document.querySelector("#confirm");
    const toast = document.querySelector("#toast");
    let timer;
    document.querySelector("#open").addEventListener("click", () => {
      dialog.returnValue = "";
      dialog.showModal();
    });
    dialog.addEventListener("close", () => {
      toast.textContent = dialog.returnValue === "cancel" ? "Seat cancelled. A refund will reach you in 5 days." : "Your seat is safe.";
      clearTimeout(timer);
      timer = setTimeout(() => { toast.textContent = ""; }, 6000);
    });
  </script>
</body>
</html>

Click the Fees tab, then press the right arrow: focus and selection move to Timings and its panel appears. Press Tab once and focus goes into the panel, not to the next tab. Open the modal: focus lands on "Keep my seat". Choose either button and a dark toast appears in the corner while focus returns to the opener. The toast region existed, empty, from the start, which is why screen readers announce its new text.

Common mistakes

  • Wrapping a whole card in a link. Link the title; stretch its click area with CSS if needed.
  • Every tab in the Tab order. Use a roving tabindex and arrow keys.
  • Creating the toast element at the moment of the message. Live regions must exist before their text changes.
  • aria-invalid="true" before the user has typed anything. Mark errors after an attempt, not on page load.
  • Tabs for content people compare. Use a table or headings so both are visible.
  • A skeleton that never ends. On failure, replace it with an error message and set aria-busy="false".

Interview questions

1. When would you choose an accordion over tabs?

Accordions stack vertically and work well on narrow screens and for many sections with short labels. Tabs suit a few related panels of similar length that users view one at a time. If users must compare content, neither is ideal; show it together.

2. What is a roving tabindex?

In a composite widget such as tabs, only the active item has tabindex="0" and the rest have tabindex="-1". Tab moves into and out of the widget in one step, and arrow keys move between items, updating which one holds tabindex="0".

3. How do you connect a hint and an error message to an input?

Give each message an id and list them in the input's aria-describedby, separated by spaces. Set aria-invalid="true" when the value is wrong, and add the error id only while the error is shown.

4. Why must a toast's live region exist before the message?

Screen readers watch live regions for changes. An element inserted already containing its text is often not announced. Keep an empty role="status" element in the page and change its text.

5. What does aria-busy do in a skeleton loader?

It tells assistive technology that the region is being updated, so it may wait before announcing its content. Support varies, so also update a status message and set aria-busy="false" when loading finishes or fails.

Key points

  • A pattern is a proven solution; a component is one implementation of it.
  • For every pattern, know the skeleton, the accessibility needs and when not to use it.
  • Cards link their title, field groups connect hints and errors with aria-describedby.
  • Native <details>, <details name> and <dialog> cover disclosures, accordions and modals with little script.
  • Tabs need roles, aria-selected, a roving tabindex and arrow keys.
  • Toasts use an existing role="status" region and never take focus; skeletons pair aria-busy with a status message.

Exercise

Build a "My orders" page for Ananya's stationery shop in Pune using at least five patterns from this lesson: a breadcrumb, tabs for "Current" and "Past" orders, an order card per order, an empty state in the Past tab, and a toast that confirms "Reorder placed" when a Reorder button is pressed. Check that only one tab is in the Tab order and that the toast does not take focus.

Show solution
HTML
<nav aria-label="Breadcrumb"><ol>
  <li><a href="/">Home</a></li>
  <li><a href="/account/">Account</a></li>
  <li><a href="/account/orders/" aria-current="page">My orders</a></li>
</ol></nav>
<h1>My orders</h1>
<div role="tablist" aria-label="Orders">
  <button type="button" role="tab" id="t-now" aria-selected="true" aria-controls="p-now">Current</button>
  <button type="button" role="tab" id="t-past" aria-selected="false" aria-controls="p-past" tabindex="-1">Past</button>
</div>
<div role="tabpanel" id="p-now" aria-labelledby="t-now" tabindex="0">
  <article class="card">
    <h2>Order 1042: geometry box and 6 notebooks</h2>
    <p>₹640. Arriving Friday.</p>
    <button type="button" data-js="reorder">Reorder <span class="visually-hidden">order 1042</span></button>
  </article>
</div>
<div role="tabpanel" id="p-past" aria-labelledby="t-past" tabindex="0" hidden>
  <h2>No past orders yet</h2>
  <p>Delivered orders appear here. <a href="/shop/">Browse the shop</a></p>
</div>
<p role="status" class="toast" id="toast"></p>
<script>
  document.querySelector('[data-js="reorder"]').addEventListener("click", () => {
    document.querySelector("#toast").textContent = "Reorder placed";
  });
  // Reuse the select() and keydown code from gallery 2 for the tabs.
</script>

The tabs reuse the roving tabindex code from gallery 2. The toast region is in the page from the start and only its text changes, so focus stays on the Reorder button.

Quick Quiz

1. Which pattern suits "show this extra detail only if the user wants it"?

2. In a tab list of four tabs, how many tabs should have tabindex="0"?

3. When is a skeleton loader the wrong choice?

4. Why should a card not be wrapped entirely in an <a>?

Learn HTML with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call