How to Style Links in CSS ?
Last Updated :
10 Sep, 2024
Styling links in CSS is one of the fundamentals in web design that helps in creating an interactive and pleasant user experience through navigation. Links are core components for web pages, and using CSS, you can easily come up with the kind of look you want in terms of the overall appearance of the website or the brand.
In this article, we will see different techniques in which links can be styled through descriptions, syntax, and professional code examples.
Prerequisites
These are the approaches to style links in CSS:
1. Basic Link Styling
Basic link styling typically involves making links somewhat more consistent with the rest of your website design through an alteration in link color, font style, and maybe even removing the default underline.
Syntax:
a {
color: royalblue;
text-decoration: none;
font-weight: bold;
}
Example: This above example shows some basic styling of link in CSS having <a> content written with RoyalBlue color.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Basic Link Styling
| Shivam Mudaliar</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
border: 2px solid #dcdcdc;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba
(0, 0, 0, 0.1);
text-align: center;
font-family: 'Arial', sans-serif;
}
a {
color: royalblue;
text-decoration: none;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<a href="#">This is a styled
link by Shivam Mudaliar</a>
</div>
</body>
</html>
Output:
Basic Link Styling2. Hover Effects
Hover effects are visual responses on hovering over a link that is used to build interactivity. The common hover effects are based on the changes in color, addition of underlines, and background changes.
Syntax:
a {
color: royalblue;
text-decoration: none;
font-weight: bold; transition:
color 0.3s ease-in-out, background-color 0.3s ease-in-out;
}
a:hover {
color: crimson;
text-decoration: underline;
background-color: lightgray;
}
Example: This example explains how to apply the hover effects to link in CSS which when hovered changes the color of the text.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Hover Effects
| Shivam Mudaliar</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
border: 2px solid #dcdcdc;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba
(0, 0, 0, 0.1);
text-align: center;
font-family: 'Arial', sans-serif;
}
a {
color: royalblue;
text-decoration: none;
font-weight: bold;
transition: color 0.3s ease-in-out,
background-color 0.3s ease-in-out;
}
a:hover {
color: crimson;
text-decoration: underline;
background-color: lightgray;
}
</style>
</head>
<body>
<div class="container">
<a href="#">Hover over this link</a>
</div>
</body>
</html>
Output:
Hover effects styling3. Active Link Styling
Active link styling changes the look of a link while it is in the process of being clicked. It provides immediate feedback to the user that a link is being turned on.
Syntax:
a {
color: royalblue;
text-decoration: none;
font-weight: bold;
transition: color 0.2s ease-in-out, background-color 0.2s ease-in-out;
}
a:active {
color: seagreen;
text-decoration: underline;
background-color: lightyellow;
}
Example: This example explains how to style an active link using CSS which when hovered shows a line under text.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Active Link Styling
| GeeksForGeeks</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
border: 2px solid #dcdcdc;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba
(0, 0, 0, 0.1);
text-align: center;
font-family: 'Arial', sans-serif;
}
a {
color: royalblue;
text-decoration: none;
font-weight: bold;
transition: color 0.2s ease-in-out,
background-color 0.2s ease-in-out;
}
a:active {
color: seagreen;
text-decoration: underline;
background-color: lightyellow;
}
</style>
</head>
<body>
<div class="container">
<a href="#">Click this link</a>
</div>
</body>
</html>
Output:
Active Link Styling4. Visited Link Styling
Visited link styling is done to provide a different look to links that the user has visited so that the user can differentiate between a visited link and not visited.
Syntax:
a {
color: royalblue;
text-decoration: none;
font-weight: bold;
}
a:visited {
color: purple;
text-decoration: none;
}
Example: This example shows how to apply CSS styling to the visited link as before clicking the text color is RoyalBlue and once the text is clicked its color gets changed.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Visited Link Styling
| Shivam Mudaliar</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
border: 2px solid #dcdcdc;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba
(0, 0, 0, 0.1);
text-align: center;
font-family: 'Arial', sans-serif;
}
a {
color: royalblue;
text-decoration: none;
font-weight: bold;
}
a:visited {
color: purple;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<a href="#" id="dynamicLink">
Visit this link</a>
</div>
<script>
document.addEventListener
('DOMContentLoaded', () => {
const link =
document.getElementById('dynamicLink');
link.href = '#?' + new Date().getTime();
});
</script>
</body>
</html>
Output:
Visited Link Styling5. Custom Underline Styles
Links can be styled more fashionably by employing different underline styles, colors, and positions to create custom underline styles.
Syntax:
a {
color: royalblue;
text-decoration: none;
position: relative;
}
a::after {
content: '';
display: block;
width: 100%;
height: 2px;
background: royalblue;
position: absolute;
bottom: -2px;
left: 0;
}
Example: The example below shows how to apply custom underlining style for link in CSS having text in RoyalBlue color.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Custom Underline Styles
| Shivam Mudaliar</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
border: 2px solid #dcdcdc;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba
(0, 0, 0, 0.1);
text-align: center;
font-family: 'Arial', sans-serif;
}
a {
color: royalblue;
text-decoration: none;
position: relative;
}
a::after {
content: '';
display: block;
width: 100%;
height: 2px;
background: royalblue;
position: absolute;
bottom: -2px;
left: 0;
}
</style>
</head>
<body>
<div class="container">
<a href="#">Custom underline
style</a>
</div>
</body>
</html>
Output:
Custom underline StyleA good way to make links visible and appealing is to style them like buttons. In most cases, this is achieved by adding padding, background color, border radius, and hover effects.
Syntax:
a {
color: white;
background-color: royalblue;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
display: inline-block;
transition: background-color 0.3s ease-in-out;
}
a:hover {
background-color: crimson;
}
Example: The following example demonstrates how to make links appear as buttons using CSS.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Button-Like Links
| Shivam Mudaliar</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
border: 2px solid #dcdcdc;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba
(0, 0, 0, 0.1);
text-align: center;
font-family: 'Arial', sans-serif;
}
a {
color: white;
background-color: royalblue;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
display: inline-block;
transition:
background-color 0.3s ease-in-out;
}
a:hover {
background-color: green;
}
</style>
</head>
<body>
<div class="container">
<a href="#">Button-like link</a>
</div>
</body>
</html>
Output:
Button-Like Link Styling 7. Animated Link Styles
The Animated link styles are those that make the user's experience a bit more interesting by using dynamic visual effects, such as expanding underlines. These are achieved through the use of transitions and pseudo-elements in CSS.
Syntax:
a {
color: royalblue;
text-decoration: none;
position: relative;
font-family: 'Arial', sans-serif;
transition: color 0.3s ease-in-out;
}
a::after {
content: '';
display: block;
width: 0;
height: 2px;
background: crimson;
transition: width 0.3s;
position: absolute;
bottom: -2px;
left: 0;
}
a:hover {
color: crimson;
}
a:hover::after {
width: 100%;
}
Example: This example shows how to use animated type styling for link in CSS.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Animated Link Styles
| Shivam Mudaliar</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
border: 2px solid #dcdcdc;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba
(0, 0, 0, 0.1);
text-align: center;
font-family: 'Arial', sans-serif;
}
a {
color: royalblue;
text-decoration: none;
position: relative;
font-family: 'Arial', sans-serif;
transition: color 0.3s ease-in-out;
}
a::after {
content: '';
display: block;
width: 0;
height: 2px;
background: crimson;
transition: width 0.3s;
position: absolute;
bottom: -2px;
left: 0;
}
a:hover {
color: crimson;
}
a:hover::after {
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<a href="#">Hover to see
the animation</a>
</div>
</body>
</html>
Output:
Animated Link Styles
Similar Reads
How to resize list style image in CSS ?
In this article, we will learn to set an image or icon as the <li> image. We need to set the size of the custom list image to make it attractive. So, we will also learn the different ways to resize the custom list image. Syntax: In this article, we will use below CSS properties. background-ima
3 min read
How to Style an Active Link in VueJS ?
In single-page applications effective navigation becomes crucial. In this article, we'll explore how to style active links in Vue.js using the popular routing library, Vue-router. Table of Content Using router-link-exact-active classConditional class bindingSteps to Setup the Project EnvironmentStep
4 min read
How to italicize text in CSS ?
Here are the approaches to italicize text in CSS: Using the font-style PropertyThe CSS font-style property is the most straightforward way to italicize text. [GFGTABS] HTML <html> <head> <style> .italic-text { font-style: italic; } </style> </head> <body> <p cl
2 min read
How to apply inline CSS ?
In this article we will learn how to apply inline CSS, Inline CSS contains the CSS property in the body section attached to the element is known as inline CSS. This kind of style is specified within an HTML tag using the style attribute. It is used to apply a unique style to a single HTML element. S
1 min read
How CSS style overriding works ?
In this article, we are going to learn how CSS style overriding works. Cascading Style Sheets referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this
3 min read
CSS :link Selector
The :link is used to target the unvisited link for styling not the links which is already visited. For styling on visited use ":visited", for styling on click use ":active" and for styling move mouse on link use ":hover". Syntax: :link { //property}Example: [GFGTABS] HTML <!DOCTYPE html> <h
2 min read
How to Link a CSS File to HTML?
To link a CSS file to an HTML file, Create a separate CSS file (styles.css) and write styles on it. Now we need to use the <link> element inside the <head> section of the HTML file to attach the CSS file. Syntax:<link rel="stylesheet" href="styles.css">rel="stylesheet": It specifie
3 min read
How to change link color in CSS ?
To change the link color in CSS, use the color property with the anchor (<a>) tag, and different pseudo-classes for various states. HTML Links are added by using the anchor tag. It creates the link to navigate to another webpage from the current page. Note: The default HTML links are in blue c
2 min read
How to Remove Underline from Links in CSS?
The text-decoration property is used to remove the underline from links in CSS. You can use the below syntax on anchor element to remove underline from link. Syntax a { text-decoration: none;}It is very basic to remove underline from link with text-decoration property. The none value removes the und
1 min read
How to style a href links in React using Tailwind CSS ?
In this article, we will learn how we can style a href link in Reactjs. Since we use Tailwind CSS, Tailwind CSS describes itself as the first applicable CSS framework. Rather than focusing on the performance of a style object, Tailwind focuses on how it should be displayed. This makes it easier for
2 min read