Pseudo-element solution
Use the bottom
-positioning value on the pseudo
-element instead of top
. This forces the pseudo
-element to be positioned at the bottom, instead of 50%
from the top. I used bottom: -10px
as that is the height of the pseudo
-element, so it aligns perfectly.
Read more on position values: https://developer.mozilla.org/en-US/docs/Web/CSS/position
HTML-element solution
Instead of creating a pseudo
-element, you could opt to make an HTML element instead.
Make a parent container, apply flex
to it so the text and the line will align.
Make the .line
-element a block element, so it will break into a new line.
You can still apply position: absolute
and position: relative
on the .line
and the h2
if you want to position it in another way. Or you could simply use e.g. transform: translateY(5px)
to move the line up a bit.
.background-highlight {
position: relative;
display: inline-block;
color: black;
text-align: right;
}
.background-highlight:after {
content: '';
position: absolute;
width: 100%;
height: 10px;
left: 0;
bottom: -10px;
background-color: #cef230;
z-index: -1;
}
/* Without pseudo */
.nopseudo {
display: flex;
}
.nopseudo h2 {
text-align: right;
}
.nopseudo .line {
height: 10px;
width: 100%;
background-color: #cef230;
display: block;
}
<h2 class="background-highlight">The Skippers <br>Escape</h2>
<div class="nopseudo">
<h2>The Skippers <br>Escape<span class="line"></span></h2>
</div>