Numbers misaligned with code in a <pre><code>

HTML code so far

<div class="code-container">
      <div class="line-numbers">
        <span>1</span>
        <span>2</span>
        <span>3</span>
      </div>
      <pre>
    <code>
&lt;div id="example"&gt;
  &lt;p&gt;This is example text&lt;/p&gt;
&lt;/div&gt;
    </code>
  </pre>
    </div>

    <p>JS</p>

    <div class="code-container">
      <div class="line-numbers">
        <span>1</span>
      </div>
      <pre>
    <code>
const ExampleVar = document.getElementById("example");
    </code>
  </pre>
    </div>

and my CSS:

.code-container {
  display: flex;
  background-color: #f4f4f4;
  border-radius: 5px;
  overflow-x: auto;
  font-family: monospace;
  color: red;
}

.line-numbers {
  padding: 10px 5px;
  text-align: right;
  color: #888;
  user-select: none; 
  background-color: #eaeaea; 
}

.line-numbers span {
  display: block;
  line-height: 1.5em; 
}

pre {
  margin: 0;
  padding: 10px;
  line-height: 1.5em;
  overflow-x: auto;
}

code {
  display: block;
  white-space: pre;
}

and this is the product:

Notice how the numbers are unaligned? How do I fix this? thanks!

What do you expect by nesting the code TAG within the pre TAG.

Can you please elaborate

Please explain What is the final result you expect by nesting the code TAG within the pre TAG.

what should I do instead? I’m lost

Ok, so I did this instead,

<code>
    <pre>
const ExampleVar = document.getElementById("example");
  </code>
        </pre>

and it worked? I am happy that it worked but don’t understand lol

Although , you passed the test. Maybe the test is looking for code element, that’s you have added. But that isn’t nested correctly. The code element should be nested in pre element.

Yeah, that’s what I thought it should be too, and still do think that, but it doesn’t work. It just misaligned them. It was working yesterday, I didn’t touch the code until today, and then today it’s all unaligned! I think it might be glitched?

@hasanzaib1389 , does it have to do with this spacing on top of it?


There is no space in the code, so is it a CSS thing?

No, space between dividend elements doesn’t affect on code

Hi @accumbensmusic1

Since the code element has a display property of block, incorrect nesting is overriding the display property.

Happy coding