My goal is to create rows of lines (with line numbers) such that:
- line numbers line up with the appropriate line even if each "line" exceeds its parent element's width and wraps to a new physical line
- only the text is selected, not the line numbers
- whitespace is preserved both within and before each line
I've got a snippet that accomplishes some of these goals on specific browsers:
.code { counter-reset: line; }
.line { counter-increment: line; white-space: pre-wrap; }
.line:before { content: counter(line) " - "; }
<div class="code">
<div class="line">Line 1</div>
<div class="line"> Line 2</div>
<div class="line">Line 3</div>
</div>
Here's the problem:
- everything works in Chromium
- everything works in Firefox except for the third goal - whitespace at the beginning of the line is truncated for some reason
- everything works in IE except for the second goal - line numbers are included in the selection
(I don't have access to a Mac, so I have no idea about Safari. I'm too lazy to test Opera.)
Is there a way to make this work in all common browsers?