-1

I want to get exactly like this in html: enter image description here

1 and 2 is just under the words even though the sentence's position changes like this: enter image description here

How can I achieve this?

Note: I want also save this sentence with numbers and their positions to database which is another subject :)

Community
  • 1
  • 1
ninbit
  • 530
  • 6
  • 24

1 Answers1

2

Positioning and pseudo-elements are probably the cleanest solution to that. A more "dynamic" variant might look like this:

p {
  font-family: sans-serif;
  line-height: 2.5;
}

span {
  line-height: 1;
  position: relative;
  text-decoration: underline;
}

span::after {
  content: attr(data-number);
  left: 50%;
  position: absolute;
  top: 100%;
  transform: translateX(-50%);
}
<p>
  This <span data-number="1">sentence</span> has an underlined <span data-number="2">text</span>.
</p>

<p>
  This <span data-number="1">sentence</span> has<br>
  an underlined <span data-number="2">text</span>.
</p>
maryisdead
  • 1,792
  • 2
  • 19
  • 30
  • Just like what I want. Pseudo-elements is the key point and I didn't know its purpose till now. Thanks! – ninbit Jan 11 '19 at 14:18