The text-decoration-inset CSS property solves an issue that we’ve had because the starting of the net, which is that textual content decorations akin to underlines prolong past the primary and final characters (to the perimeters of the content material field, to be particular), leading to vertical misalignment.

I say it’s an issue “we’ve” had relatively sheepishly as a result of possibly you, like some customers, don’t truly care. However should you’re a humorous bunny like me (I believe “designer” is the technical time period) then it most certainly drives you loopy.
That being mentioned, it’s not an issue that I’ve tried to repair as a result of the juice simply isn’t definitely worth the squeeze. One of the best repair might be text-decoration: none and ::after with a customized background, however this could be a bit finicky and I’d relatively use all the options that include native textual content decorations, akin to text-decoration-thickness, text-underline-position (which allows us to alter the place of the underline relative to the font’s inside metrics; the baseline, for instance), and text-underline-offset (which determines the offset from that place).
So, how does text-decoration-inset work? Effectively, if I trim an underline simply sufficient for it to vertically align with the textual content, I wind up with this as an alternative (this solely works in Firefox 146, by the way in which):

Nonetheless, you possibly can truly trim the decorations as a lot as you need, which allows us to create some actually cool ones and even transition or animate them. Let’s take a fast look, we could?
text-decoration-inset fundamental utilization
text-decoration-inset, previously text-decoration-trim, allows us to clip from the ends of the underline or no matter text-decoration-line is computed. That is the syntax:
text-decoration-inset: ;
Sure, because of this we will set totally different inset values for the left and proper sides.
These values should be s, however we will use relative lengths akin to em items, that are relative to the computed font-size. So, if the font-size adjustments, the insets scale with it. For instance, within the demo above, 0.076em (which is what I’ve set because the left inset) means 7.6% of the computed font-size, and that’s the worth that makes the left inset align with the left stem of the letter “N” and different left stems. This worth was decided by trial and error, but it surely solely must be decided as soon as for every font.
If that first letter was, say, W? Yeah, then the inset wouldn’t align, so it’s not a excellent answer. I’d say that it’s appropriate for when you understand what the content material will likely be.
Perhaps the W3C will provide you with an answer for vertically aligning textual content decorations in addition to a number of traces of textual content each precisely and robotically. Till then, that is nonetheless a cool answer that permits us to create completely aligned results like this (this demo makes use of an overline and an underline, and a complete ‘lotta text-decoration-thickness in fact):
Animating text-decoration-inset
text-decoration-inset is extra fascinating once we begin to consider transitions/animations. We regularly animate underlines, or ought to I say fake ::after underlines, however with text-decoration-inset we will do it natively. Within the instance under I multiply the insets by ten on :hover. Nothing too loopy, however do not forget that we will solely use values, so attempt to use em items, or not less than check the textual content with totally different font sizes.
Once more, Firefox 146+ required in the meanwhile:
a {
transition: 300ms;
text-decoration-inset: 0.046em 0.009em;
&:hover {
text-decoration-inset: calc(0.046em * 10);
}
}
Getting a bit extra bold now, this subsequent demo leverages a CSS @keyframes animation to create that capturing star underline impact. The way it works is that we push the left inset all the way in which to the opposite aspect — however s solely, keep in mind? We are able to’t use 100% right here, so as an alternative I’ve decided that the width of the factor is 4.5em and used that as the worth as an alternative (the extra exact we’re, the higher the animation or transition). Test the code feedback for a full clarification:
a {
/*
The worth at the beginning and finish of the
animation, in addition to the default worth
*/
text-decoration-inset: 0.046em 0.009em;
&:hover {
animation: 1s next-level;
}
}
@keyframes next-level {
/* By half-way by means of the animation... */
50% {
/*
...the left inset has shifted 4.5em,
which is the complete width of the factor
*/
text-decoration-inset: 4.5em 0.009em;
/* It’s pale out as properly */
text-decoration-color: clear;
}
/* Instantly after that... */
50.999% {
/* ...each insets are set to the left */
text-decoration-inset: 0.046em 4.5em;
}
/* Then it animates again to the default worth */
}
Total, text-decoration-inset is a pleasant function. It isn’t with out its flaws, however no function ever is. Personally, something that helps me to refine a element natively may be very a lot welcome, and with text-decoration-inset we’re in a position to refine two — the textual content ornament alignment (relative to the textual content) and the textual content ornament transition or animation.








