Is it possible to change the distance between the text and the underline?
Yes, as colleagues have already shown you can increase the distance between what would be an underline and the text. I'll just add a few things to complete what you asked in the beginning.
But in the future I think we'll have some news about this, as W3C has in its drafts a dedicated property for this.
text-underline-offset
: official W3C documentation link
"Specifies the offset of underlines as a length." This replaces any information in the font or derived from glyph shapes / character ranges. Authors are strongly encouraged to use in units so that the offset scales with the font.
If yes, do you have any direct CSS property that we can use to put the spacing?
To remove the original underline , created by text-decoration: underline
, there is no such property yet.
Since for text-decoration we only have these 3 properties:
text-decoration-line
Sets the type of decoration used, such as underline or line.
text-decoration-color
Defines the line color
text-decoration-style
Defines the line style, solid
, wavy
, dashed
etc ...
Here's the Mozilla documentation if you'd like to check out: link
There is still the text-underline-position
property but it is used to place underline in vertical text etc, not for this kind of thing, although it has a correction for when descendants go beyond the line. >
W3CSource: link
In addition, can you use an image as underline for a text?
Yes it is also possible, but take into account what the other colleagues have said. The example I made was with a ::after
pseudo element, it will always be 100% of the tag width, and since I used measurements in rem
and em
it will always keep a relative proportion to the size of font-size
p {
line-height: 2em;
font-size: 1rem;
}
span {
display: inline-block;
position: relative;
font-size: 1em;
}
span::after {
content: "";
position: absolute;
top: 2em;
left: 0;
width: 100%;
height: 0.2em;
background-image: url(https://placecage.com/50/50);
}
span:nth-child(2) {
font-size: 1.25em;
}
<p>
Lorem ipsum dolor sit, amet consectetur <span>adipisicing 123 abc</span> elit. Quod dolore natus voluptates at a soluta ad nulla aspernatur <span>consequatur</span> ducimus! Adipisci reprehenderit itaque delectus, molestias magni recusandae blanditiis eos libero quisquam voluptatibus deleniti eveniet numquam corporis porro nulla laudantium quibusdam quam sunt ea temporibus ratione incidunt! Incidunt possimus rem illo quam cum minima ad aut sunt sapiente consequuntur eum inventore ipsum, dignissimos quaerat neque deserunt nisi, odit, suscipit quo molestias ut illum officia. Voluptatem magni ipsa nobis facere voluptate deserunt minima adipisci voluptas sapiente dolore placeat facilis perspiciatis repudiandae tenetur aut at, nam id voluptatum atque libero! Veritatis, qui maxime.
</p>