Presenting Code with Line Numbers
May 21, 2007
You probably already know this, but I design websites. While the aesthetics and functionalities differ from one site to the next, I often end up reusing various snippets of code. But because I don’t have an organised collection of these pieces of code, I inevitably end up wasting time writing code from scratch or scouring the net looking for it. So, until I get around to creating my design notes blog, I’m going to be posting those pieces of code here.
*
Line numbers are integral to deconstructing and understanding any piece of code – they make specific bits and pieces in a block of code easy to pinpoint. So when being explained on the web, code needs to be presented with line numbers. However, there is currently no way to simply and automatically number lines with XHTML/CSS – unless you use <ol>, the numbered list tag, with every every individual line of code enclosed in its own <li> tag.
That awkwardness aside, you can create something that looks like this:
Pretty, isn’t it? I first saw this particular style at 24 ways. Whipping up the code wasn’t difficult.
My HTML is:
<ol>
<li><code>I like to think</code></li>
<li><code>that Rumi would have been</code></li>
<li><code>a fan of Helvetica.</code></li>
</ol>
And my CSS is:
ol {
width: 400px;
border: 1px solid #E4F2F7;
padding: 5px 10px;
margin: 50px auto;
padding-left: 30px;
font-size: 15px;
}
li {
margin: 5px 0;
}
code {
display: block;
background: #E4F2F7;
padding: 5px;
font-family: Helvetica, Arial, Verdana, sans-serif;
}
I’m not going to bother updating the CSS stylesheets for this blog just yet. But for now you can see the difference, right? I just don’t like how much clunkier it makes the HTML.

20 Responses to “Presenting Code with Line Numbers”
1 Adnan. May 21, 2007
Hmmmm.. is that HTML bit valid HTML? doesn’t seem like. The first code tag has an ol tag inside of it. Is that by design?
Also, if you were to get numbered lines for this purpose, wouldn’t it be harder to copy paste this code into someplace usable? You would find yourself removing the numbers each and everytime, and before you know it, that may become more annoying than not having numbers in the first place.
2 fathima May 21, 2007
It was more than invalid. It was insane. It was 2 in the morning when I typed this and I remember getting confused over whether or not I wanted the HTML code I presented here to be styled or not and so, being confused, I put in all this extraneous and invalidating code. Anyway, thanks for the catch. I’ve fixed it now.
And you’re absolutely right about the copy-pasting. That’s another major disadvantage of this method: you can’t copy the code without copying the line numbers, too. One way of sidestepping that issue is by using a background image of numbers. Check Miha Hribar’s site where he does exactly that. What I don’t like about this method is that it breaks when you increase font-size or have a really long line of code that wraps.
3 Muntaka May 21, 2007
This post is crazy. It’s just plain and simply cray-ze. The kind of cray-ze which deserves wrong spellings.
4 fathima May 23, 2007
buddy, this stuff is my bread and butter. it pays my bills and makes me feel like a contributing member of society and my family. i like my cray-ze, thankyou verymuch.
5 Laura May 28, 2007
Ahem. You have been rather silent of late, especially for one who made such a stink over basit’s blogging reticence. What’s the deal?
6 fathima May 29, 2007
you have uncovered me for the hypocrite that i am.
… no but i am working on it, both on the writing more often/regularly and the hypocrisy. it’s dreadfully tiring.
7 b May 30, 2007
see, that’s what i said. but when i said it, did i receive understanding or acceptance? i did not.
i also have maybe three more things to add to your reading list but am not sure yet, so they will wait a little.
8 TDH Jun 5, 2007
WOW. I knew you would be some designer.
Still, lets see. I used to code like a crazy javascript monkey till I found certain web 2.0 libraries which integrate through CSS and provide some classy functionality.
I wonder if you would have heard about this
http://script.aculo.us/
http://moofx.mad4milk.net/
I designed some really cool stuff with this and sadly all work I did with designing websites and creating style sheets is now the property of my previous employer :(
New employer wants me to work on server side stuff which kind of is annoying because people do not get to know the full me but the upside is I get to work on enterprise level stuff.
9 fathima Jun 8, 2007
buddy, i was all /over/ the Ajax. ok, not really all over.
when scriptaculous was first coming it out, i played with it. and moo. i love the moo site. but i feel very uncomfortable with Ajax and Ruby on Rails and JS in general. back in the day i had the time and the lack of a life and so i could spend hours and hours and days and months slaving over CSS. i need that kind of quiet to really get into Ajax and all the rest of it.
also, don’t i know you from Yasmine’s site?
10 TDH Jun 10, 2007
Oh yeh! .. lol
I frequent her place too but she is busy I guess and updates after long intervals.
WOW! Okay. That is geek stuff. I always wanted to make my own theme, but never got the time to get around the whole thing. Can you believe that I know 7 scripts (mostly unix). I left open source dev after uni and never came back to it (that explains I do not know Ruby which is a recent happening), ’cause well corporates do not like to have them around.
Ajax is kind of fun. Also do have a look at the new Adobe Apollo platform. That is going to be the next big thing. You can pretty much say good bye to Ajax after that.
Good thing you have a good life now :) I still don’t :$ partly because of 10 hour work day + 2-3 hour traffic jams. Partly because may be I have lost it .. haha
11 Zainab Aug 28, 2007
why would rumi have liked helvetica?
esp. since helvetica sounds androgynous.
12 fathima Aug 28, 2007
what would rumi have had against androgyny?
13 Zainab Aug 29, 2007
fathimacader: stop answering questions with questions — tis bad manners, aka ‘respect your elders’
so yah: why would rumi have liked helvetica?
and then i will answer thine question.
14 fathima Aug 29, 2007
only old people demand respect. the ageless simply get it.
and rumi would have liked helvetica, because i like helvetica. and i like it because … well look at it: a tinge of blurriness at the edges / a suggestion of growth; tall, yet not pointy; a slight awkwardness; basic awesomeness.
15 Zainab Aug 30, 2007
hmm. that doesn’t sound so much like helvetica to me.
sounds like a person, yes.
who is this Ageless, btw — i think they and i should meet.
16 fathima Aug 31, 2007
i know people who are like Helvetica. i like them. they make me like people in general. then i meet some like Times New Roman and i have a temporary crisis of faith.
so i am grateful for Helvetica and for people like Helvetica.
17 Zainab Aug 31, 2007
because rumi was rumi, he’d probably gaze /through/ this
and say:
they’re both fonts — facetypes, not archetypes.
also: it’s what these fonts do that matters, not what they look like, unless you’re suggesting the two are connected, i.e., the
look and act, not font and people.
if you’re a helvetica, can i be bestioles?
18 Zainab Aug 31, 2007
bestioles :
http://www.dafont.com/bestioles.font
19 fathima Aug 31, 2007
such an amateur, you make me hurt.
20 Zainab Aug 31, 2007
you actually meant to say you loved me.
that’s okay — i get you, fathimacaderwhoseheadisbigbutnotadugong.
(: