Thursday, September 3, 2009

CSS Style to format source code

One of the things I want to show in this blog is source code.  The best source code viewer I’ve seen to date is the one they use at Stack Overflow.  The cool thing they do is highlighting based on syntax.   My CSS isn’t quite as nice, but it works.   All right, let’s not kid ourselves.  Mine is just Courier New in a box.  Here’s what I am using:


<style type='text/css'>

code {
color: #000077;
background: white;
border-bottom: blue 1px solid;
text-align: left;
border-left: blue 1px solid;
margin: 1em 0px;
padding-left: 30px;
padding-right: 20px;
padding-top: 20px;
padding-bottom: 20px;
display: block;
font: 100%/17px "Courier New", courier, fixed, monospace;
font-weight: bold;
border-top: blue 1px solid;
border-right:blue 1px solid;
}

</style>



I use that style then surround my code snippet <pre><code>like so</code></pre>.



I experimented with green or yellow on black, but that always made my eyes get all funny.  The other thing I wanted was a horizontal scroll bar when the frame became too narrow.  From my reading of the specs, overflow: auto should do it, and it works perfectly in Firefox, but in IE 8 I loose the indenting so I’m not going to use that for now.

No comments:

Post a Comment