Introduction to CSS Link Color
The advantage of using Cascading Style Sheet is that it offers every kind of styling. The user can customize how each element is styled, for maximum ease of use for the end-user. Customizing the behavior of a hyperlink is also offered in CSS. The developer can control how the link will behave using various pseudo-classes. Also, one can assign different colors, and the link color will change according to its usage. This styling helps the end-user segregate between various links, like, one which has not been clicked upon yet or the one which has already been clicked on. Let us have a look at some of the properties offered by CSS to customize and animate the hyperlinks on a page.
Usage and Examples of CSS Link Color
There are various states of a hyperlink. CSS offerspseudo classes for each of these states, which can be custom styles by developers according to the requirement of the web page. The above-mentioned states of a hyperlink are: link, visited, hover, active.The state ‘link’ refers to an untouched link, yet to be accessed. ‘Visited’ refers to a link, which has been accessed. ‘Hover’ is the state of the link, when the user will hover over the link. And finally ‘active’ refers to the state of the link when it is immediately clicked upon. Let us look at the example and see how each state can be styled for, and see its demonstration.
1. Demonstrating different colors for different link states, using External CSS
- Since we are using external CSS, we will start by creating the CSS file first.
- As stated above, we will be styling the hyperlink element and each state can be styled as a pseudo class. In other terms, we will style <a> element and each state will be defined as a class i.e. a:link, a:hover, a:visited, a:active.
- We will define the pseudo class, and define the styling for each. The coding for each class will look like this:
a:link{
font-style: normal;
color: blue;
font-family: Arial, Helvetica, sans-serif;
}
- The final CSS code will look as below:
a:link{
font-style: normal;
color: blue;
font-family: Arial, Helvetica, sans-serif;
}
a:hover{
font-style: italic;
color: darkgreen;
}
a:visited{
font-style: normal;
color: darkred;
font-family: Arial, Helvetica, sans-serif;
}
a:active{
font-style: italic;
color: yellow;
}
- Next, we will code for the HTML page. Since this is example uses external CSS, in the header section, we will call for the external CSS file.
- In the body section we will define the link, using the element <a>. The final HTML code will look as follows:
<html>
<head>
<title>Hyperlink Styling</title>
<link rel="stylesheet" type="text/css" href="link.css">
</head>
<body>
<h2>This page demonstrates the various states of hyperlink</h2>
<b><a href="https://www.google.com">This is a hyperlink</a></b>
<ol>
<li>If the link is not used, it will appear Blue and normal</li>
<li>If the link is hovered upon, it will appear Green and Italic </li>
<li>If the link has been used, it will appear Red and normal</li>
<li>If the link is active, it will be Yellow and Italic</li>
</ol>
</body>
</html>
- We can see the final outcome by saving this page and opening it through a browser.
- The link will initially appear to be blue, as in the first screenshot.
- It will change color to Green and italic when we will hover upon it.
- You can observe the color change to yellow, the exact moment the link is clicked.
- And in the final screenshot, you can see that the link, once visited, turns into a dark red color.
2. Styling hyperlink using Internal CSS
- In this example, we will demonstrate the styling for hyperlink, using internal CSS.
- Since this example uses internal CSS, we will directly code for the HTML page.
- In the header section, we will define the styling for each state of the link (as a pseudo class), within the <style> element. The head section will be coded as below:
<head>
<title>Hyperlink Styling</title>
<style>
a:link{
font-style: normal;
color: blue;
font-family: Arial, Helvetica, sans-serif;
}
a:hover{
font-style: italic;
color: darkgreen;
}
a:visited{
font-style: normal;
color: darkred;
font-family: Arial, Helvetica, sans-serif;
}
a:active{
font-style: italic;
color: yellow;
}
</style>
</head>
- Now, in the body section, similar to the above example, we will make use of the element <a> to demonstrate the usage of each state of the link that has been styled. The final HTML code will should look as below:
<html>
<head>
<title>Hyperlink Styling</title>
<style>
a:link{
font-style: normal;
color: blue;
font-family: Arial, Helvetica, sans-serif;
}
a:hover{
font-style: italic;
color: darkgreen;
}
a:visited{
font-style: normal;
color: darkred;
font-family: Arial, Helvetica, sans-serif;
}
a:active{
font-style: italic;
color: yellow;
}
</style>
</head>
<body>
<h2>This page demonstrates the various states of hyperlink</h2>
<b><a href="https://www.google.com">This is a hyperlink</a></b>
<ol>
<li>If the link is not used, it will appear Blue and normal</li>
<li>If the link is hovered upon, it will appear Green and Italic </li>
<li>If the link has been used, it will appear Red and normal</li>
<li>If the link is active, it will be Yellow and Italic</li>
</ol>
</body>
</html>
- Saving this page and opening it through a browser will help us in seeing the outcome similar to the below screenshots.
- The link will initially appear to be blue, as in the first screenshot.
- It will change color to Green and italic when we will hover upon it.
- You can observe the color change to yellow, the exact moment the link is clicked.
- And in the final screenshot, you can see that the link, once visited, turns into a dark red color.
Conclusion
Hence the above examples helped us in seeing how different colors and states can be used in styling the links, which can make it easier for the end users to use the links on a page. The developers can use these states of the link as required by the page. Also, this is just one of the few options on how links can be stylized.
Recommended Articles
This is a guide to CSS Link Color. Here we also discuss the introduction and usage of css link color along with a different example and its code implementation. You may also have a look at the following articles to learn more –