CHAPTER 41 · HTML

Details and summary: let readers open extra information

Build expandable hints with ordinary HTML. Understand the visible summary, the open state and a group that can show one answer at a time.

Download example files (ZIP)

Start with a familiar folded note

Imagine a practice sheet with a hint written under a folded flap. You can try the question first, then lift the flap when needed. Details creates an expandable area on a page. Summary supplies its always-visible label. The browser handles opening and closing; this basic behaviour needs no JavaScript.

This is useful for optional explanations and short answers. Keep instructions needed to complete the main task visible. A learner should not have to guess which closed flap contains an essential rule.

Read the syntax in order

Put summary first inside details, then the extra content, then close details. Write one summary for each details element. The label should explain what will open, such as How can I find a file in the same folder? Use a paragraph or list after it for the answer.

HTML fragment — part of a document
<details>
  <summary>What should I save?</summary>
  <p>Save the page with an .html extension.</p>
</details>

The opening details tag begins the whole control. Summary is the clickable label, not another ordinary paragraph. The closing summary tag ends that label. The p holds the extra explanation.

Closed by default; open when requested

Without open, the details content starts collapsed. Add the boolean open attribute to begin expanded. Open="false" still includes the attribute and therefore means open; remove it to begin closed. Readers can change the state themselves.

The summary remains available in either state. Closing does not delete the answer from the source or make it private. Someone can read it by opening the control or inspecting the source. This is a learning convenience, not a password barrier.

Use a keyboard as well as a mouse

A native summary normally receives keyboard focus. Press Tab until it is focused, then use Enter or Space to toggle it. Keep the visible focus indication so people can see where they are. Do not put a button inside summary just to recreate its existing action. Avoid links or other controls inside the label, since they make the intended action less clear.

Write a distinct label for each answer. Repeated Show more labels are hard to scan. Native controls provide useful behaviour, but you should still test the browsers and assistive tools your readers use.

A named group can open one answer at a time

Give several details elements the same name to create a group. In browsers supporting this feature, opening one closes the other open member. The elements do not have to be adjacent to belong to the same group.

In the second program, both use name=file-review and only the first begins open. Unlike a form control name, this name groups disclosures; it does not create submitted form data. Test older browsers too. If grouping is unsupported, the answers can still operate independently.

Know when to leave the answer visible

Use details when hiding the extra information helps the reader concentrate. Keep an urgent warning, the price of a purchase or required form instructions visible. Do not use it as a substitute for a proper heading structure on an entire long course.

The open content can contain suitable paragraphs and lists. An attractive animation or remembered state after a reload is not supplied automatically. Add those only when the actual product needs them and you can test the extra behaviour.

Follow the idea

Open extra information
  1. 1Read a specific summary
  2. 2Try the question first
  3. 3Open or close the details
  4. 4Keep essential instructions visible

The answer stays in the document; a closed panel is not private.

Complete program: study-details.html

Save this as study-details.html. Open it in your browser.

study-details.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Expand a Study Hint</title>
  </head>
  <body>
    <h1>Read a hint when you need it</h1>
    <main>
      <p>Try explaining a relative link before opening the hint.</p>
      <details>
        <summary>How can I find a file in the same folder?</summary>
        <p>Use its filename, such as notes.html, as the relative link address.</p>
      </details>
      <details open>
        <summary>What should I check after saving?</summary>
        <p>Refresh the browser and confirm that your latest change appears.</p>
      </details>
    </main>
  </body>
</html>
Browser previewDocument title: Expand a Study Hint

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: two question labels. The first answer begins closed; the second begins open. Click either label, or focus it with Tab and press Enter or Space. The answer changes visibility without loading another page.

Complete program: grouped-details.html

Save this as grouped-details.html. Open it in your browser.

grouped-details.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>One Question at a Time</title>
  </head>
  <body>
    <h1>Review two file questions</h1>
    <main>
      <p>These answers share a group. In supporting browsers, opening one closes the other.</p>
      <details name="file-review" open>
        <summary>Why did my change not appear?</summary>
        <p>Check that you saved the file you opened, then refresh the browser.</p>
      </details>
      <details name="file-review">
        <summary>Why does my link reach the wrong file?</summary>
        <p>Read the path from the folder containing the current page.</p>
      </details>
    </main>
  </body>
</html>
Browser previewDocument title: One Question at a Time

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: two grouped questions. In a supporting browser, opening the second closes the first. In an older browser without named grouping, each can remain open independently. The content is available either way.

Problem and solution

Problem: a student writes open="false" but the answer still appears.

Solution: open is a boolean attribute. Remove it entirely for a closed starting state. Use the normal label to test both states.

Common mistakes and useful tips

  • Put one summary first inside details.
  • Do not use open="false" to mean closed.
  • Do not hide essential instructions.
  • Do not remove keyboard focus styling.
  • Do not treat collapsed content as secret.
  • Test named groups instead of assuming identical browser support.

Practice questions

  1. Which element provides the visible label?
  2. Which answer begins open in the first program?
  3. How do you make a details element start closed?
  4. What does the shared name do?
  5. Does a closed answer disappear from the source?
  6. Save the first program and toggle both labels with the keyboard. What remains visible?
Check your practice answers
  1. Summary.
  2. The second, because its details element has open.
  3. Omit open rather than setting it to false.
  4. It groups the disclosures so supporting browsers allow one open member at a time.
  5. No. It is still document content.
  6. Each summary remains visible; only its extra content changes visibility. No JavaScript is needed for this basic action.

Interview questions

Do details and summary require JavaScript?

Not for their native opening and closing behaviour. Extra behaviour, such as custom persistence, is separate.

How is open different from an ordinary text setting?

It is boolean: its presence means true regardless of a value such as false.

Why choose a specific summary label?

It tells readers which information they can reveal and makes several disclosures easier to distinguish.

When is details unsuitable?

When the hidden information is essential to noticing or completing the current task. Keep that information visible instead.

Quick revision

  • Summary names the expandable area.
  • Open sets the starting state.
  • Shared names can group answers.
  • Test mouse and keyboard operation.