HTML5 Beginner Semantic Structure · 6 min read

HTML Disclosure Widgets with details and summary

Lesson 17 of 36 in the HTML5 learning path.

How do I build a native accordion without JavaScript?

The details element is a native disclosure. A summary element inside it is the visible question, and the rest of the content is hidden until the question is activated. The browser supplies the open and close behavior, the keyboard toggle, and the disclosure state. You do not need a script to build this FAQ. The name attribute groups several details elements so that, in current browsers, opening one question closes the others. The open attribute marks the question that should start expanded.

Use details and summary for an FAQ, a transcript, or a block of extra help under a lesson. The summary should be the question itself, not the words click here. Do not put a form the student must complete inside a closed details if missing it would skip a required step. For a modal that must hold focus, use a dialog instead. This widget is a disclosure, and HTML5 already implements it.

Syntax & Parameters

Syntax
<details name="course-faq"><summary>Question</summary><p>Answer</p></details>

Example

Interactive preview available
faq.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Course FAQ</title>
  <style>
    body { margin: 0; font: 1.05rem/1.55 Georgia, "Times New Roman", serif; color: #1c1917; background: #faf7f2; }
    header { background: #1e3a5f; color: #f8fafc; padding: 1.25rem 1.5rem; }
    main { max-width: 42rem; margin: 0 auto; padding: 1.25rem 1.5rem 2rem; }
    h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; }
    p { margin: 0 0 0.85rem; }
    details { background: #fff; border: 1px solid #e7e5e4; padding: 0.7rem 0.9rem; margin: 0 0 0.6rem; }
    summary { font-weight: 700; cursor: pointer; }
    details p { margin: 0.6rem 0 0; }
  </style>
</head>
<body>
  <header>
    <p>North Campus, Web Systems 1</p>
  </header>
  <main>
    <h1>Course FAQ</h1>
    <p>Each question is a summary. The answer sits inside details, so the disclosure works with no script. The shared name groups the questions so opening one closes the others in current browsers.</p>
    <details name="course-faq">
      <summary>When is lab two due?</summary>
      <p>Lab two is due Friday at 17:00. Submit the HTML file and a screenshot of the browser.</p>
    </details>
    <details name="course-faq" open>
      <summary>Can I use a framework this week?</summary>
      <p>No. The point is native HTML you can read in view source. A framework can wait until the document structure is familiar.</p>
    </details>
    <details name="course-faq">
      <summary>Where are office hours?</summary>
      <p>Thursday from 15:00 to 16:00, in room B204. Email the instructor if that hour is already full.</p>
    </details>
  </main>
</body>
</html>

Expected Output

The page shows Course FAQ, an introduction, and three questions. Can I use a framework this week? is open, and its paragraph about native HTML is visible. The due-date and office-hours questions show only their summaries.

Activating When is lab two due? reveals the Friday 17:00 sentence and, in a current browser, closes the framework answer because both details share name="course-faq". Activating a summary with the keyboard does the same toggle. There is no network request and no script. Older browsers that ignore name still open and close each details on its own, and the open attribute still starts the framework answer expanded.

Line-by-Line Walkthrough

  1. The page is a complete document with a heading and a short introduction. There is no script element, because the widget does not need one.
  2. Each question is a details element. The summary is the first child and is the visible label of that disclosure.
  3. The answer is a paragraph inside the same details, after the summary. While the details is closed, that paragraph is not shown.
  4. All three details share name="course-faq". In browsers that support the name attribute, the group behaves as one open panel at a time.
  5. The framework question has the open attribute, so it is expanded when the page loads. The other two start closed.
  6. Activating a summary toggles its details. Opening a closed question in the group closes the one that was open.
  7. The summary text is the question, such as When is lab two due? It is not a generic command.
  8. Keyboard users can reach each summary and toggle it. The browser provides that behavior with the element.
  9. A dialog is a different tool. This FAQ does not inert the rest of the page or move focus into a modal layer.

Load the page and confirm the framework answer is visible. Activate the office-hours question. The framework answer should close, and the hours paragraph should show, with no script running.

Common Pitfalls & Security

details is a disclosure, not a security boundary and not a modal. Content inside a closed panel is still in the HTML. People can find it, and a required field hidden there is easy to miss.

  • A summary that says click here gives every question the same name. Use the question text.
  • Putting the only submit button for a required form inside a closed details hides the next step.
  • name groups the widgets only in browsers that implement it. Do not assume an older browser will close the others for you.
  • details does not trap focus or inert the page. A confirmation that must interrupt the task belongs in a dialog.
  • Hiding the answer with CSS while leaving details closed, or the reverse, fights the built-in state. Use the open attribute instead of a class to represent open.

Best Practices & Modern Standards

HTML5 details and summary are the default accordion. Write a real question in the summary, put the answer after it, and add open on the item that should start visible. Use a shared name when the design is one open answer at a time, and know that support for name is the progressive part.

Keep required tasks outside the disclosure. Do not add a script that reimplements the toggle unless you are replacing the element entirely and are prepared to handle keyboard and state. For a modal, move to the dialog element rather than stretching details into a fake overlay.

Real-World Use

The course FAQ page on the Web Systems site is this group of details. Students open the due date, the framework rule, or the office hours without waiting for a script. The framework answer starts open because it is the rule people miss in week one.

The same widget fits a transcript under a lecture video and a short help note under a lab step. The department does not use it for the enrollment form. That form stays visible. A booking confirmation that must take over the page uses a dialog instead of another details panel.

Frequently Asked Questions

Do I need JavaScript to open and close details?

No. The browser toggles a details element when its summary is activated, including from the keyboard. The sample page has no script. Add script only if you need behavior the element does not provide, and prefer not to replace the built-in toggle.

What does the name attribute do on details?

A shared name groups the details elements. In browsers that support it, opening one closes the others in that group, which is the accordion behavior. Without support, each details still opens and closes on its own. The open attribute still chooses the one that starts expanded.

Is a closed details hidden from view source?

No, closed only changes what is shown, and the answer remains in the HTML for search and assistive technology. Do not put a secret in a closed panel. It is not a lock.

When should I use dialog instead of details?

Use dialog when the task must interrupt the page, trap focus, and inert the background, such as booking a slot. Use details when the answer can stay in the page flow and the person may ignore it. The FAQ in this lesson is the second case.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…