Displaying code inside a post

In my posts I have put some code-snippets. I got some questions on how I did this. Well, to be honest, it is very easy.
I am using the <pre>-tag inside my post. Anything that is put between a <pre> and a </pre>-tag is handled by your browser as pre-formatted, and your browser leaves it as it is. You can add some styling to it inline, such as <pre style="font-size:90%;color:#009000;">, or you can add a class definition to your template skin.
If you want to display html-code inside your post, be aware that you have to replace every < by &lt;, and every > by &gt;.

This is what people say:

Shelly said...

I use Quick Escape to render code postable. I do it here:
http://www.accessify.com/tools-and-wizards/developer-tools/quick-escape/

Leon said...

I finally got a chance to try displaying code in a post. Thanks for reminding me how to do it. Thanks to Shelly too for the Quick Escape link.

Mohd. Nazmi @ Nicolas Ramos said...

thanks you so much for the tips hans.....really help so much.by the way,i have a problem with my blog about the underline link(blue).it's really stress me when i open my blog in firefox.how to remove the underline hans?but it look ok in my IE browser.

Hans said...

@nicolas: it appears that you have put a <u> and a </u> tag around the links inside your posts. It has nothing to do with the styling as such, that is okay. Edit your post, remove the underline, and it will be okay.

Anonymous said...

HI.I'M NEW LEARNING PROGRAMMING LANGUAGES I STARTET WITH HTML OF COURCE .
I TRIED THIS CODE BUT I DON'L FOLLOW "If you want to display html-code inside your post, be aware that you have to replace every < by <, and every > by >".
IF YOU PLEASE MORE DETAILS.
THANK YOU

Hans said...

@Anonymous: if you write html, you put tags in your document. <body> is a tag, for example.
In your html-document, the tag is not displayed; the tag tells your browser how to format the text that is in bewteen the two tags.
In our posts, where we are now and then giving examples of html-code, we want our tags to be shown. Therefore, if I use <h2> here in this comment, I don't want it to be interpreted by the browser as a header-tag, but just as plain text. To achieve this, in my texteditor I did not type "<h2>", but I typed "&lt;h2&gt;". I hope you understand it now. If not, use notepad, create a simple .html-document, and experiment to see the difference.

Anonymous said...

of cource,I am very happy for being able to understand the explanation And I'd Make the most beautiful site ever
Thank You Very Much And Happy Valentine Day

Jesper petersen said...

thanks you so much for the tips hans.....really help so much.
Digg / All News, Videos, &
Images

edundayo said...

Thanks for the info.

tanztj said...

thank for the tips.