Marie Mosley just finished up a revamping of the text-decoration
property (and friends) in the Almanac. You’re probably aware of this property. For instance, most default browser styles include underlined links by way of text-decoration: underline;
– which you can remove with text-decoration: none;
.
But you may not be aware that there is more you can do with this property, as well as various sub-properties offering more fine-grained control.
Text can have multiple decorations
Like:
a {
text-decoration: underline overline;
}

See the text-decoration
entry in the Almanac. More specifically, this is adding multiple values to the text-decoration-line
subproperty.
You can change the color of the decoration
The default for the color of the decoration lines is the same as the color
of the text. But you can change that:
a {
text-decoration-color: #f06d06;
}

Check out the text-decoration-color
entry in the Almanac.
Note that Gecko renders the underline behind descenders while WebKit/Blink render on top:

A common way of handling colored underlines has generally been to use a border
instead of text-decoration
. Borders can have individually controlled colors, thicknesses, and position a bit better than text-decoration
can pull off.
But there are some things borders can’t do, like…
You can change the style of the decoration
Can’t make a border do this!
a {
text-decoration-style: wavy;
}

Check out the text-decoration-style
entry in the Almanac.
It’s going to get even fancier
There has been some clear desire for better underlined text. For instance, skipping the descenders for better readability, as noted in that post:

That’s going to be controlled by text-decoration-skip
, although not yet implemented anywhere. In the meantime, setting an underline to a more relaxed, less contrast-y color might help. Here’s rgba() in use:

And skipping descenders is just one ability it’s likely to have. You’ll be able to skip certain inline elements, whitespace, and control the edges.
Note that Safari/iOS seems to skip descenders by default.

-webkit-text-decoration-skip: none;
works.Playground
Due to varied levels of browser support, some (or all) of this demo may not work in your browser.
See the Pen text-decoration-style by CSS-Tricks (@css-tricks) on CodePen.
So yeah! Even simple stuff like this (that we sometimes take for granted) changes over time in CSS land.
Interesting quirky bug I recently discovered:
By default, underlines pass through all child content (you can’t turn them off by changing the
text-decoration
property) except for inline blocks.You would think “inline block” would include an inline SVG. But the default display mode for inline SVG is just
display: inline
. Most browsers will underline every letter in your SVG icon if you include it within a heading or link that has underlining. Internet Explorer is for once the sensible browser, and does not do this. For the others, you have to set your icon todisplay: inline-block
.(And if you’re wondering: I discovered this while creating little “PDF” icons to be inserted after links to PDF documents. I wanted the icons to be inside the links, so that the color would match, but the underlining was uuuugly!)
Awesome. I didn’t know about that. How often did I use some kind of :after/:before combination to customise the underline?
I’ve got the latest Chrome but don’t get any of those examples in the demo working. They look all the same: underlined.
@René: Chrome = version 31 and only with experimental web platform features flag enabled according to the browser support link:
https://css-tricks.com/almanac/properties/t/text-decoration-style/#browser-support
Me too. Chrome doesn’t show anything but underlines.
Same for me on Chrome on Android tablet. All underlines are the same.. Just underlines.
In saying that, it would still be worthwhile using them because when the browsers do catch up, your fancy underline will be there ready and waiting.
This actually made me giggle, I was like “NO WAY THIS WORKS.” My coworkers and I now feel silly for not believing you, also having the same issue in chrome/IE. Thanks for the share, made me think I didn’t know my css at first. :)
Great article Chris. Was attempting this a little while back. Also was wondering if you had any decent examples of text-decoration effects utilizing css transitions?
Need some thing like this in the spec:
text-decoration-padding-bottom: 0.2em; ( With a better name. )
That’s genius. I’d always hoped for something like that
It’s awesome. Thanks.
Nice one I can use it in my tupac quotes page . thanks
Until there is widespread browser support, it’s probably best to not use these CSS declarations on widespread use. Inheritance functionality might need to be tightened up. Overall, these would be very useful to have and I hope other browsers jump on board.