45

How do I add foldable code or text on Stack Overflow when writing a question or an answer?

<details>
  <summary>Click to expand!</summary>
```HTML
<h1>Hide this until someone presses the show/fold button</h1>
```
</details>

Hide this text until someone taps the show/fold button.


This is what I saw on GitHub and wondered if you could have this on Stack Overflow?

https://gist.github.com/pierrejoubert73/902cc94d79424356a8d20be2b382e1ab

17
  • 3
    You cannot. We have no collapsible blocks. There is no reason to collapse anything. If it shouldn't be shown, don't include it in the answer.
    – Cody Gray Mod
    Commented Apr 17, 2022 at 4:45
  • 33
    I was thinking it would be useful to minimise the length of an answer, and for those people that didn't understand the short stuff can check what's inside. Commented Apr 17, 2022 at 4:48
  • 6
    Or, they could simply scroll past it, if you use section headings and otherwise write clearly enough for people to be able to understand the organization of your answer. Hiding content makes moderation more difficult, and it is irritating to many besides.
    – Cody Gray Mod
    Commented Apr 17, 2022 at 5:18
  • 9
    The only collapsible "code blocks" are code snippets. They can be collapsed by default, but they should only be used for JavaScript/HTML/CSS. Here's an example of how they look like when collapsed vs expanded.
    – 41686d6564
    Commented Apr 17, 2022 at 5:52
  • 2
    @41686d6564 This is something I was looking for, except I have Python code. I understand it's not going to work with Python, since Python can be used for so much stuff. But if they just added the collapsible then it would work. Commented Apr 17, 2022 at 5:57
  • 3
    FWIW, spoiler blocks used to collapse when hidden, but that feature was removed.
    – PM 2Ring
    Commented Apr 17, 2022 at 6:44
  • 4
  • 2
    "code drop down" is probably collapsible text or code folding. Commented Apr 18, 2022 at 10:04
  • 1
    While this is not tagged as a feature request, there is also Collapsible text blocks in questions/answers (2012) Commented May 1, 2022 at 7:46
  • 11
    @CodyGray Very frequently I want to add a debug log dump to my questions, but I don't want to make people scroll past it (can be thousands of lines of data. I don't know what in particular an expert would want to see, so I just include it all). Very common and considerate reason for collapsible blocks
    – Connor
    Commented Oct 19, 2022 at 22:09
  • 6
    A +1 from me, I just posted this question and a collapsible element would have improved readability. Commented Oct 30, 2022 at 19:49
  • 13
    @CodyGray, a moderator should be open to suggestions, not strangle them with killer phrases like: "There is no reason to collapse anything. If it shouldn't be shown, don't include it in the answer." Or: "they could simply scroll past it" Workarounds do not replace features, and there are good reasons to have collapsible sections. Resistance to change, instead of embracing it, is one of the main reasons for lack of innovation, not just here on this platform, but generally. Stack Overflow is merely an example. This is for developers by developers. We all should be more open-minded.
    – kriegaex
    Commented Mar 9, 2023 at 14:42
  • 13
    Nowhere did I say that you ought not have an opinion. I said you should not use killer phrases. I can only describe how I feel when I see the "Mod" tag on a post: I think that someone speaks with authority and maybe even on behalf of the platform owner. My guess is that many others might feel the same way, even though it might be incorrect. I am not criticising your opinion, even though I strongly disagree with it. I am cricicising the way you utter it, strangling further discussion. I am not easily intimidated, which is why I am speaking up. But others might be.
    – kriegaex
    Commented Mar 10, 2023 at 7:04
  • 6
    @CodyGray-onstrike I strongly disagree - like anything, folded text could be misused but in some cases it could be very nice. For example, someone is encountering an error with a traceback that is a few hundred lines long and wants to include that as it might contain useful info. However, people with a similar error wouldn't need the traceback information, they'd just want to view a high-level description of the problem and the solution. Collapsible text would make information available to problem solvers and save answer seekers from a ton of scrolling. PS: gl w/ your strike. Commented Jan 4 at 18:22
  • 4
    Hey mod, don't impose your opinion on everyone. It is assumed that you'd influence the endorsement of features on this platform. Putting a log's stacktrace in a collapsible section is one of MANY reasons such a useful feature should be in these forums. It seems to me everyone is making a good case for this. Humility is a virtue.
    – Imad
    Commented Apr 22 at 17:02

0

You must log in to answer this question.

Browse other questions tagged .