Styling Code Snippets with CSS
Tags: Beginner
Although there are many useful plug-ins out there that dress up code snippets, I would like to share a technique playing with the background of the <pre> and <code> tag with CSS.
Common Problem with FireFox
When trying to add some padding to the <pre> tag, FireFox creates a cross-browser bug where it interferes with the spaces created by the space or tab bar. See below for an example.

Solution
We can go around this issue by nesting the <code> tag within the <pre> tag and specifying a margin in <code>.
HTML
Start out by placing your code snippet in between the <pre> and <code> tag. Note that anything inside of the <pre> tag is preserved (spaces and line breaks).
<pre><code>.classname {
/*--Code Goes Here--*/
/*--Code Goes Here--*/
/*--Code Goes Here--*/
/*--Code Goes Here--*/
}
</code></pre>
CSS
pre {
font-size: 12px;
padding: 0;
margin: 0;
background: #f0f0f0;
border-left: 1px solid #ccc;
border-bottom: 1px solid #ccc;
line-height: 20px; /*--Height of each line of code--*/
background: url(pre_code_bg.gif) repeat-y left top; /*--Background of lined paper--*/
width: 600px;
overflow: auto; /*--If the Code exceeds the width, a scrolling is available--*/
overflow-Y: hidden; /*--Hides vertical scroll created by IE--*/
}
pre code {
margin: 0 0 0 40px; /*--Left Margin--*/
padding: 18px 0;
display: block;
}
Make <pre> Tags Print Friendly
To make <pre> tags print friendly and make the code wrap to the next line, Tyler at www.longren.org shared a great technique to tackle this issue. If you are new to print style sheets, check out my previous tutorial.
print.css
pre {
overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not needed in Firefox 3 */
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */ /*
width: 99%; */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
Conclusion
If you haven’t dressed up your code snippets yet, now is a good time. Feel free to take the background images of my samples as well. If you have any questions or know of any other techniques, please share them!
Related Resources
- Simple jQuery Solution To A Simple Problem
- Syntax Highlighter (Free syntax highlighter written in Java Script)
- Syntax Highlighter WordPress Plugin
- Quick Highlighter
- Postable – Post-friendly code
Related Posts
Author Bio
My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!
You can learn more about me or
Follow me on twitter for more updates and resources!
Did You Enjoy This Post?
Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.




+ Add Comment21 Peeps Have Spoken Their Minds...
Hi,
you might solve the padding-problem with CSS-Resets like
* {
margin: 0;
padding: 0;
}
this little snippet resets all borders for every browsers… You might also use the Reset-Stylesheet by http://developer.yahoo.com/yui/reset/ this cleans up all styles and you need to style your tags excactly…
As always, yet another great little tutorial, and one that is definately useful.
Hey Christian,
That is what I first assumed as well and tried the universal * reset, but it didn’t do the trick unless I was doing something wrong ~
Also I’m not a big fan of Reset Style Sheets, I did give the one you posted a try but really twisted things up and didn’t fix the issue at all X-p
I like your little bug. He’s cool. Thanks for this dude. You know what would be great? I was looking at a few of your posts and saw that you had suggested some WordPress Syntax Highlighters along with some JavaScript ones as well. I’ve been hunting for something that has been updated in 2.7 but every time I check the popular Syntax Highlighters the plugin page shows they are only compatible up to a certain version, rarely if ever 2.7. Do you have any preference on what to use with 2.7?
ok! Haven´t worked with reset style sheets yet, but read the post at a german blog. I thought it might help :)
Dan: That bug image is actually stolen from the FireBug Icon hehe :-p
As for the plugin, Im not too sure since I haven’t given it a try. Maybe one of the readers can suggest one that is good :-)
Christian:
Hey no worries, thanks for the heads up either way :-)
This is great, when I create my blog I will be using this. Thank you!
Thanks for the tips! Just make sure you have enough padding on the bottom of your code snippet for the scroll bar (18px is the minimum). IE draws the scroll bar inside of the snippet, obscuring the last line. :-(
Great tut! Perfect time to use this for site.
Hey
what is the url to the image?
pre_code_bg.gif
i can’t find it :(
http://www.sohtanaka.com/web-design/examples/pre-tags/pre_code_bg.gif
http://www.sohtanaka.com/web-design/examples/pre-tags/pre_code_bg_ylw.gif
http://www.sohtanaka.com/web-design/examples/pre-tags/pre_code_bg_blk.gif
Nice article. Thank you for your effort. Will come in handy. Cheers.
I’m testin in Firefox 3.5 and IE8, ando i fixed this problem with:
pre {
word-wrap:break-word; /* ie8 */
white-space:pre-wrap; /* ff3 */
}
Thanks a million! I’ve used your css code for my blog. Neat and straightforward I would say. Thanks again.
Thanks for the information. The vertical scroll bars in IE have been driving me nuts! Now I can reduce the padding that I’ve had as a work-around.
Thanks a lot. Using it on my blog. reference made to this post :)
Hi when i paste that code in my html page for making a post it does not come out nice. all i see is the code and the .nameclass appears and it does not show that block and bg image? what did i do wrong?
Soh,
Thank you for the CSS tips and the clear explanation. I upgraded all of the C++ code samples on my website to use your CSS pre style code. They all look nicer and it was amazingly easy to do.
Curtis
nice thank…
This post was a HUGE help, thank you! I have been fighting with ugly “pre” for ever. Here’s what I ended up with…I didn’t care for the background image and went with plain gray: See it here. I might change to tan to match my theme…mulling it over.
Thanks for this :)
Used it in my blog :D
Speak Your Mind...