Skip to main content

Command Palette

Search for a command to run...

How to Change Link Underline Color with CSS

Updated
โ€ข3 min read
A

Indie Guitarist

Last night, when I look at Basecamp website, I notice an interesting CSS stuff.

ezgif-2-5893d35b0bcf.gif

When I hover the Nav link at the header, there will be a nice yellow highlight appear below the link. This is cool, tho .

When I inspect the a tag, they used text-decoration stuff.

Screenshot 2021-06-23 at 16.06.11.png

I do a quick research and found this awesome, simple CSS magic to change the underline color.

I already do the research, you just need to grab the popcorn ๐Ÿฟ and enjoy the journey ๐Ÿš€.


By default, the color of the underline is set the same as the text color of the link. If the link is blue ๐ŸŸฆ, the underline also a blue color ๐ŸŸฆ. But, you know right, because of human's creativity(cough, cough...)๐Ÿ˜†, sometimes we want to give the underline a different color from the text of the link.

There are two ways I found to make this happen. One is using the text-decoration stuff, the second one is using the old border-bottom trick.

personally, I prefer text-decoration over border-bottom. The right tool for the right job. I donno.


โœ… text-decoration

text-decoration is the most straightforward way to underline text.

text-decoration-color is what we needed

a {
    text-decoration: none;
    color: blue;
}

/* show underline on hover */
a:hover {
    text-decoration: underline;
    text-decoration-color: red;
    -webkit-text-decoration-color: red;
}

note: -webkit is for Safari.

This is the only thing you needed. If you want to customize more, read more :P


๐ŸŒˆ text-decoration properties

text-decoration works fine by itself, but you can add a few properties to customize the way it looks:

1) text-decoration-color

text-decoration-color lets you change an underlineโ€™s color separately from its text color.

2- text-decoration-skip

This property will skip the edgy alphabet like g, p, f. It didn't go through it. Examples :

3- text-decoration-style

text-decoration-style give you a free underline design using different values. No need to add SVG.

Here are the available values that you can use:

  • dashed
  • dotted
  • double
  • solid
  • wavy

note: This example is from Mozilla MDN. You also can seperate the text-decoration into text-decoration-color and text-decoration-style.

a {
 text-decoration: underline;
 text-decoration-color: red;
 text-decoration-style: wavy;
}

4- text-underline-offset

This is quite cool. It can be used to define how far the line is from the initial text.

5- text-underline-thickness

This property is used to tell how big the underline is.


โœ… border-bottom

disclaimer: if you happy with the first trick, just go with it. This is just another same trick.

If we want to use border-bottom trick, first we need to remove the underline with the text-decoration property value of none. Then we add the border-bottom property with 3 short-hand CSS values of 3px solid red.

3px = Variable of the underline width in pixels

solid = Border style (solid, dotted, or dashed)

red = Color code. Hex also can, like #999999


I add this cool trick into my blog

I also add this awesome tricks into my own blog. I implement it on every a tag or any link-related tag.

Pretty cool, hah... ๐Ÿ˜Ž

Thank you for traveling with me ๐Ÿš€, I hope you enjoyed the journey ๐Ÿ”ฅ.

The End


resources:

1 2 3 4 5 6 7 8

613 views
J
Jack Med4y ago

Wow.. Thanks for the trick. Keep posting more!

1
S

very interesting , good job and thanks for sharing such a good blog.

1
T

amirul asyraf, Thanks for sharing it. Nice trick.

1
C

I was looking at the examples and was like, pretty sure these are my examples ๐Ÿ˜‚. So thank you for linking me in your resources I'm glad my examples could help you out here.

Well written document and it's great it covers all these text-decoration features.

Keep it up โœจ

1
A

Yeah Chris, some of the examples are from yours ๐Ÿ˜‚. Thanks for helping us :P

1
P

Wow๐Ÿ”ฅ !! very interesting ๐Ÿ‘

1
A

Yeah, super cool hah :P

S

Brother, You wrote this article really well! Kudos to you! Keep up the amazing work! Loved the article a lot ๐Ÿ’–

2
A

Thanks, dude. I will !!!! I will bring my reader into a fascinating journey always ๐Ÿš€ .

A

Cool tip so useful amirul asyraf

1
A

Super dope tricks hah ๐Ÿคœ๐Ÿค›

E

Great trick! And it does look really nice amirul asyraf.

1
A

Yeah, very cool tricks :p ๐Ÿš€