Sub topic 12
Collapsible sections and the HTML GitHub allows
This chapter assumes you can write basic GFM headings, lists, and code blocks, and that you have edited a README on GitHub. Markdown lets you embed raw HTML. GitHub renders the safe part of it and removes the rest.
Why use HTML in a README
Some jobs have no Markdown syntax. You cannot fold away a long section, show keyboard keys, or set an image width with Markdown alone. A README often needs these, for example to hide a long troubleshooting list so the page stays short.
Collapsible sections
The details element makes a section that readers can expand. The summary element holds the visible label.
<details>
<summary>Troubleshooting</summary>
If the install fails, delete `node_modules` and run the command again.
1. Remove the lock file.
2. Reinstall.
</details>The blank lines after </summary> and before </details> matter. Without them, GitHub treats the contents as raw HTML, and Markdown such as backticks and lists is not rendered.
To show a section expanded by default, add the open attribute: <details open>.
HTML that GitHub keeps
GitHub sanitizes HTML before it displays it. These elements are commonly used and render as expected:
| Element | Use |
|---|---|
| `details`, `summary` | Collapsible sections |
| `kbd` | Keyboard keys, such as Ctrl+C |
| `sub`, `sup` | Subscript and superscript |
| `br` | Forced line break |
| `img` | Image with `width` or `height` |
| `picture` | Different images for light and dark themes |
A short example that uses several of them:
Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to stop the server.
H<sub>2</sub>O and x<sup>2</sup>
<img src="docs/logo.png" alt="Project logo" width="120">HTML that GitHub removes
The sanitizer strips anything that could run code or change page layout:
scriptandstyleelements.iframeand other embedded content.styleattributes and mostclassandidattributes.- Event attributes such as
onclick.
If your README looks fine in a local previewer but loses its styling on GitHub, inline CSS is the usual cause. Use plain Markdown or a permitted attribute such as width instead.
Common errors
- Missing blank line inside
details, so Markdown shows as plain text. - An unclosed
detailstag, which hides the rest of the page. - Using
style="color:red"and expecting color. - Relying on HTML for content that plain Markdown can express, which makes the source harder to read in a text editor.
Exercise
Your collapsed section shows the literal text **Step 1** instead of bold text. What is the most likely cause, and how do you fix it?
Show answer
There is no blank line between the </summary> tag and the content, so GitHub treats the content as HTML and does not render Markdown. Add a blank line after </summary> (and one before </details>).
Exercise
You add <span style="color:red">Deprecated</span> to your README. What does GitHub display, and what could you use instead?
Show answer
GitHub removes the style attribute, so the text appears in the normal color. Use plain Markdown such as bold text, or a warning alert, to mark the item as deprecated.