›Displaying code and code blocks
Sep 30 2006
One of the things that I found attractive about missunderstood was how Will handled code block display, using a background image that helped the code block stand out distinctly as code/script, with pseudo line numbering. Well, here’s an example:
#content .code {
color:#444;
background:#F4f4f4 url(images/codenums.png) repeat-y;
border:1px solid #ccc;
padding:1px;
padding-left:25px;
font: 12px/18px "courier new", helvetica, "times new roman", monospace;
width:470px;
margin:2px auto;
white-space:nowrap;
overflow:auto;}
I had to add 1 pixel to the top and bottom of the codenums.png image to make the numbers line-up (a breeze with IrfanView, didn’t bother launching PhotoShop ;’) and use a fixed font size/style. Note that long lines are not wrapped, but scroll off to the right (should help avoid copy/paste issues).
This method currently requires “Use Visual Rich Editing” to be turned off (under Users:Your Profile). Here is how to put code blocks into your post: Start with a <div class=”code”> (will call the class style properties, beackground, etc.) immediately followed by a <pre> (’pre’-formatted, to maintain original structure) so the beginning looks like this:
<div class=”code”> <pre>
Enter your code right after that:
<div class=”code”> <pre>
if $girlfriend(’hair’) == ‘blonde’ then {
dispose($sharpobjects);
} else {
exit;
}
and then close the div and pre:
<div class=”code”> <pre>
if $girlfriend(’hair’) == ‘blonde’ then {
dispose($sharpobjects);
} else {
exit;
}
</pre> </div>
And there ya go…