askvity

How to Make Links Not Blue in CSS?

Published in CSS Link Styling 4 mins read

To make links not blue in CSS, you primarily use the color property to set your desired hue, typically for various link states (unvisited, visited, hover, active). Additionally, browsers commonly add an underline to links, which can also be customized.

Understanding Default Link Styles

By default, web browsers apply specific styles to hyperlinks (<a> tags) to distinguish them. As noted in a blog post from November 12, 2022, browsers typically add an underline to links, and also apply blue and purple colors: blue for unvisited links and purple if you have previously visited the page. These default styles help users identify clickable elements but can sometimes clash with a website's design.

Changing Link Color

To change the default blue or purple color of links, you'll target the <a> element and its pseudo-classes using the color property. It's crucial to style all link states to maintain a consistent user experience.

CSS Properties for Link Colors

Here's how to change the color for different link states:

  • a:link: Styles an unvisited link.
  • a:visited: Styles a link that the user has already visited.
  • a:hover: Styles a link when the user's mouse cursor is over it.
  • a:active: Styles a link at the moment it is clicked.

Example CSS Code:

/* Styling for all unvisited links */
a:link {
    color: #ff5733; /* Example: A shade of orange-red */
}

/* Styling for all visited links */
a:visited {
    color: #c70039; /* Example: A deeper red */
}

/* Styling when hovering over a link */
a:hover {
    color: #900c3f; /* Example: A darker red/purple */
}

/* Styling when a link is being clicked */
a:active {
    color: #581845; /* Example: A deep purple */
}

Tip: The order of these pseudo-classes matters for proper styling. A common mnemonic to remember the correct order is LoVe Hate And Respect (Link, Visited, Hover, Active).

Removing the Default Underline

Beyond color, browsers also add an underline to links by default. To remove this, you use the text-decoration property.

As highlighted by a blog post from November 12, 2022, to remove the underline from a link with CSS, you should add text-decoration: none to your code. This property can be applied to any or all of the link states, depending on your design preference.

Example CSS Code:

/* Removes underline from all link states */
a {
    text-decoration: none;
}

/* Or remove it for specific states, e.g., only on hover */
a:hover {
    text-decoration: underline; /* Adds underline only on hover */
}

Comprehensive Link Styling Example

For a cohesive look, it's best to combine color and text-decoration properties for all link states.

Link State Description Example CSS Property
:link Default style for unvisited links color: #007bff;
:visited Style for links that have been visited color: #6610f2;
:hover Style when mouse cursor is over the link color: #0056b3;
:active Style when the link is being clicked color: #004085;
a General styling (e.g., remove all underlines) text-decoration: none;

Combined CSS Snippet:

a:link {
    color: #007bff; /* Blue */
    text-decoration: none; /* No underline */
}

a:visited {
    color: #6610f2; /* Purple */
    text-decoration: none;
}

a:hover {
    color: #0056b3; /* Darker blue on hover */
    text-decoration: underline; /* Add underline on hover for emphasis */
}

a:active {
    color: #004085; /* Even darker blue when clicked */
    text-decoration: underline;
}

Practical Considerations

  • Accessibility: While removing underlines and changing colors can enhance design, ensure that links remain clearly distinguishable from regular text, especially for users with visual impairments. Using a hover effect that adds an underline or changes the background color can improve usability.
  • Consistency: Apply consistent link styling across your entire website for a professional and user-friendly experience.
  • Specificity: Be aware of CSS specificity. If your link styles aren't applying, another CSS rule might be overriding them. More specific selectors (e.g., .nav-link:link vs. a:link) will take precedence.

Related Articles