-1

I'm trying to get the border of a <p> element to only take up the space occupied by text. Here's the relevant part of my html:

<h2 style="font-weight: normal;">

  <style>
      .border {
      border-style: solid;
      border-color: black;
  </style>

  Plate number:
  <br>
  <p class="border" style="">5649JSN</p>

</h2>

And here's how it looks:

enter image description here

How can I fix this? Thanks in advance

Edit: I can't know beforehand how much space the plate number will take

Swati
  • 26,918
  • 4
  • 18
  • 39
David Antelo
  • 443
  • 4
  • 17

1 Answers1

2

Illegal HTML does what illegal HTML does.

Is it valid to have paragraph elements inside of a heading tag in HTML5 (P inside H1)?

Don't wrap your P in H2 or make H2 display:inline-block

.border {
  border-style: solid;
  border-color: black;
}

h2 {
  font-weight: normal;
  display: inline-block;
}
<h2>
  Plate number:
  <br>
  <p class="border">5649JSN</p>
</h2>

Better: don't use a P

.border {
  border-style: solid;
  border-color: black;
}

h2 {
  font-weight: normal;
}
<h2>
  Plate number:
  <br>
  <span class="border">5649JSN</span>
</h2>
mplungjan
  • 155,085
  • 27
  • 166
  • 222