{"id":363619,"date":"2022-02-25T07:28:33","date_gmt":"2022-02-25T15:28:33","guid":{"rendered":"https:\/\/css-tricks.com\/?p=363619"},"modified":"2022-02-25T07:28:35","modified_gmt":"2022-02-25T15:28:35","slug":"when-to-avoid-css-text-decoration-shorthand","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/when-to-avoid-css-text-decoration-shorthand\/","title":{"rendered":"When to Avoid the text-decoration Shorthand Property"},"content":{"rendered":"\n

In my recent article about CSS underline bugs in Chrome<\/a>, I discussed text-decoration-thickness<\/code> and text-underline-offset<\/code>, two relatively new and widely-supported CSS properties that give us more control over the styling of underlines.<\/p>\n\n\n\n

Let me demonstrate the usefulness of text-decoration-thickness<\/code> on a simple example. The Ubuntu web font<\/a> has a fairly thick default underline. We can make this underline thinner like so:<\/p>\n\n\n\n\n\n\n\n

:any-link {\n  text-decoration-thickness: 0.08em;\n}<\/code><\/pre>\n\n\n\n
\"Showing<\/figure>\n\n\n\n

\/explanation Throughout this article, I will use the :any-link<\/code> selector instead of the a<\/code> element to match hyperlinks. The problem with the a<\/code> tag as a selector is that it matches all <a><\/code> elements, even the ones that don\u2019t have a href<\/code> attribute and thus aren\u2019t hyperlinks<\/a>. The :any-link<\/code> selector only matches <a><\/code> elements that are hyperlinks. Web browsers also use :any-link<\/code> instead of a<\/code> in their user agent stylesheets.<\/p>\n\n\n

Hover underlines<\/h3>\n\n\n

Many websites, including Google Search and Wikipedia, remove underlines from links and only show them when the user hovers a link. Removing underlines from links in body text is not a good idea<\/a>, but it can make sense in places where links are more spaced apart (navigation, footer, etc.). With that being said, here\u2019s a simple implementation of hover underlines for links in the website\u2019s header:<\/p>\n\n\n\n

header :any-link {\n  text-decoration: none;\n}\n\nheader :any-link:hover {\n  text-decoration: underline;\n}<\/code><\/pre>\n\n\n\n

But there\u2019s a problem. If we tested this code in a browser, we\u2019d notice that the underlines in the header have the default thickness, not the thinner style that we declared earlier. Why did text-decoration-thickness<\/code> stop working after we added hover underlines?<\/p>\n\n\n\n