Add Line Numbers To Code Blocks Using Only CSS (And Some Spans)

Intro

I'm using syntect^1^^ to highlight code for my site. It's working nicely. One feature I'd like that it doesn't have is the ability to add line numbers to the code blocks. It's possible to do it in CSS with a little tweaking.

Code Setup

The trick to this approach is adding `<span>`html` tags that wrap every line of code. They're used by the CSS to position the line numbers. I wish this step wasn't necessary, but I haven't found a way around it.

The syntect highligher I'm using doesn't


alfa
bravo

charlie
delta
<pre><code="codeLines">
<span class="codeLine">alfa</span>
<span class="codeLine">bravo</span>
<span class="codeLine"></span>
<span class="codeLine">charlie</span>
<span class="codeLine">delta</span>
</code></pre>

Reference

An example that requires adding code elements to every line

Reference

syntect highlighting library

TKTKTKT: Add link