It would be great to have the option to fold code snippets, entire questions and answer posts on a user basis and allow defining some generic foldable regions within the question/answer editor.

Non generic folding info could be stored in cookies perhaps to move the storage/processing requirement to the client side.

Definable folding can take any free and safe HTML tag or other multi character trick (<>Fold this<>)

  • 1
    Usually I wouldn't call a bounty onto a feature request, but my question is getting old, and I'd like some response from the powers that be. As all answers are equally supporting, I'll award the bounty to that person, who can make it use the best.
    – akarnokd
    Commented Jul 20, 2009 at 12:51
  • 1
    I think this is kind of dupe for this one: Add markdown support for hidden-until-you-click text (aka spoilers)
    – fretje
    Commented Jul 20, 2009 at 13:02
  • 1
    I think mine is a broader topic.
    – akarnokd
    Commented Jul 20, 2009 at 13:14
  • I think the other one is broader, as that one is about a tag for hiding any part of your question/answer. Yours is only about hiding code, so narrower (is that the right word?) in my opinion.
    – fretje
    Commented Jul 20, 2009 at 14:43
  • 1
    Quote: "allow defining some generic foldable regions"
    – akarnokd
    Commented Jul 20, 2009 at 15:00
  • Okay, got it... not so clear when looking at the title though... and I still think they fall into the same bin...
    – fretje
    Commented Jul 20, 2009 at 15:10
  • You weren't there at the beginning I guess. Rich B's edit changed my original title: "Code, block, question and answer folding" - still visible in history
    – akarnokd
    Commented Jul 20, 2009 at 15:17
  • 1
    NB: I just realised that I posted a duplicate request (only on SO's meta). That's still under review: Collapsible Code Markup
    – JohnLBevan
    Commented Nov 24, 2016 at 18:48
  • 2
    There's now also the HTML5 details tag. See meta.stackexchange.com/a/303150/289870
    – serv-inc
    Commented Nov 12, 2017 at 21:24
  • It wasn't formulated as a feature request, but recently there was this on MSO: How can I add foldable code or text when answering someone’s question on Stack Overflow? Commented May 3, 2022 at 8:50

21 Answers 21


Yes, I like this idea a lot. Every so often I post an answer where the main bit is actually quite small, but a footnote has a lot of code baggage - it would be great if that baggage could be folded up to keep it in proportion with respect to the rest of the post.

  • Hmm, you imply the user should be able to define the default folding status on his region? ><Unfolded by default><, <>Folded by default<>?
    – akarnokd
    Commented Jul 14, 2009 at 19:21
  • Yes, quite possibly.
    – Jon Skeet
    Commented Jul 21, 2009 at 9:08
  • 37
    This is a great reason for folding. Why did the powers that own Stack Overflow reject this?
    – Vaccano
    Commented Jan 28, 2010 at 4:00
  • 5
    Is there a chance to reconsider this? Please see my question as well: meta.stackexchange.com/q/157747/183189
    – MarioDS
    Commented Apr 26, 2013 at 18:36
  • 4
    GitHub provided <details> for this. For StackExchange, it could accept an attribute: <details open>. I think this feature could be useful when asking a question, to post the detailed specs of your environment, just in case there is a relevant factor in there. Commented Oct 26, 2020 at 5:17

This feature request (and it being declined) is almost 6 years old. I'd appreciate if this could be reconsidered. There are a lot of use cases that have been mentioned but I don't even think this should be focused on code blocks. Since code blocks get a scrollbar anyway you can already fill your 30k character limit with a single code block, without taking up a lot of vertical space. So putting in massive amounts of code isn't really discouraged by the system to begin with.

However, I think collapsible regions would be much more useful for normal explanations. In many cases there is the simple answer which will serve most people's needs, and then there's also the in-depth "if you really want to know the details" explanation. In those cases, I would prefer not to take up so much screen real estate for the people who are not interested in the full explanation, but I'd also prefer not to link to the gritty details externally.

You might say "a good answer is tailored to its audience, just pick the short or the long version appropriately" - but I'm not writing the answer only for the OP but also for future visitors, so catering to both cases would certainly improve the usefulness of the answer.

I also don't understand why some people are so strongly opposed to this: collapsible regions are already a thing on SE. On one hand, Stack Snippets can be hidden, but more importantly spoilers are implemented on mobile as collapsible regions. If they're such a bad thing, why have them on one platform in the first place? Couldn't we just turn spoilers into collapsible regions everywhere? (Although a comment-based syntax like Stack Snippets would be more convenient than prepending >! to every single line.)

  • 4
    Another use: In linguistic stackexchange sites some text may be transcribed and/or translated differently more than once. A foldable block of text would help to keep the answer short as you can fold a version of the text.
    – Seninha
    Commented Jul 31, 2017 at 22:27
  • 1
    I actually would find this much more useful on sites like psychology.SE where giving just a citation puts the verifiable stuff beyond a paywall for non-experts, while giving a quote makes the answer fairly unwieldy often enough. There's a reason why social sciences books have adopted a style a of footnotes for details/citations. Some approximate this on psy.SE using a small font, but I find that solution pretty irritating. Commented Jan 8, 2018 at 7:04
  • More details on spoilers: meta.stackexchange.com/questions/1191/…
    – Vadzim
    Commented May 4, 2018 at 10:53
  • Especially now that we have the HTML5 "<details> & <summary>" tags. Commented Mar 19, 2022 at 2:21

I like the idea, especially because it would make it easier to make a question 'multi lingual' where you could post a question or answer with multiple code samples in multiple languages without making it a nightmare to read.

  • 13
    Like how MSDN does their code samples in C#, VB, and C++.
    – TheTXI
    Commented Jul 14, 2009 at 14:42
  • 1
    @TheTXI: Exactly.
    Commented Jul 14, 2009 at 15:15
  • 2
    For language agnostic questions, nice.
    – akarnokd
    Commented Jul 14, 2009 at 19:23

This is a hugely helpful idea for the [R] tag and I suspect any other statistics/data-heavy tags as well.

We strongly recommend our users include sample data:

How to make a great R reproducible example?

Often due to the nature of the statistical beast even carefully culled sample data is unwieldy. For instance this well-conceived post:

Dealing with Messy Dates

There's simply no way to include the variety of examples the questioner was interested in without a huge code dump. I tend to put mine at the end, but it's still a sub-optimal solution that makes answering harder and the site less beautiful.

  • 2
    I agree with the idea, but that dump of dates is breathtaking. Not all train wrecks should be hidden, and that is a very memorable train wreck. Still, you're right. Some people seem to post their stuff to pastebin or other sites, in order to have the effect of code folding.
    – Iterator
    Commented Nov 14, 2011 at 13:11

Personally, I like the idea. I've been guilty of long-freakin'-code-posting myself. However, I somehow doubt Jeff would agree. :)

  • 10
    Jeff can opt-out of such feature and he won't experience it at all.
    – akarnokd
    Commented Jul 14, 2009 at 19:12
  • 17
    Jeff's arguments are quite valid in the context of a software development environment. StackOverflow, however, is not a software development environment, not an IDE. It is a place to talk about problems with source code. Sometimes you need to share the code so that someone can run it -- they need not examine the entire snippet. There are text pastebins that exist, and can be linked to from within a question, however it makes more sense to fold the code and keep the code bundled with the question on StackOverflow. This would then allow for personalized fold/unfold settings. Commented Dec 8, 2010 at 15:38
  • 2
    Jeff was probably sick of getting blamed for his code :v
    – kekekela
    Commented Nov 14, 2011 at 1:38
  • Which bit of that article are you referencing? I'd rather not read more than I have to of things written by Jeff. :-/ Commented Mar 10, 2015 at 22:00
  • 1
    Oh wait, it's a broken link. Here's an Internet Archive Wayback Machine capture of the article titled “The Problem With Code Folding”, dated 2008-07-06: web.archive.org/web/20100120154520/https://codinghorror.com/… You see, that's the problem with external links, especially in the case of pastebins which may be set to expire. Commented Mar 10, 2015 at 22:46
  • (I was initially just seeing the unrelated articles at blog.codinghorror.com because that's what the broken link silently 301-redirected to without me noticing, hence why it wasn't obvious as to what you were referring.) Commented Mar 10, 2015 at 22:59
  • Updated link with wayback link provided by @James Haigh. Thank you.
    – John Rudy
    Commented Mar 16, 2015 at 15:48
  • Jeff actually declined this feature request.
    – Matt
    Commented Jul 12, 2018 at 16:15

The <details> tag does exactly this

One main criticism for this feature request was that a new syntax had to be defined. This is provided by the HTML5 standard with the <details> and <summary> tags. Example

  <summary>Some details</summary>
  <p>More info about the details.</p>

Shows Some details with an arrow to click to show more. Folded vs unfolded:

folded enter image description here

@Jeff said that he wants to avoid too much code. That is already enforced by the hard limit, and people who know about HTML-styling Stackexchange are not very likely to post unnecessary code.

  • 7
    Does it work when editing posts on SO? Commented Jan 2, 2020 at 13:01
  • 1
    AFAIK no, but it surely should.
    – Nuke
    Commented May 3, 2022 at 3:52

You mean a collapsed code-snippet like this?

alert('Most folded code ever!');

  • 2
    Exactly. But does this work with all languages, or even pseudo-code? Commented Dec 18, 2015 at 16:19
  • 9
    @ScottBeeson infuriatingly not. This issue feels like the US-campaign-finance reform of Stackoverflow; 80% or so seem to want it, but those with decision-making power prefer to keep doing other things. Commented Dec 18, 2015 at 18:58
  • @SeldomNeedy - YES. How do I do that, to collapse code snippet in SO? I would appreciate your help.Do I have to add some code, it's not a built in feature in Stackoverflow?
    – DanielaB67
    Commented Jul 28, 2017 at 14:19
  • 1
    @DanielaB67 Even if you're logged out, you should see an option to "Edit" or "Improve this answer". Select that to reveal the markup used. Commented Jul 28, 2017 at 22:55
  • @SeldomNeedy Yes I saw it, thanks! Meanwhile I've got an answer here: meta.stackoverflow.com/a/354002/3362989 I asked the question in SO and used the link to your sample, so moderators returned the question to stackexchange :) Thanks again!
    – DanielaB67
    Commented Jul 29, 2017 at 0:17
  • @ColmBhandal TIAS? Commented Jun 12, 2020 at 17:43
  • does this not work on stack overflow itself? what could i be doing wrong?
    – Dave Ankin
    Commented Jul 5, 2022 at 14:36

This would be very nice - especially if it's closed (or showing only the first line of code) by default.

  • First line is nice and you can just have a // short description in it.
    – akarnokd
    Commented Jul 14, 2009 at 19:22

I like the idea of automagically inserting folding on (at least) code. It would help with the newbies who dump their entire architecture into a question -- which I still prefer over those who provide no code at all. Anything over, say, 10 lines? What's the cutoff for when it adds a scroll bar? Because that's probably about the time we should definitely have a fold.

  • I don't see any trouble having a highly customizable folding settings. jQuery is a powerful library.
    – akarnokd
    Commented Jul 14, 2009 at 19:19

Here is my solution to this problem - a greasemonkey script which will hide all but the first line of each code block until it is clicked on (click again to re-fold the block). It is a bit hackish atm, but it does work on my computer. If you find any bugs in it, let me know and I will try to fix them.

EDIT: Added menu commands to show and hide all foldable blocks.

// ==UserScript==
// @name           Code folding
// @namespace      http://stackoverflow.com/
// @include        http://stackoverflow.com/questions/*
// @include        http://meta.stackexchange.com/questions/*
// ==/UserScript==

//default values

// Add a selector for each element that you wish to fold
// to the array below (one array item per selector
var elementsToFold = new Array("pre:has(code)");

// Close elements by default?
var closeByDefault = true;

//- Do not edit below this line -
function getJQuery() {
    if (typeof unsafeWindow.jQuery == 'undefined') {
        window.setTimeOut( getJQuery, 100 );
    else {
        $ = unsafeWindow.jQuery; letsGo();
//add the folded style into the document
var style = window.document.createElement("style");
style.type = "text/css";
style.innerHTML = ".folded {height:1.0em; overflow:hidden;cursor:N-resize;}";


function letsGo () {
    $(elementsToFold.join(",")).click( function() {
    window.showHideElements = function (event) {
    window.hideAllElements = function () {
        $(elementsToFold.join(",")).each(function() {
    window.showAllElements = function () {
        $(elementsToFold.join(",")).each(function() {

    if (closeByDefault) {
        $(elementsToFold.join(",")).each(function() {

    GM_registerMenuCommand("Show all folded blocks", showAllElements);
    GM_registerMenuCommand("Hide all foldable blocks", hideAllElements);
  • Can I make this somehow work in Chrome?
    – akarnokd
    Commented Jul 22, 2009 at 6:40
  • 1
    Apparently Chrome has experimental support for greasemonkey scripts - start with chrome --enable-greasemonkey. The other option is to try greasemetal.31tools.com, which apparently supports most grease-monkey scripts. However, I don't use chrome, so I can't be sure. If you have more information, let me know and I will update my answer accordingly.
    – a_m0d
    Commented Jul 22, 2009 at 7:00

I’d like to provide (but one) example of a valid reason for providing collapsible elements: log files. Sometimes you must provide logs for analysis, and often they tend to be lengthy and cannot be pared down without losing potentially valuable information. Yet, there is no reason to have them be full-length by default. For example, someone may be able to diagnose an issue without even looking at it while someone may need some clue from the log to figure out the problem.


It would also be useful to support collapsed sections of text or images. Example usages:

  • On one of the mathematical Stack Exchange sites, I might write an answer which contains a long but relatively uninteresting proof of one of the claims. Since the proof is needed for completeness but may be a detour from the main point, I want it to be collapsed until an interested user clicks.
  • For a question on a graphical computer program, I might include a screenshot illustrating what I want to do. I might have some additional screenshots illustrating more examples; these would probably only be interesting to someone that is trying to answer my question.
  • Details on multiple options. For example, a User Experience question might ask:

Which position for some control would allow the fastest navigation by users?

  • Controls on the left
    (Collapsed: UI mockup with controls on the left)
  • Controls on top
    (Collapsed: UI mockup with controls on top)
  • Controls on bottom
    (Collapsed: UI mockup with controls on bottom)

They could rig up something like this, they already use these interface elements elsewhere in the site:

Code Block Dropdown

So of course, hitting the arrow would bring up the full code block. I'd actually do it somewhat differently in order to not lose width on the code block, but something along these lines would be helpful.


How about now?

With the advent of live examples, it would be nice to also keep the code onsite on the off-chance the live example site goes offline or loses the contents, but also nice to keep the code collapsed (as it's already readily available, and live examples tend to accommodate more code).

Pretty please?


This is a good idea, especially for code. I sometimes find myself putting a sizable piece of sample code into an answer, which some people may just not want to read through. It would be nice if I could fold it for people to skip over if they like.

Incidentally, this would also help with "code golf" or challenge questions where solutions or hints included with the question could be folded so that people don't have to see them if they don't yet want to.

  • It depends on what you consider the default state of foldable objects.
    – akarnokd
    Commented Jul 14, 2009 at 19:17
  • There now is a thing called “spoilers” but they still take up all of the space of their contents.
    – Matt
    Commented Jul 12, 2018 at 2:32

Great idea. I'd suggest supporting it for quote blocks as well.

  • That's implicit if you wrap your quoted text in a foldable region.
    – akarnokd
    Commented Jul 14, 2009 at 19:13

I'd like to see the code block small at first, with an option to expand beyond the bounds of the question (to solve the not-too-uncommon problem of horizontal scrolling).


I'd like to bring this topic back again, because I really feel the need for a collapsible area. In some cases (I'm primarily working with R) I need to give some more input for others to be able to help me/solve the problem. Like multiple files of codes. It would be really nice to have them each in a collapsible area so others can easily focus on checking one file after the other. This can be a huge improvement in the readability of a question or answer.


I think one of the real issues here is a lack of control over how the feature could be used. Users unfamiliar with SE could do all sorts of thing with a feature like this, potentially creating time consuming issues for users that are moderating.

In addition to that, it is well known that Google prefers that you hide as little content as possible. It would be necessary to inform users of this SEO practice, before one could reasonably ask them to use it correctly.

I personally like the idea, but there are significant practical issues worth considering.


For HTML/ CSS/ JS/ Any JS Libraries (React, Vue ...)

1 Select Javascript/ HTML/ CSS snippet.

2 Enter the Code.

3 Make sure Show console is unchecked if the code is not runnable.

4 Choose Hide Snippet by Default

5 Make other configurations as needed.

6 Save & Insert into post.


// Add your code here
const message = "Hi there";

Add a note mentioning that the below code is not runnable if it is not and point to platforms where the code can be executed if applicable. Check this answer for reference.

enter image description here enter image description here


Line numbers and code folding don't belong here, because you shouldn't be posting huge swaths of code anyway. If it's more than a few snippets, the question and/or the answers are busted.

If you "need" to link to a lot of code, host it elsewhere, and give us the relevant snippet with a link to the rest.

Any features that encourage posting a lot of code are also broken by design and will be declined for the same reason -- why would we want to encourage behaviors that lead to unreadable posts?

  • 1
    I could argue, but I simply accept your view.
    – akarnokd
    Commented Jul 21, 2009 at 8:55
  • 38
    It's quite possible to have long answers that include several sections, some of which are almost incidental. Being able to fold those sections up would make the answer easier to navigate. There are more reasons for wanting folding than just "there's one huge block of code."
    – Jon Skeet
    Commented Jul 21, 2009 at 9:09
  • 5
    half the time I'd rather fold up the text and just look at the code. Commented Jul 21, 2009 at 14:33
  • 33
    @Jeff: Please think about it from a futuristic point of view. Hosting the code somewhere else and linking to it means that when that site goes down, we end up with a stale link and probably a stale answer here. This kind of splitting up of the answer into two sites means two points of failure. And "huge swaths of code" are unavoidable in some cases to provide even simple and legitimate examples - mostly due to verbose languages, but also due to other reasons.
    – Sundar R
    Commented Jul 22, 2009 at 7:00
  • 3
    I also disagree with you Jeff....I don't see any reason why some legitimate questions wouldn't benefit from, or actually require, large amounts of supporting code. Let's say someone is asking a question about how to handle a fairly in depth requirement....now, if I've done something similar, I may be willing to post entire code files as it takes just a few minutes, but, to work through it all and extract only the relevant bits and pieces and post them, and hope I haven't missed anything crucial, well, I may not bother. Hopefully you reconsider this (folding AND uploading).
    – tbone
    Commented Aug 21, 2009 at 20:31
  • 2
    I would like to point out some users are downvote and vote-to-close-as-spam happy if you link to pastebins for large amounts of code.
    – Macha
    Commented Oct 17, 2009 at 13:08
  • 2
    @Jeff; you need to define "huge swaths". I have had several questions which need code which is not really relevant simply because if I don't post it I know that the first comments gonna be "show us the code". Commented Dec 23, 2010 at 0:04
  • 2
    @Jeff: Here is an example of a good answer which would be better if the code were folded by default: stackoverflow.com/questions/5038169/… Commented Feb 20, 2011 at 21:37
  • 3
    @Jeff: Also, you need to reconsider your decision to decline, I think, based on the vote pattern in this question, assuming that the votes represent the community. Commented Feb 20, 2011 at 21:38
  • 2
    @Jeff - You may be interested in this question, which was created when someone posted an answer about how to link a DLL in several ways here. My answer was "Scrollbars appear at 25 lines, and you get 30k characters - Use them!" Did you have the scrollbars at the time this question was asked? If not, I think it might be good to revisit it. Commented Nov 14, 2011 at 1:41
  • 1
    @Jeff In practice I've seen that others follow this suggestion, by way of pastebin.
    – Iterator
    Commented Nov 14, 2011 at 13:16
  • 7
    @Jeff, just because you can’t think of a valid reason for it doesn’t mean there isn’t. Consider log files. Sometimes you must provide logs for analysis, and often they tend to be lengthy and cannot be pared down without losing potentially valuable information. Yet, there is no reason to have them be full-length by default. For example, someone may be able to diagnose an issue without even looking at it while others may need some clue from the log to figure out the problem.
    – Synetech
    Commented Apr 21, 2014 at 17:00
  • The 'About' page says, stackoverflow is just quesions, and answers, no chitchat. But the reality often is: question, text wall of context, comments, 1st answer, text wall of context, 2nd answer. Also this does not just concern code blocks, but any extra/background/context information anyone thinks meaningful. In particular in meta-* discussions, the question part necessarily becomes an article stating pros / cons / related works, alternatives, etc.
    – tkruse
    Commented Dec 3, 2017 at 3:39
  • meta.stackexchange.com/a/230135/393735
    – Matt
    Commented Jul 16, 2018 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .