Skip to content

Instantly share code, notes, and snippets.

@pierrejoubert73
Last active July 17, 2024 20:17
Show Gist options
  • Save pierrejoubert73/902cc94d79424356a8d20be2b382e1ab to your computer and use it in GitHub Desktop.
Save pierrejoubert73/902cc94d79424356a8d20be2b382e1ab to your computer and use it in GitHub Desktop.
How to add a collapsible section in markdown.

How to add a collapsible section in markdown

1. Example

Click me

Heading

  1. Foo
  2. Bar
    • Baz
    • Qux

Some Javascript

function logSomething(something) {
  console.log('Something', something);
}

2. Code/Markdown

<details>
  <summary>Click me</summary>
  
  ### Heading
  1. Foo
  2. Bar
     * Baz
     * Qux

  ### Some Javascript
  ```js
  function logSomething(something) {
    console.log('Something', something);
  }
  ```
</details>

3. Tips & Tricks

3.1 Expand by Default

To have a collapsible section expanded by default, simply include the 'open' attribute within the <details> tag:

Hello World!
<details open>
  <summary>Hello</summary>
  World!
</details>

3.2 Customize Clickable Text

You can modify the appearance of the clickable text by adding styling inside the <summary> tags:

Wow, so fancy WOW, SO BOLD
<details>
  <summary><i>Wow, so fancy</i></summary>
  <b>WOW, SO BOLD</b>
</details>

3.3 Nested Collapsible Sections

NB: When including headings within collapsible sections, remember to add a new line after the <summary> tag.

Section A
Section A.B
Section A.B.C
Section A.B.C.D Done!
<details>
<summary>Section A</summary>
<details>
<summary>Section A.B</summary>
<details>
<summary>Section A.B.C</summary>
<details>
<summary>Section A.B.C.D</summary>
  Done!
</details>
</details>
</details>
</details>

Troubleshooting

  • If certain markdown or styling, such as # My Title, fails to render in the collapsible section, try adding a line break after the </summary> tag.
  • If your section fails to render, it might be malformed. Consider copying the functional examples provided here and building from there!
@brunolnetto
Copy link

@Sterh20 the table rendering works without markup <summary>Click me</summary>.

@Sterh20
Copy link

Sterh20 commented Dec 12, 2023

@Sterh20 the table rendering works without markup <summary>Click me</summary>.

@brunolnetto
I was too hasty. Turns out you can make it work with summary section too. You just need to add additional empty line after summary section:

Click me
Header 1 Header 2
Row 1 Row 1
Row 2 Row 2
<details>

  <summary>Click me</summary>

  | Header 1 | Header 2 |
  | -------- | -------- |
  | Row 1    | Row 1    |
  | Row 2    | Row 2    |
  
</details>

@brunolnetto
Copy link

Marvelous! 🥂

@jonchen727
Copy link

Block quotes don't work

[!NOTE]
Not working :(

Note

It works here

<details>

<summary>Block quotes don't work</summary>

> [!NOTE]
> Not working :(

</details>

@cloudbring
Copy link

A few issues:

  1. Nesting only goes 4 levels deep
  2. HTML code in a nested details tag messes up the accordion

Invalid Code Example

  • Trying to write an example here also breaks everything
<details>
<summary><h1>H1</h1></summary>

> :warning: Issue: Doesn't show header underline


```markdown
<details>
<summary>First Level</summary>
## Fun Content

<details>
<summary>2nd Level with a Code Block of a Details Tag</summary>

```markdown
<details>
<summary>Details Tag inside an embedded markdown code block</summary>
## Embedded Fun Content
</details>
``` ```

H1

⚠️ Issue: Doesn't show header underline

Code

<details>

<summary><h1>H1</h1></summary>

# H1 Inside

## H2

### H3

#### H4

##### H5
</details>

H1 Inside

H2

H3

H4

H5

H2

⚠️ Issue: Doesn't show header underline

H1 Inside

H2

H3

H4

H5

H3

H1 Inside

H2

H3

H4

H5

Embedded Headers

H1 Embedded with Nested Details with Headers

⚠️ Nesting only goes 4 levels deep

  • H5 doesn't show content even though markdown goes 5 levels deep

H1

H2

H3

H4

H5
A Sixth Level for Fun and Breakage

H1 Inside

H2

H3

H4

H5

H1 Inside

H2

H3

H4

H5

H1 Inside

H2

H3

H4

H5

H1 Inside

H2

H3

H4

H5

H1 Inside

H2

H3

H4

H5

H1 Inside

H2

H3

H4

H5

H3

H4

H5

@ngouy
Copy link

ngouy commented Jan 15, 2024

You're welcome

There is kinda no point. It's not markdown anymore, it's "just" pure - yet valid - HTML

Here is something I came up with that works. An indented list with collapsable sections.
Each section can have it's own re-indented, list, with collapsable items, + regular MD content

The tricky part is how GH-flavored-md react to spaces and such. You have to indent your whole section in a specific way, and your closing </details> tag must be indented as if it was part of the body

For it to work, I suggest you keep the spaces and newlines intact

  • Section 1 with a link
    • subsection 2 with a link
      • a list
      • with some stuff

      and other things

      • like
      • a task list
    • another subsection

      a. with another list
      b. and some other stuff
      d. and,
      more,
      classic,
      md
      e. no need of html

    • last sub-section

      blablabla

      def some_code
        puts "Rails is so cool"
      end
    • a random not collapsable section

      legacy. Should be restructured.

      console.log("look what I found, a new js framework. Still nothing even close to being as efficient as rails though...")
    • and another collapsable section

      ...

  • section 2

    some parent content

    and another list

    • section 2.1

      and some content

    • section 2.2

      and some content

    • section 2.3
      and some no collapsed content

the code:

- <details open>
  <summary><a href="#">Section 1 with a link</a></summary>

  - <details>
    <summary><a href="../src/actions/">subsection 2 with a link</a></summary>

    - a list
    - with some stuff

    > and other things

    - [x] like
    - [ ] a task list 

    </details>

  - <details>
    <summary><b>another subsection</a></summary>

    a. with another list
    b. and some other stuff
    d. [and](),
      [more](),
      [classic](),
      [md]
    e. _no need_ __of html__
    </details>

  - <details>
    <summary>last sub-section</a></summary>

    blablabla

    ```rb
    def some_code
      puts "Rails is so cool"
    end
    ```
    </details>

  - a random not collapsable section
    > legacy. Should be restructured.

    ```js
    console.log("look what I found, a new js framework. Still no real alternative to rails though")
    ```

  - <details>
    <summary>and another collapsable section</summary>

    ...
  </details>

- <details>
  <summary>section 2</summary>
      
  some parent content

  and another list

  - <details>
    <summary>section 2.1</summary>

      and some content
    </details>

  - <details>
    <summary>section 2.2</summary>

      and some content
    </details>
  
  - section 2.3
    and some no collapsed content

</details>

@Snailedlt
Copy link

Block quotes don't work
Note

It works here

<details>

<summary>Block quotes don't work</summary>

> [!NOTE]
> Not working :(

</details>

Seems like a problem with all alerts inside of divs. I made a comment on the thread that introduced alerts here: https://github.com/orgs/community/discussions/16925#discussioncomment-8383959

@TidbitSoftware
Copy link

TidbitSoftware commented Feb 20, 2024

@fightling Not without JavaScript.

@MelSumner This is a really important point. I think that a lot of us will probably shrug and go ahead anyway as we "need" accordion in Markdown (unfortunately). I read the first article you linked. What do you think the fix here might be? Is it up to the screen-reading software to handle the case of details being used in this way? Should accessibility be incorporated into the HTML standard? Edit: it looks like one way around this would be to precede the details section with a Markdown heading,

# Heading
<details>
  <summary>Read More</summary>
  Content
</details>

@MelSumner
Copy link

@TidbitSoftware it would be something like

<details>
  <summary>Read more about penguins</summary>
  <div class="content">
    <p>Interesting paragraph text about penguins</p>
    <p>Another interesting bit of paragraph text about penguins</p>
  </div>
 </details>

@moshiurse
Copy link

Also, for anyone interested, you CAN get the summary to work as a title; you just have to use html tags:

This is an h2 title

And it is collapsible!

Here's how it works:

<details><h2><summary>This is an h2 title</summary></h2>
Content goes here.
</details>

Its not working!!

@pierrejoubert73
Copy link
Author

pierrejoubert73 commented Mar 5, 2024

This is an h2 title

Content goes here.

@moshiurse You put the <h2> tags in the wrong place. They go inside <summary>, not outside.

This is an h2 title

Content goes here.

@moshiurse
Copy link

<details>
<summary><h5>See Here</h5></summary>

image

But it is going down. Can you help me to fix?

@pierrejoubert73
Copy link
Author

@moshiurse You mean the new line after the caret?

@moshiurse
Copy link

@moshiurse You mean the new line after the caret?

Yes it is added as a next line

@pierrejoubert73
Copy link
Author

@moshiurse, Could you paste your markdown here and see how it renders? If the behaviour is reproducible, we can debug it. If the behaviour is normal, then the problem is in your environment.

@ProphecyOak
Copy link

counterSorter.py

For a given file, either counts or sorts it and places the output in CounterSorterOutput by default.

  • If you are sorting, include an s or the word sort after the specified file.
  • If you are counting, include a c or the word count after the specified file.

For either option, you may:

  • Specify a designated output file using the -d or --dest flag.
  • Specify a sorting function using the -m or --method flag and a key from the SORT_FUNCTIONS dictionary in properties.py.
  • Invert the sort direction using the -r or --reverse flag.
  • Ignore the header of the file using the -s or --skip flag and a number of lines to skip.

For whatever reason, this works in the chat here but not in the readme for my repo.

<details><summary><h3>counterSorter.py</h3></summary>
 >For a given file, either counts or sorts it and places the output in `CounterSorterOutput` by default.
 >- If you are sorting, include an `s` or the word `sort` after the specified file.
 >- If you are counting, include a `c` or the word `count` after the specified file.
 >
 >For either option, you may:
 >- Specify a designated output file using the `-d` or `--dest` flag.
 >- Specify a sorting function using the `-m` or `--method` flag and a key from the `SORT_FUNCTIONS` dictionary in `properties.py`.
 >- Invert the sort direction using the `-r` or `--reverse` flag.
 >- Ignore the header of the file using the `-s` or `--skip` flag and a number of lines to skip.
</details>

This is the result in my repo:

image

Any idea why this might be happening?

@pierrejoubert73
Copy link
Author

@ProphecyOak I headed over to some online markdown editors and tested it there. None of them rendered the collapsible section.
They cannot interpret the elements like GitHub does (It is HTML, after all). I believe that whatever is rendering your markdown will look at those elements, see that they are HTML, and interpret them as best they can. Can you inspect those elements?

@moshiurse
Copy link

moshiurse commented Mar 6, 2024

@moshiurse, Could you paste your markdown here and see how it renders? If the behaviour is reproducible, we can debug it. If the behaviour is normal, then the problem is in your environment.

See Here

Sometimes with a root user, things might not work properly. So creating a new user with sudo permission is a better option.
<details>
<summary><h2>See Here</h2></summary>
  Sometimes with a root user, things might not work properly. So creating a new user with sudo permission is a better option.
</details>

Looks like it working in this comment but in my repo it is not working!!
For directly check the md file you can check this: https://github.com/moshiurse/moshiurse/blob/main/Cloud-Server-Setup.md

@pierrejoubert73
Copy link
Author

pierrejoubert73 commented Mar 6, 2024

@moshiurse Interesting. It looks fine to me. Perhaps it is browser-related. Which browser/version are you running?
Edit: Sorry, I just realized you said it looks fine here. But the fact that it still looks ok to me on your md but not for you is quite odd. Do you think it could be a responsive issue?
image

@moshiurse
Copy link

@moshiurse Interesting. It looks fine to me. Perhaps it is browser-related. Which browser/version are you running? Edit: Sorry, I just realized you said it looks fine here. But the fact that it still looks ok to me on your md but not for you is quite odd. Do you think it could be a responsive issue? image

I don't use h2 tag here
When i am using it looks like this
image
Now I update the md file you can see. https://github.com/moshiurse/moshiurse/blob/main/Cloud-Server-Setup.md

@thc282
Copy link

thc282 commented Mar 6, 2024

@moshiurse
about the tag problem
i have tried, if the h2 outside the details is fine. it won't have any new line in the summary. However all the content inside the details will becomes h2

And i think this just the github issue. In long ago, i have tried the same trick in my repo. It work well and dont have any newline, now that repo summary text just move to next line

@beatrizsmerino
Copy link

Hi @moshiurse!

I have the same problem, although it didn't happen a few days ago.

Here you can see something about:

I tried everything and in the end I had to use svg images.
In short, the problem is that a few days ago Github didn't treat h1 etc. as block elements, so the arrow and the title were seen side by side.

@DolevArtzi
Copy link

@Sterh20 the table rendering works without markup <summary>Click me</summary>.

@brunolnetto I was too hasty. Turns out you can make it work with summary section too. You just need to add additional empty line after summary section:

Click me

<details>

  <summary>Click me</summary>

  | Header 1 | Header 2 |
  | -------- | -------- |
  | Row 1    | Row 1    |
  | Row 2    | Row 2    |
  
</details>

Wanted to add that this also works for LaTeX, including double $$ blocks. see example:


Rendered Output:

Proof of variance of geometric

Reveal Proof To compute the variance of $X$, where $X \sim \text{Geom}(p)$, we'll start by finding the second moment. $$E[X^2] = E[X^2 \mid A]P[A] + E[X^2 \mid A^c]P[A^c]$$

Code:

 <details><summary>Reveal Proof</summary>
 To compute the variance of $X$, where $X \sim \text{Geom}(p)$, we'll start by finding the second moment. 
 $$E[X^2] = E[X^2 \mid A]P[A] + E[X^2 \mid A^c]P[A^c]$$
</details>```

@jhj0517
Copy link

jhj0517 commented Apr 5, 2024

Hi! I want to test if I can embed image inside detail.

demo Content1

Content2

ForegroundService

It works! very thanks.

@brendanmaguire
Copy link

Click The rest

@KelvinKaungDev
Copy link

@lebathang
Copy link

test Hello everyone here

code:

 <details>
 <summary>test</summary>
 <b>Hello everyone here<b>
 </details>

@vinser
Copy link

vinser commented Apr 29, 2024

Using this code in my project README.md on GitHub

### Setup and fine tuning
<details>
<summary><i><h4>1. Main configuration file</h4></i></summary>
For advanced sutup you can edit `config/config.yml` selfexplanatory configuration file.  
This file by default is located in `config` subfolder of program file location.
</details>

I get what it should be:

Setup and fine tuning

1. Main configuration file

For advanced sutup you can edit `config/config.yml` selfexplanatory configuration file. This file by default is located in `config` subfolder of program file location.

But when I use the same code on GitHub Pages with classic minima skin I get line break between marker and summary tip the same as moshiurse wrote above.
image

Are any thoughts how to fix this?

@mayank7j-shell
Copy link

Testing if really need to add indentation. Turns out, not really.

Click me
Header 1 Header 2
Row 1 Row 1
Row 2 Row 2

Code:

<details>

<summary>Click me</summary>

| Header 1 | Header 2 |
| -------- | -------- |
| Row 1    | Row 1    |
| Row 2    | Row 2    |
  
</details>

@tmarkovski
Copy link

tmarkovski commented Jun 4, 2024

You can also use code highlighting in the body or summary, with a caveat: must use <code>hello</code> block, and not backticks `hello` when in summary.

Code:

<details>
<summary>Contents of <code>file.txt</code></summary>

```
[File contents inside code block]
```
</details>

Rendered:

Contents of file.txt
[File contents inside code block]

@Mark-Landucci
Copy link

Is it possible to place a collapsible section in a table? To have several table rows that the user can expand or collapse?

@Mark-Landucci
Copy link

For example, can I place these in a table with columns and rows? I've been trying, but haven't figured out a way to do it correctly.

Dogs Likes to bark a lot.
Cats Likes to meow at night.
Birds Likes to fly away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment