Pseudo-class
A CSS pseudo-class
applies styles to a web page element based on a specific state (e.g., click, focus).
Pseudo-classes are used by appending a colon (:
) to a selector, with :hover
being one of the most commonly used examples.
The hover pseudo-class allows you to define a style for when the mouse cursor hovers over an HTML element.
Check out the example below that uses the :hover
pseudo-class.
<html>
<body>
<button class="my-button">Hover Me</button>
</body>
</html>
CSS
.my-button {
display: inline-block;
padding: 10px 20px;
background-color: #0000ff;
color: #fff;
border-radius: 5px;
transition: background-color 0.3s ease;
border: none;
}
.my-button:hover {
background-color: #ff0000;
}
This CSS code styles a button (<button>
) element with the .my-button
class.
The my-button class uses the .my-button:hover
pseudo-class to change the background color to red when the mouse cursor hovers over the button.
Notably, the transition
property adds a smooth animation to property changes.
In this example, a 0.3-second transition effect is applied to the background-color change, making the button's color change smoothly.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.