I need a span to be displayed as a marker BENEATH important texts on my page.
It should have been positioned at the bottom of the p element – but it does not.
Can someone tell me what’s wrong in the code?
.marker {
position: absolute;
bottom: 0px;
background-color: red;
height: 3.9px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="a.css">
</head>
<body>
<div>
<p>
This is
<span>IMPORTANT INFORMATION THAT NEEDS TO BE EMPHASIZED
<span class="marker"></span>
</span>
</p>
</div>
</body>
</html>
2
Answers
Quote in comments: "What is it that you actually try to do? Do you want to mark that by underlining it?
Respond of OP: "Yes, exactly"
XY problem here. Instead of using a pseudo-element and positioning it, Simply add the underline as
border-bottom
to element itself:Another method and actually the better one would be the usage of
text-decoration: underline
:There are some things to consider with this approach and using
text-decoration
or evenborder-bottom
can be an easier way to get the style you currently have. An obvious problem is that it will not work properly if the text is long enough that it wraps lines.If you’re just doing a plain straight line under the text, removing the
marker
element and just using the container withborder-bottom
is an easier way to get this style and provides a bit more customization than doestext-decoration
.The benefit to this approach is that it allows for the most customization. For example, you could use an SVG to get a hand-drawn look for the underline.
Basically there are three approaches to underlining text:
text-decoration
: most simple, least control of styleborder-bottom
on the container of the text: slightly more complex, slightly more control on styleposition: absolute
on the marker element: most complex, full control over style, won’t work with wrapping textIf you choose to go with this approach here is what you’ll need to change:
You’ll need to give the wrapper a position of
relative
so that the marker element is positioned absolutely relative to the parent.The next reason why you’re not seeing the element is because it has no width, this can be fixed by adding
width: 100%
. Finally, you’ll notice the marker shows up after the text, this can easily be fixed withleft: 0px
.