What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 150 Stack Exchange communities.

I frequently make answers with code samples that are surrounded with pre tags instead of having each line indented with an extra four spaces, because doing the latter either means I have to add four spaces each line, or go into gedit, change the tab settings to four-spaced tabs, then type away, then indent. I'm a little too lazy to do that sometimes.

My question is, as most of the time when I do this, someone quickly swoops in and changes it to four-spaced indents to invoke code mode. Why is this? Is there a benefit to that method? Is there a rule against pre tags?

share|improve this question

4 Answers 4

up vote 7 down vote accepted

Well, you can just select the lines you want and press ctrl+k (or click the code-sample button) and it'll do the indent for you...

IIRC there are some things that the <pre> doesn't like, but I can't think of them off the top of my head. Indented is generally easier to work with, IMO.

share|improve this answer
+1 -- in additon, <pre> content doesn't get code highlighting, which is probably the reason why people prefere four-space-indent – balpha Sep 15 '10 at 10:29
+1 for Ctr-k. Didn't know about that. I've been indenting in my editor. – Peter Ajtai Sep 15 '10 at 11:46
Thanks! A great tip there, and now I know that the indented code block has syntax highlighting and automatically escapes special HTML characters. – Delan Azabani Sep 15 '10 at 12:00

I use the 4 white spaces for code blocks (and the CTRL+K shortcut) when I want syntax highlighting.

I use <pre> when I want pre-formatted text without syntax highlighting (e.g. for console output).

I sometimes use <pre><code> when I want pre-formatted text for code with syntax highlighting and more advanced formatting options, like bold and italic, for special emphasis:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("you can't do that with 4 spaces");
share|improve this answer
Very nice tip, thanks! – Delan Azabani Sep 15 '10 at 12:25
Note that it appears that <!-- language: lang-none --> is apparently now the preferred mechanism for preformatted text without syntax highlighting. Note also that syntax highlighting on <pre><code> no longer works automatically when a language is not detected. – Phrogz Apr 1 '11 at 20:31

Well, if my code actually involves HTML/XML, then this will most certainly not work.

My code!
Hello world!



<div>Hello world!</div>

Of course you would could to rewrite all that angular brackets diligently as &gt; and &lt;, but that's just a waste of time...

share|improve this answer

Semantically, <pre> is for pre-formatted text, not code: it's to make sure whitespace is preserved and little else.

As others have pointed out, syntax highlighting is only invoked on code blocks, so consider it a gift that others are taking the time to fix your mistake and properly enclose the code with the correct markup.

share|improve this answer

You must log in to answer this question.

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