30

How do you put a whole codeblock into spoiler text?

This is normal text

This is spoiler text

This is one line of visible code

This is one line of invisible code

def visible_function():
  print 'hello world'

But to get a multi-line code block that is invisible?

def invisible_function():
  print 'Eeek! I feel so naked!'
1

2 Answers 2

30

You can do it using HTML

>! <pre><code>def invisible_function():
>!   print 'Eeek! I feel so naked!'</code></pre>

This results in some extra padding, though.

def invisible_function():
   print 'Eeek! I feel so naked!'

7
  • 3
    I tried your approach on SO but it didn't work:-(
    – Christoph
    Commented Mar 15, 2012 at 16:59
  • Doesn't work on math.SX, either.
    – JeffE
    Commented Apr 30, 2012 at 12:42
  • @Christoph: It looks like the CSS for spoilers conflicts with the syntax highlighting (which is disabled here).
    – hammar
    Commented Apr 30, 2012 at 12:59
  • 3
    @Christoph, there's a bug report for that: Code samples inside of spoilers are still visible on some sites.
    – Arjan
    Commented Feb 2, 2013 at 19:03
  • This does not work if you use <> in the code, e.g. <pre><code>#include <stdio.h></pre></code> or <pre><code><i>text</i></code></pre>
    – user12205
    Commented May 21, 2015 at 13:15
  • 3
    @ace: You can use &lt; and &gt; to escape them.
    – hammar
    Commented May 21, 2015 at 15:32
  • this worked for me in Anime.Stackexchange here anime.stackexchange.com/questions/27603/…
    – Kym NT
    Commented Feb 10, 2016 at 8:59
9

This may not be the right way to do this. I was just trying different combinations with the syntax and came up with one possible way to include multiline code within the spoiler section using the HTML <pre> tag.

By default, the text is not shown as per spoiler behavior.

text hidden

Multiline code displayed onmouseover.

multiline code

Example

SELECT  *
FROM  dbo.Spoiler
WHERE NumberOfLines > 1

You must log in to answer this question.

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