0% completed
Pseudo-classes let you style elements based on their state. With pseudo-classes, you can change the style of an element when the user interacts with it. In this lesson, we use pseudo-classes such as :hover
, :active
, and :focus
to add interactivity without using JavaScript.
Follow the code block below to use common pseudo-classes:
selector:hover { /* Styles for when the element is hovered */ } selector:active { /* Styles for when the element is clicked */ } selector:focus { /* Styles for when the element is focused */ }
Explanation:
:hover
pseudo-class is used to change the style when the mouse pointer is over an element.:active
pseudo-class changes the style during the moment an element is being clicked.:focus
pseudo-class is used to style elements when they have focus, for instance, when a text input is selected.In this example, the background of a button changes when the mouse hovers over it.
Explanation:
In this example, a link changes its color when clicked. The active state gives a quick visual cue when the user presses the link.
Explanation:
In this example, an input field gets a border when it receives focus. This helps users see which field is active for typing.
Explanation:
These examples show how pseudo-classes like :hover
, :active
, and :focus
add interactive effects to elements. They are simple yet powerful tools to enhance user experience without requiring extra scripts.
.....
.....
.....