How to use CSS to style code snippets

Thursday, November 19th, 2009
Advertisement

Subscribe.
Enter your email:

It is easy to use CSS to style code snippets, as shown in Figure 1.

Figure 1: Sample output of CSS style for code snippets

Figure 1: Sample output of CSS style for code snippets

Step 1: Append this code inside a style sheet.

pre{
    font-size:12px;
    background:#fff;
    border:1px solid #000;
    line-height:20px;
    width:600px;
    overflow:auto;
    overflow-y:hidden;
    margin:0;
    padding:0;
}
pre code{
    font-family:Monaco,Courier;
    display:block;
    margin:0 0 0 40px;
    padding:18px 0;
}

Step 2: Put the code snippets inside <pre> and <code> tags.

<pre><code>def f():
    txt = "Some sample code"
    return txt
</code></pre>
If you are new here, you might want to subscribe to the RSS feed or newsletter.

Enter your email address:

Creates the exact copy of your hard disk and allows you to instantly restore the entire machine.
New Acronis True Image Home 2010 is the most reliable and easy in use backup solution. Now with online backup option!
15% Discount Code: FMAATIH2010

What else?

Like this article? Share it

 Digg  del.icio.us  TwitThis  Facebook  Reddit  StumbleUpon

Leave a Reply

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>