Click here to Skip to main content

Using PRE tags on Code Project

How to use PRE tags to preserve formatting, and improve readability of code snippets or tabular data in posts.
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment
When publishing code or some text that needs a strict syntax (in an article, a forum message, a tip, whatever) ALWAYS use the appropriate tags, which is either CODE tags (for a small snippet, no more than one line) or PRE tags (for all multi-line snippets).
The overall advantage is better readability, and as a result reaching a broader audience and getting more and better feedback. In detail:
  • CODE and PRE tags result in a non-proportional font, which preserves indentation;
  • PRE tags give a nice background color (pink-ish by default), very good for contrast; BTW: CODE tags don't, they leave the background white (not so good for color contrast) or turn it blue-ish on forum messages (horrible for contrast), so I don't like them being used in conjunction with syntax coloring at all.
  • CODE and PRE tags perform syntax coloring, assuming you add a LANG="some language code" to the opening tag; unfortunately without the change in background, such coloring makes CODE completely unreadable; so PLEASE set LANG=NONE for every snippet in CODE tags. For PRE tags too, the default is LANG='CPP' even in the C# and VB forums; so you really should add LANG='CS' or LANG='VB' to get the keywords recognized correctly (use single or double quotes, and upper- or lower-case).
 
As this is simple and very effective, I can tell you I simply no longer read code that is not tagged appropriately; if the author doesn't care, neither do I.
 
Here is an example (with the PRE tags shown explicitly):
<pre lang='cs'>    / * the opening PRE tag (normally not visible!) */
 
private static int multiply(int arg1, int arg2) {
    return arg1*arg2;
}
 
</pre>             / * the closing PRE tag (normally not visible!) */
Warning: If you are using a multi-mode editor, you must be in "raw mode" (click the <> ) to enter and edit HTML tags such as PRE and CODE.
 
And here is a list of language codes, it may be incomplete and there might be small errors, it is a dynamic thing:
text 	plain text (= no syntax coloring)
cpp 	C++
cs 	C#
vb 	VB, VB.NET, VBscript
java    Java, JavaScript
SQL 	SQL
midl 	Microsoft IDL
asm 	Assembly
msil 	MSIL
xml 	XML
html 	html
css 	CSS
 

Warning

It may come as a surprise however CodeProject does not take the content of PRE blocks literally, a number of HTML tags are accepted, others ignored and removed. Here is a non-exhaustive list of the ones that seem to work:
<b>text</b>   generates bold text (which can not be discerned well at all, so I don't recommend it)
<i>text</i>   italicizes the text
<u>text</u>   underlines the text
 
A huge consequence is you have to HTML-escape the special characters < and > and & i.e. you must replace them like so:
<   &lt;
>   &gt;
&   &amp;
 
This escaping can be performed manually (when in "raw view") or by cheking the "Encode < symbol while pasting" checkbox (or something similar) in the forums. Pasting code in Q&A normally does it automatically, at the same time it adds PRE tags. When the < > & symbols aren't escaped correctly, parts of the snippet may vanish, and what remains may be shown in unintended colors. When done properly, it all should look like < > & in "HTML view" or in preview.
 
Inner warning: Depending on the browser used, it may seem sufficient to escape just <, however some browsers (and I suspect some CodeProject code too) don't work well when the others are left unescaped.
 

Additional Features

  1. You can locally change foreground or background color by using
    <span class='emphasis'> or <span class='highlight'>
  2. You can change the entire background color by using <pre style='background-color:#DDFFDD'> or some other hex RGB value.
  3. You can get the lines numbered automatically, by adding linecount='on'
Here is an example using most of the above:
<pre linecount="on" lang="cs" style='background-color:#DDFFDD'>
private int <b>multiply</b>(int arg1, int arg2) {
    return <span class='emphasis'>arg1*arg2</span>;
}</pre>
which yields:
  1  private int multiply(int arg1, int arg2) {
  2      return arg1*arg2;
  3  }
 

Final Words

  1. Please use PRE tags for multi-line code snippets, as well as for any tabular data you want to represent.
  2. Don't use CODE tags in conjunction with PRE tags. There is no need for them, the PRE tags handle it all.
  3. Don't forget to HTML-escape each and every < > & inside a PRE block (unless you actually want an executable HTML tag of course).
 
Smile | :)
Posted 3 Sep '09
Edited 16 Mar '11

Comments
Simon_Whale - 28 Jun '10
Reason for my vote of 5 good to see some, well constructed "how to use, the site" instructions / tips
Tarun.K.S - 13 Jan '11
Reason for my vote of 5 Now it's more clear! Thanks for sharing!
linuxjr - 13 Jan '11
Reason for my vote of 5 Thanks for sharing.
Dalek Dave - 13 Jan '11
Luc, Brilliant pal. 5ing you!
Walt Fair, Jr. - 13 Jan '11
Thanks, Luc.
Jyothikarthk - 17 Jan '11
Thanks for sharing. Absolutely nice.
thatraja - 5 Mar '11
Reason for my vote of 5 Excellent buddy, Thanks.
AspDotNetDev - 14 Mar '11
The syntax highlighter may have changed since you posted this. Seems your sample of bold/italicize/underline isn't working for the underline. Also, you say "Encode &lt; symbol while pasting"... seems you encode that less than a little too much. Should say "Encode < symbol while pasting" (actually, the forums say "Encode "<" (and other HTML) characters when pasting", and the QA section says "encode HTML characters", but the latter changes so often who knows what it will say next week).
Luc Pattyn - 14 Mar '11
I fixed the < mistake, thanks. And no, I'm no longer trying to quote the exact text as it changes all the time. :)
Eduard Keilholz - 18 Mar '11
Reason for my vote of 5 Excellent!
BarrRobot - 21 Mar '11
Reason for my vote of 5 A very useful article.
jim lahey - 12 Apr '11
Reason for my vote of 5 superb. we still need an article on "how to use a search engine before posting on CP"
Luc Pattyn - 12 Apr '11
Thanks. Searching a bit you could stumble on this one :)
jim lahey - 13 Apr '11
good stuff! I thought in hindsight that I was being a little harsh. looking at this morning's questions saw to that.
Ali Fakoor - 2 Oct '11
Reason for my vote of 5 good information about code formatting
Mihai MOGA - 7 Oct '11
Reason for my vote of 5 Great article. Please keep it up!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. SAKryukov (1,105)
  2. OriginalGriff (1,090)
  3. Abhinav S (475)
  4. thatraja (455)
  1. SAKryukov (9,074)
  2. Christian Graus (5,846)
  3. OriginalGriff (4,523)
  4. Abhinav S (4,310)
  5. thatraja (4,230)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralList of language attribute values PinsubeditorIndivara15:51 13 Jan '11  
GeneralThis is the kinf of stuff that is cool! PinmvpDalek Dave13:21 13 Jan '11  
GeneralRe: This is the kinf of stuff that is cool! PinmvpLuc Pattyn13:28 13 Jan '11  
Questionlinecount attr. not working ? PinmemberXmen W.K.8:05 10 Jun '10  
AnswerRe: linecount attr. not working ? PinmvpLuc Pattyn8:26 10 Jun '10  
GeneralMake XML-style quotation marks PinmemberSAKryukov7:00 14 Jan '10  
GeneralRe: Make XML-style quotation marks PinmvpLuc Pattyn8:57 14 Jan '10  
GeneralSmiley bug PinmvpLuc Pattyn13:45 3 Sep '09  
GeneralRe: Smiley bug Pinmemberaspdotnetdev17:17 22 Nov '09  
GeneralRe: Smiley bug PinmvpLuc Pattyn0:53 23 Nov '09  
GeneralEdited it - works fine PinmvpDaveyM6913:26 3 Sep '09  
GeneralRe: Edited it - works fine PinmvpLuc Pattyn13:43 3 Sep '09  
GeneralPre tags worked for me PinmvpDaveyM6913:22 3 Sep '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 17 Nov 2011
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid