How to Add a Number line to your Code Using Only CSS

So in this article I will be showing you how to add a numberline to the code or script that you are showing between <pre></pre> tags on your website, using only css. It took me a while to figure out how, but thanks to the /r/web_design subreddit I was able to figure out how with their help.

If you are using Jetpack, you need to insert this code into your custom .css

First note that the in the content: part, you can generate how ever length of numbers you need using Java:

Java for loop make numerline

And copy and paste the numbers in between two single comments '' of content:'' .

Second, you might need to change .post-content name to fit your website’s theme. You can check using inspect element:

Inspect Element to find name before pre tag
So I am using inspect element to find the parent element that is before the pre tag. In this case it is .post-content

That’s it. Please tell me what you think, if I can improve anything else, or it I missed anything! Down below is an example of the numberline:

1 overflow-y: hidden; makes sure that the numberline isn't seen below the content-box of the <pre></pre> tags

Edit: Now I use the Crayon Syntax Highlighter plugin instead. It takes care of all the highlighting, editing, and multitudes of small details when it comes to displaying code; it even detects the type of programming language that you are using. I highly recommend it :D

What's Your Opinion?