html - Right-align CSS counters in ::before pseudo-element -


i'm using css counters , <code> tag show syntax highlighted code snippets automatically generated line numbers:

jsfiddle

html:

<code>  <div class="line"><span>line 1</span></div>  <div class="line"><span>line 2</span></div>  ... </code> 

css:

code {     display: inline-block;     border: 1px black solid;     padding: 1em;     font-family: "consolas", "monaco", "courier new", monospace;      counter-reset: line; }  code .line {     display: block;      counter-increment: line; }  code .line::before {     border-right: 1px black solid;     padding-right: 1em;     margin-right: 1em;      content: counter(line); } 

it works 9 lines, once hits 2 digits, gets out of alignment:

css counters

how can make left edges of lines align? or right-align line numbers?

i've tried:

  • counter(line, decimal-leading-zero) - works 99 lines, breaks @ 100, , don't way looks
  • altering content javascript, getcomputedstyle(line, '::before').content returns "counter(line)"

you can use display:inline-block; , width based on practical needs:

demo: http://jsfiddle.net/zxsxu/14/

code .line::before {     display:inline-block;     width:2em;     border-right: 1px black solid;     padding-right: 1em;     margin-right: 1em;     content: counter(line); } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -