Line Numbers for Code Listings

I always wanted to have the code listing on the site  with “line numbers’. Unfortunately, most of solutions using JavaScript and CSS, require special formatting for the code wrapped  with <pre> tag. So, I’ve borrowed the code (Source: Provide Line Numbers for Your Code Listings | CodeAnimal), modified a little JavaScript and CSS to work with single or multiple tags…

modified JavaScript code

jQuery(function($) {
  $('pre').each(function(index, element) {
    var temp = $(this).html().split('\n');
    var insert = "";
    $.each(temp, function() {
        insert += '<span class="line">' + this + '</span>';

modified CSS

pre {
    counter-reset: linecounter;
pre span.line{
    counter-increment: linecounter;
pre span.line:before{
    color: rgba(0,102,51,1);
    text-align: right;
    content: counter(linecounter);
    width: 2em;
    display: inline-block;
    border-right: 2px solid rgba(204,255,153,1);
    margin-right: 10px;
    padding-right: 10px;
pre span:nth-child(2n+1) { background-color:rgba(204,204,204,.25); }
pre span:hover { background-color:rgba(255,255,204,1); }