Collapsible Sections in Markdown: How details and summary Work
You make a collapsible section in Markdown by wrapping content in the HTML details element and giving it a summary element as its label. Markdown has no collapse syntax of its own, so this works only where your renderer passes that HTML through. GitHub does. Many other tools do too, but not all of them.
Quick answer
Three pieces make the pattern: a details wrapper, a summary line that acts as the clickable label, and your content. Put a blank line between the HTML tags and your Markdown, and the Markdown renders normally inside the collapsed block.
Here is the shape of it, as it would sit in a README:
<details>
<summary>Show the full error log</summary>
Anything you write here stays hidden until the reader clicks.
npm ERR! code ELIFECYCLE npm ERR! errno 1
</details>That's the whole technique. The rest covers why the blank lines matter, how to open a section by default, and what happens when a renderer ignores the HTML.
Step-by-step: the basic pattern
GitHub's documentation describes the details tag as a way to hide technical details that not every reader needs, such as a long log in an issue comment. Any Markdown inside the block stays hidden until the reader clicks the right-pointing triangle. The summary tag goes inside details and tells readers what is inside; its label appears to the right of the triangle.
GitHub's own example puts a heading, a sentence and a fenced code block inside the collapsed area, with a blank line after the opening tag, after the closing summary tag, and before the closing details tag. Copy that spacing. The page doesn't say why, but the reason is in the Markdown spec, covered next.
Write the summary as a real label, not "Click here". A reader scanning a long README should know what they would get by expanding: "Show the full error log" or "Example config for Linux".
Why blank lines matter
The CommonMark spec defines an HTML block as a group of lines treated as raw HTML. Both details and summary are on the list of tag names that start one. That kind of block runs until the next blank line, and everything up to that point is passed through untouched.
So Markdown on the line right after the opening tag is never parsed. The spec shows the same thing with a div. With blank lines around the text, the emphasis renders. Without them, the asterisks stay literal:
<div>
*Emphasized* text.
</div>Separating Markdown from HTML with blank lines is, in the spec's words, a very simple and flexible way of including Markdown inside HTML tags. If you want to see how that spec came to exist, read how Markdown got a spec.
Two related traps:
- Indentation. Indent the opening tag by four spaces and it becomes an indented code block, so the tag shows up as visible text.
- Fenced code right after a tag. A fenced code block placed directly after an HTML line, with no blank line, is not parsed as code. It stays part of the HTML block.
If your collapsed content comes out as raw asterisks or visible backticks, add blank lines first. That fixes most cases.
Open by default, and exclusive groups
To show a section expanded when the page loads, add the open attribute to the details tag. MDN's reference notes that it is a Boolean attribute: the details are shown when it exists and hidden when it is absent. That means open="false" still opens the section. To close it, remove the attribute entirely.
<details open>
<summary>Setup notes (expanded by default)</summary>
Readers see this immediately and can collapse it.
</details>MDN also documents a name attribute. Give several details elements the same name and only one stays open at a time, which gives you an accordion without scripting. Browser support isn't the limit: MDN marks details as widely available since January 2020. Support in your Markdown renderer is another question, and attribute handling can vary by tool, so test it where it will be read.
Two smaller facts worth knowing. If you leave out the summary, the browser shows a default label of its own choosing, so always write one. And readers can open a focused section with the space bar, not only the mouse.
When the section does not collapse
Because collapsing depends on raw HTML, the result depends on the renderer. If a tool strips or escapes HTML, your section either shows fully expanded or shows the tags as text. A static site generator with HTML disabled, a notes app with a limited preview, or a chat tool can each behave differently.
Before you rely on it:
- Preview the file in the place readers will see it.
- Keep the hidden content readable even if the tags show. A plain label line and the content beneath it still make sense as text.
- Avoid hiding steps people must follow. If the instructions are essential, leave them visible.
This is the same portability question that comes up with other extensions, such as why some Markdown checkboxes are clickable and others aren't. The file stays valid Markdown either way, but the experience changes with the tool.
Where collapsible sections earn their place
They work best for material that is useful to a few readers and noise to the rest: long logs, full config files, per-platform install notes, or a changelog of old versions. They fit naturally in a README people will actually read, where the first screen should answer the main question and the long tail waits behind a label.
They work poorly for anything a reader must see to proceed. They're also awkward when a page will be printed or exported, because a collapsed block may not print its content.
Doing this with Carets
Carets is a native notes and code editor for iPhone, iPad and Mac, and it keeps everything as plain text. That suits this technique. A collapsible section is just a few lines of HTML and Markdown in a file, so you can write it, read it, and move the file to GitHub without any conversion.
Three features help when you write these blocks:
- Markdown, plain text and code in one editor. You can draft the README, the details wrapper and the fenced log in one file.
- Syntax highlighting for code and config files. The code you place inside a collapsed section is easy to scan while you edit.
- Files, projects and tags. Keep a snippet file with your details template, tagged so you can find it from your phone or your Mac.
Your files stay plain text, so they remain portable. Always preview in the destination, since how a section collapses is decided by the site or app that renders it, not by the file. Get Carets for iPhone, iPad and Mac on the App Store.
Frequently Asked Questions
Is there a pure Markdown syntax for collapsible sections?
No. Collapsing relies on the HTML details and summary elements, which Markdown passes through as raw HTML. Renderers that strip or escape raw HTML show the content expanded or show the tags as text.
How do I make a collapsible section open by default?
Add the open attribute to the details tag. It is a Boolean attribute, so open="false" still opens it. Remove the attribute to close the section.
Why is my Markdown inside details not rendering?
Usually a missing blank line. A details tag starts a CommonMark HTML block that ends at the next blank line, so Markdown only renders when blank lines separate it from the tags.