How to remove underline from a name on hover
The quickest, dirtiest way to eliminate that pesky underline on hover is to unleash the power of CSS. Define the text-decoration: none
for the a:hover
state. Pow! Just like this:
This itsy bitsy CSS rule right here, my friend, will get rid of underlines for the hover state on all your links.
Rather than going all Rambo on your stylesheet, why not target a specific class? It's easier on the eyes and doesn't mess up your overall aesthetics.
This targets anchors within elements with the souped-up legend.green-color
class. How Neat is that?!
The nitty-gritty details
So, you've solved your problem, hurray! But let's not stop there. There's a whole lot more going on under the hood when it comes to CSS and anchor tags. Let me explain:
The chameleon effect
If those darn links of yours are not just underlining but also changing colors on hover, you might want to specify your preferred color to keep things in check:
Freestyling with inline styles
An inline style attribute on your anchor tags can be your knight in shining armor here. Don't believe me?
Now, before you start having all the fun with inline styles, remember, great power comes great responsibility. Inline styles can be, ahem, "fun" to maintain. Stick with external stylesheets, trust me.
Wrestling with CSS specificity
CSS is a tricky business. If your rules are not applying as expected, you might be dealing with CSS specificity. If that's the case, aim for more specific selectors or use a class to strike bullseye:
Visualization
Okay. Enough tech talk. Let's make things easier:
Before you hover, you have a nice wooden plank, static and simple (🟤). When you hover without our rule, you get a line (🖌️), an unwanted decoration. With our CSS rule, the hover is just your plain plank, no change:
[🟤 (normal)] ➡️ [🟤🖌️ (hover)] ➡️ [🟤 (hover with rule)]
You defeated the alien invader (🖌️), good job soldier!
Extra tricks up your sleeve
Let's consider more advanced scenarios; because why not?
Creating custom underlines with pseudo-elements
You're not a fan of ordinary? Well, who's stopping you from creating your own underlines with ::before
or ::after
pseudo-elements:
Accessibility matters
When modifying hover states, remember not everyone sees the world in the same colors as you do. Do them a favor:
- Use more drastic visual changes than color changes.
- Even if you say bye to underline, differentiate links from text via formatting (size, style, etc.)
Give them a smooth transition
Add some smoothness to the hover transformation using text-decoration-color
and transition
:
Feeling better about our changes?
Was this article helpful?