The problem with a lot of ‘how to show code’ tutorials on the Internet is that they teach you completely the wrong things! I’ve seen countless tutorials telling people to wrap the code in a
<textarea> or just leave spaces between the <, > and the tag name — ALL wrong. And no, not just wrong because it’s my personal preference, but semantics wrong and standards wrong.
The key to displaying code properly is to replace the HTML entities like < with their name/number equivalent. The most common entities and their replacements are as follows:
|Result||Entity Name||Entity Number|
|‘||' (except IE)||'|
Semantically speaking, the code with it’s new entities should be wrapped in the
<code> </code> tags which can be customised like any other tag with CSS.
<textarea>s look neat & tidy?
So? That’s no excuse. Why not just alter the way the
<code> </code> tags look, to imitate the textarea but without having to misuse the tag? Add a class (remember: classes can be used multiple times) to the
<code> tag that you want to appear in a box, and add the following to your CSS:
border: 1px solid #7F9DB9;
Then, simply wrap your code areas up like so:
<code class="class-name"> <tag> </code>
Neat, tidy code, and no tag-abuse.