» CSS » Tips for Styling Code in your Posts

Chris CoyierTips for Styling Code in your Posts

Written by Chris Coyier from CSS-Tricks on January 18, 2008

If you’ve ever included snippets of code on your blog, you know that presenting that text can be a unique challenge. HTML provides a tag for this purpose, the <code> tag. Unfortunately, just chucking your code in those tags isn’t the end-all solution. For one thing, you typically need to turn off your Rich Text editor and hand-write them in if you want to get those tags at all. For another, if you are including actual HTML code in your sample, those tags will still render as HMTL, not text.

What does the code tag actually do?

Not a whole heck of a lot, actually. By default, it’s an inline element. Meaning you can use it just like <em> tags or spans or whatnot. Also by default, it changes the font-family to a mono-spaced font like Courier or Monaco. For starters, you may want to consider changing it to a block-level element (display: block;) and adding some padding/margin/background/stroke to it’s CSS to emphasize the code to your readers. Sticking with the mono-spaced font is a good idea though. Something very “codeish” about that.

Like I mentioned, if you are including HTML code in your code blocks, you will need to replace your < characters with &lt; and your > characters with &gt;. This will stop your browser from seeing those as actual tags and it will render as regular text. If you have large amounts of code to do this to. You may want to consider a GREP solution (advanced Find/Replace).

find-replace.png

Now you have your code all looking nice on the page, but one thing that can still remain a problem is the spacing. Good code is usually full of indents to visually break up sections. As you likely know, multiple concurrent spaces will not render in HTML. You can always force spaces with &nbsp; or other similar spacing codes, but there is a much simpler solution.

Using in conjunction with the pre tag

The <pre> tag (short for pre-formatted) is essentially for preserving spacing in text blocks. Wrap your code block in pre tags and apply spacing as normal to your code and it will display properly. Multiple spaces and tabs will be rendered as such.

Automating the process

Since we are dealing with blogs here, thankfully there are some plugins to make displaying code blocks in your post easier. Code Markup is a plugin for WordPress that allows your HTML markup within code tags to be displayed without having to replace all the <’s and >’s. It also has the nice feature of allowing some html tags to be rendered and some not to be, if you choose.

If anyone knows of other plugins that help with this, I’d love to hear about them.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Bumpzee
  • E-mail this story to a friend!
  • Ma.gnolia
  • Print this article!
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Chris Coyier from CSS-Tricks on January 18, 2008 | Filed Under CSS
Unique Blog Designs

5 Responses so far | Have Your Say!

  1. Ronald Huereca  |  January 18th, 2008 at 6:39 pm #

    Ronald Huereca - Gravatar

    Postable is also a nice tool for inserting code into posts/comments: http://www.elliotswan.com/postable/

  2. Daniel  |  January 19th, 2008 at 3:11 am #

    Daniel - Gravatar

    awesome info.great to learn something. although using the HTML codes wud show the syntax rather than create a link.

  3. Hopeful Spirit  |  January 19th, 2008 at 7:05 am #

    Hopeful Spirit - Gravatar

    Hello!

    Have you heard about The Seventh Day weekly blog carnival at On the Horizon?

    You can submit a post on ANY topic! (Keep it clean, ok?) It’s a great way to reach a new audience for your site! The Carnival is also an excellent resource for discovering new sites and making new blogging friends.

    The Carnival takes place every Seventh Day (Sunday) and posts may be submitted through Saturday at noon for the next day’s edition.

    I invite you to submit your best work (1 post per blog per week) from the prior week on ANY topic using the submission form!

    Want to help spread the word? You can put the Blog Carnival widget or The Seventh Day button on your site! (Email me for the graphic file.)

    Questions? Drop me an e-mail: admin@hopefulspirit.com. Or use the Contact Form at On the Horizon !

    Thanks and hope to see your post included on The Seventh Day!

  4. vhxn  |  January 19th, 2008 at 11:06 am #

    vhxn - Gravatar

    Great info and a nice tip too.

  5. Chris Coyier (Post Author)   |  January 19th, 2008 at 11:36 am #

    Chris Coyier - Gravatar

    Postable is sweet, thanks for pointing that out Ronald.

Trackbacks to 'Tips for Styling Code in your Posts'

Leave Feedback

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>