On many FAQ pages (lists of frequently asked questions), an answer appears only when you click its question, and it is hidden again when you click the question once more. In HTML you can build an open-and-close part like this with two tags: <details> and <summary>. You don't need any extra code. The browser shows and hides the answer for you.
One open-and-close block
We will call one <details> and everything inside it an open-and-close block:
<details>
<summary>What is CSS?</summary>
<p>CSS is used to style HTML elements.</p>
</details>
<details>is the block's opening tag. Everything that should open and close goes inside the block.<summary>holds the block's title: the text that is always visible and can be clicked. Write it as the first thing inside<details>.<p>holds the answer. The browser hides everything in<details>except<summary>until the title is clicked.</details>is the block's closing tag.
The page shows only "What is CSS?" with a small triangle in front of it. Click the title and the answer appears below it. Click again and the answer is hidden again.
Forgot the <summary>? The browser still makes a title of its own, with the default text "Details". Visitors can't tell what is inside the block. So always write a <summary> with clear text.
Open from the start with open
A <details> block is normally closed when the page loads. Want the answer visible right away? Add the open attribute:
<details open>
<summary>Shop opening hours?</summary>
<p>Every day, from 8 am to 9 pm.</p>
</details>
The attributes you've met so far have a value, like href="..." on a link. open is different: just write its name in the <details> opening tag, with no = and no quote marks.
The answer (the shop's hours) is visible as soon as the page loads. Visitors can still close the block by clicking its title.
Write open in the <details> tag, not in <summary>. In <summary> it does nothing.
Several blocks make an FAQ
Each question gets its own <details> and <summary>. Write the blocks one after the other:
<details open>
<summary>First question</summary>
<p>First answer.</p>
</details>
<details>
<summary>Second question</summary>
<p>Second answer.</p>
</details>
On the page, the two questions show one after the other. The first answer is visible from the start, because only the first block has open. The second answer appears only when its question is clicked.
Want only one answer open at a time? Give every <details> a name attribute with the same value, for example name="faq". When you open one block, the others close automatically.
⚠️ Blocks that won't open and close properly
- If the
<summary>is outside<details>, the title becomes plain text and can't be clicked. - If one
<details>holds every question, all the answers open and close together. Each question needs its own block. - An answer written after
</details>always shows and is never hidden. Write the answer before</details>.
TL;DR: <details> wraps one open-and-close block. The <summary> inside it is the clickable title, and the other content stays hidden until that title is clicked. Without a <summary>, the title is "Details". Add open to <details> to make the block open from the start. For an FAQ, write one <details> per question, one after the other. Give every <details> the same name attribute if only one block should be open at a time.
⚡ A title you can click
Write:
<details>
<summary>What is CSS?</summary>
<p>CSS is used to style HTML elements.</p>
</details>
Meaning: The <summary> is the title that is always visible and can be clicked. The rest of <details> stays hidden until that title is clicked. You need no extra code: the browser opens and closes the <details>.
⚡ A missing <summary>
Guess first: if there's no <summary>, what's the title?
"Details". The browser makes that title itself, so visitors can't tell what is inside.
Meaning: Always write a <summary> with clear text, as the first thing inside <details>.
⚡ Open from the start
Write:
<details open>
<summary>Shop opening hours?</summary>
<p>Every day, from 8 am to 9 pm.</p>
</details>
Meaning: The open attribute makes the <details> open when the page loads. Just write its name, with no = and no quote marks, in <details>, not in <summary>. For an FAQ (a list of questions and answers), each question gets its own <details>.