The different CSS properties that can be used to style links include:
- color: sets the color of the link text
- text-decoration: sets or removes an underline from the link text
- font-weight: sets the boldness of the link text
- background-color: sets the background color of the link
- padding: sets the amount of space between the link text and its container
- margin: sets the amount of space outside the link container
- :hover, :active, and :visited pseudo-classes: allow you to change the link’s appearance when the user interacts with it, such as when they hover over it or click it
You can also use CSS transition properties to animate the changes made by the :hover, :active, and :visited pseudo-classes.
Text Decoration
You can use the text-decoration property along with the pseudo-classes :link, :visited, :hover, and :active to style links in different states.
For example, to remove the underline from unvisited links, you can use the following CSS:
a:link {
text-decoration: none;
}
To add an underline to visited links, you can use the following CSS:
a:visited {
text-decoration: underline;
}
To change the color of the underline when the user hovers over a link, you can use the following CSS:
a:hover {
text-decoration: underline;
text-decoration-color: red;
}
To change the color of the underline when the user clicks a link, you can use the following CSS:
a:active {
text-decoration: underline;
text-decoration-color: green;
}
It’s also possible to combine multiple styles for one state like this:
a:hover {
text-decoration: underline wavy blue;
text-decoration-thickness:3px
}
It’s also possible to animate the text-decoration using CSS transition properties.
Background Color
You can use the background-color property along with the pseudo-classes :link, :visited, :hover, and :active to style links in different states.
For example, to set the background color of unvisited links to blue, you can use the following CSS:
a:link {
background-color: blue;
}
To set the background color of visited links to red, you can use the following CSS:
a:visited {
background-color: red;
}
To change the background color of a link when the user hovers over it, you can use the following CSS:
a:hover {
background-color: yellow;
}
To change the background color of a link when the user clicks it, you can use the following CSS:
a:active {
background-color: green;
}
It’s also possible to animate the background-color using CSS transition properties. You can also use the background shorthand properties to set multiple background properties at once.
a:hover {
background: linear-gradient(to right, red, yellow);
}
It’s important to note that the order of these styles in the CSS file can affect the resulting styles, so make sure that the styles for visited links come after the styles for unvisited links, and that the styles for active links come after the styles for hover links.
Link as a Button
To style a link as a button, you can use a combination of CSS properties along with the pseudo-classes :link, :visited, :hover, and :active.
Here’s an example of how you can style a link as a button using CSS:
a {
padding: 10px 15px; /* Add padding to the link */
border-radius: 5px; /* Add rounded corners to the link */
text-decoration: none; /* Remove the underline */
background-color: blue; /* Set the background color */
color: white; /* Set the text color */
font-weight: bold; /* Make the text bold */
transition: background-color 0.2s ease; /* Add a transition for the background color */
}
/* Change the background color on hover */
a:hover {
background-color: darkblue;
}
/* Change the background color when the link is active */
a:active {
background-color: blue;
}
/* Change the background color when the link is visited */
a:visited {
background-color: purple;
}
You can further style the button by adding a border, changing the font-size, padding and margins, and using other CSS properties. Additionally, you can use the CSS box-sizing property to include the padding and border in the total width and height of the button.
a {
box-sizing: border-box;
}
It’s important to note that the :visited pseudo-class can be abused to track users, so it’s best practice to avoid using it.