How to Change the Color of Link on Hover using CSS ?
Last Updated :
20 Mar, 2024
Changing the color of a link on hover can provide visual feedback to users, indicating that the link is interactive. It improves the user experience by helping users understand that the link is clickable.
These are the following approaches:
Using CSS pseudo-class
CSS provides the: hover pseudo-class which allows us to specify styles that should apply when the mouse is over an element, such as a link (<a> tag).
Syntax:
selector:hover {
/* styles to apply on hover */
}
Example: The below code uses: hover pseudo-class to change the Color of the Link on Hovers.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Change Link Color on Hover</title>
<link rel="stylesheet"
href="styles.css">
</head>
<body>
<div class="container">
<h1>GeeksForGeeks</h1>
<h2>Changing Link color on Hovers
using pseudo-class</h2>
<a href="#">Machine Learning</a><br>
<a href="#">Data Science</a><br>
<a href="#">Blockchain</a>
</div>
</body>
</html>
CSS
/* styles.css */
.container {
text-align: center;
}
h1 {
color: green;
}
a {
color: blue;
text-decoration: none;
}
a:hover {
color: green;
text-decoration: underline;
}
Output:

Using CSS Variables
In this approach, we're using CSS variables to define the default color of the link and the color it should change to on hover.
Example: The below example uses CSS variables to define a link of color.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<link rel="stylesheet"
href="styles.css">
</head>
<body>
<div class="container">
<h1>GeeksForGeeks</h1>
<h3>Changing link color Using CSS Variables</h3>
<a href="#" class="custom-link">Hover over me</a>
</div>
</body>
</html>
CSS
.container {
text-align: center;
}
h1 {
color: green;
}
:root {
--default-color: blue;
--hover-color: red;
}
.custom-link {
color: var(--default-color);
text-decoration: none;
}
.custom-link:hover {
color: var(--hover-color);
}
Output:

Using Inline CSS
This approach uses inline CSS and HTML event attributes to change the color of a link on hover.
Example: The below example shows how we can add inline CSS to change the link color on hover.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Link Color Change on Hover</title>
</head>
<body>
<div class="container"
style="text-align: center;">
<h1 style="color: green;">GeeksForGeeks</h1>
<h3>Changing link color Using inline CSS</h3>
<a href="#"
onmouseover="this.style.color='green'"
onmouseout="this.style.color='blue'"
style="color: blue;">Visit GeeksForGeeks
</a>
</div>
</body>
</html>
Output:

Similar Reads
How to Change the Border Color on Hover in CSS ? Border color in CSS defines the color of an elementâs border. You can change the border color on hover using the :hover pseudo-class, allowing you to modify the borderâs appearance when a user hovers over the element. Using CSS hover Pseudo ClassThe hover pseudo-class in CSS is a powerful tool that
1 min read
How to Change the Item Color of List on Hover ? In CSS, we can customize the interaction with the list element by changing the color of the list item on hover. We can also apply the different colors to each item using the :hover pseudo class in CSS. Approach: Using the :hover Pseudo-classIn this approach, we are using the :hover pseudo-class in C
2 min read
How to Change the Color of Bullets using CSS? Changing the color of bullets using CSS means styling the bullet points in a list (<ul> or <ol>) to have a different color than the text. This can be done using pseudo-elements or setting the color property on the list item, enhancing design consistency.1. Adding An Extra MarkupBy adding
2 min read
How to change the underline color in CSS? In this article, we are going to learn how to change the underline color in CSS, To change the underline color in CSS, use the text-decoration-color property. Set it to the desired color value. Styling is implemented in HTML text to make it catchy and attractive. The text can be made italic, underli
1 min read
How To Change Color Of SVG On Hover? SVG stands for Scalable Vector Graphics which is an XML form approach to create graphical elements using the code format of XML, that is, Extensive Markup Language. We can use CSS elements to add colors to svg, or change its properties and even add pseudo-class effects to it. In this article, we wil
7 min read