I want to display source code with line numbers in a web page. (Unfortunately I cannot use existing highlighting components like google-code-prettify because I must use my own syntax parser.)
To prevent the line numbers from copy&paste with the source code I use a table with one row and two cells: In the first cell there are the line numbers, in the second cell is the source code:
<html><body>
<table>
<tr>
<td>1<br/>2<br/>3</td>
<td>
<pre>int main() {
println("I am a very looooooooooooooooog source code line.")
}</pre>
</td>
</tr>
</table>
</body></html>
This works, but I have problems with long source code lines: The table gets very wide. If I break long lines the line numbers on the left side are not correct anymore.
The best solution would be a table were the user can scroll wide lines, just like this example from GitHub on line 88. I have tried with various CSS parameters like overlay
, width
, white-space
but I didn't get the result from GitHub.
How can I scroll vertical long lines in a table without scrollbars?
(Or is there a better solution to display code with line numers without tables? I have seen solutions that uses an ordered list or an textarea, but they also have problems.)