Links or hyperlinks are an essential part of a website. It allows visitors to navigate through the site. Therefore styling the links properly is an important aspect of building a user-friendly website.
See the tutorial on HTML links to learn more about links and how to create them.
A link has four different states — link, visited, active and hover. These four states of a link can be styled differently through using the following anchor pseudo-class selectors.
You can specify any CSS property you'd like e.g. color, font, background, border, etc. to each of these selectors to customize the style of links, just like you do with the normal text.
The order in which you are setting the style for different states of links is important, because what defines last takes precedence over the style rules defined earlier.
In all major web browsers such as Chrome, Firefox, Safari, etc. links on the web pages have underlines and uses the browser's default link colors, if you do not set the styles exclusively for them.
By default, text links will appear as follow in most of the browsers:
However, there is no change in the appearance of link in case of the hover state. It remains blue, purple or red depending on which state (i.e. unvisited, visited or active) they are in.
Now let's see how to customize the links by overriding its default styling.
Simply use the CSS color property to define the color of your choice for different state of a link. But when choosing colors make sure that user can clearly differentiate between normal text and links.
Let's try out the following example to understand how it basically works:
If you don't like the default underline on links, you can simply use the CSS text-decoration property to get rid of it. Alternatively, you can apply other styling on links like background color, bottom border, bold font, etc. to make it stand out from the normal text a little better.
The following example shows how to remove or set underline for different states of a link.
You can also make your ordinary text links look like button using CSS. To do this we need to utilize few more CSS properties such as background-color, border, display, padding, etc. You will learn about these properties in detail in upcoming chapters.
Let's check out the following example and see how it really works: