Monday, December 22, 2025

text-decoration-inset is Like Padding for Textual content Decorations


The text-decoration-inset CSS property solves an issue that we’ve had for the reason that starting of the online, which is that textual content decorations comparable to underlines lengthen past the primary and final characters (to the sides of the content material field, to be particular), leading to vertical misalignment.

I say it’s an issue “we’ve” had slightly sheepishly as a result of possibly you, like some customers, don’t truly care. However in case you’re a humorous bunny like me (I feel “designer” is the technical time period) then it almost 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 well 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 slightly use all the options that include native textual content decorations, comparable to text-decoration-thickness, text-underline-position (which permits us to vary 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? Properly, if I trim an underline simply sufficient for it to vertically align with the textual content, I wind up with this as a substitute (this solely works in Firefox 146, by the way in which):

A default blue link zoomed in to the spacing between the text underline and border box, showing that the underline does not extend all the way to the border box.

Nonetheless, you possibly can truly trim the decorations as a lot as you need, which permits us to create some actually cool ones and even transition or animate them. Let’s take a fast look, lets?

text-decoration-inset fundamental utilization

text-decoration-inset, previously text-decoration-trim, permits 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, which means we will set completely different inset values for the left and proper sides.

These values should be s, however we will use relative lengths comparable to em models, that are relative to the computed font-size. So, if the font-size modifications, 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, however it 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 good answer. I’d say that it’s appropriate for when you already know what the content material can be.

Possibly the W3C will give you an answer for vertically aligning textual content decorations in addition to a number of strains of textual content each precisely and robotically. Till then, that is nonetheless a cool answer that allows 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 attention-grabbing 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 keep in mind that we will solely use values, so attempt to use em models, or at the very least check the textual content with completely different font sizes.

Once more, Firefox 146+ required in the mean time:

a {
  transition: 300ms;
  text-decoration-inset: 0.046em 0.009em;

  &:hover {
    text-decoration-inset: calc(0.046em * 10);
  }
}

Getting a bit extra formidable 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, bear in mind? We will’t use 100% right here, so as a substitute I’ve decided that the width of the component is 4.5em and used that as the worth as a substitute (the extra exact we’re, the higher the animation or transition). Examine the code feedback for a full clarification:

a {
  /*
    The worth at first 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 component
    */
    text-decoration-inset: 4.5em 0.009em;

    /* It’s light 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 could be very a lot welcome, and with text-decoration-inset we’re capable of refine two — the textual content ornament alignment (relative to the textual content) and the textual content ornament transition or animation.

Related Articles

Latest Articles