Home >

Line selection without Javascript

Code & demo

Click on line numbers to select them.

HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
<div class="container">
  <pre class="lines">
    <a id="L1" href="#L1">1</a>
    <a id="L2" href="#L2">2</a>
    <a id="L3" href="#L3">3</a>
    ...
  </pre>
  <pre class="code">
    code…
      goes…
        here…
  </pre>
</div>

CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.container {
  position: relative;
  display: flex;
}

.lines a:target::before {
  content: "";
  position: absolute;
  display: block;
  left: 0;
  width: 100%;
  height: calc(1rem + 3px);
  background-color: rgba(0, 0, 0, 0.1);
}

.code {
  width: 100%;
}

Notes

Support

All major browsers.

Sources