Pseudo-Classes in CSS
Posted By: Rimjhim Jain Published: 14, Jan 2024

To Specify the special state of the selected elements we use Pseudo-class keywords in CSS. We can combine these keywords with a CSS selector to add an effect to existing elements based on their states. For Example, how our element will behave when we hover over it or how the property of elements changes when we click them or just visit them.
The syntax for pseudo-classes is:
selector: pseudo-class {
property: value;
}
There are many pseudo-classes in CSS but the most commonly used are:
- :hover - The :hover pseudo-class is applied when the user hover it's mouse pointer over the element. An example of using the :hover pseudo-class on a element is:
div:hover {
background-color: red;
}
It will change the color of div element to red whenever user will hover over it.
a:link {
color: green;
}
The link which is not visited till yet will have green color.
a:visited {
color: purple;
}
The link which is already visited by user will have purple color.
button:visited {
color: pink;
}
When we click(active) on this button then it's color will change to pink.
p:lang(eng) {
background-color: orange;
}
//here the term in "( )"is the language attribute on which you want to style.
The background color of text of your paragraph whose language is eng(English) will become orange.
p:first-child {
background-color: teal;
}
The background color of first child of the paragraph will change to teal.
input:focus {
background-color: gray;
}
The background color of input element will change to gray when we focus on it(like by clicking on it).