Enhancing Styles with User Action Pseudo-Classes
User action pseudo-classes allow you to style elements based on how users interact with them. They are commonly applied to <button> and <a> elements to provide feedback during interactions.
The syntax is as follows:
selector:pseudo-class {
property: value;
}
selectorcan be any selector we considered in the previous chapters;pseudo-classneeds:before its declaration, and we do not add any space.
We will consider the most helpful state pseudo-classes (hover, focus, active and visited).
:hover
Triggers when a user points to an element with a mouse cursor. Great for creating interactive visual feedback.
index.html
styles.css
:focus
Activates when an element receives keyboard focus (usually via the Tab key). To ensure accessibility, hover and focus styles are often paired, giving mouse and keyboard users a consistent experience.
The difference between :hover and :focus
:hoverreacts to mouse cursor activity;:focusreacts to the keyboard activity ("Tab" key).
In the following example, we have the same element with different pseudo-classes. Please pay attention to the styles.css file. We can notice that we can add the same styles for the hover and focus effect by separating the selector and pseudo-class with ,.
index.html
styles.css
:active
Triggered while an element is being activated, commonly during a mouse click on buttons or links.
index.html
styles.css
:visited
Applies to links after the user has already visited their destination. Browsers typically display unvisited links in blue and visited ones in purple, unless you override these styles.
index.html
styles.css
1. Which pseudo-class is triggered when a user hovers their mouse over an element?
2. Which pseudo-class is applied to a link that has already been visited?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about how to use :hover and :focus together for accessibility?
What are some best practices for styling navigation links with pseudo-classes?
Could you show examples of using :active and :visited with links?
Awesome!
Completion rate improved to 2.56
Enhancing Styles with User Action Pseudo-Classes
Swipe to show menu
User action pseudo-classes allow you to style elements based on how users interact with them. They are commonly applied to <button> and <a> elements to provide feedback during interactions.
The syntax is as follows:
selector:pseudo-class {
property: value;
}
selectorcan be any selector we considered in the previous chapters;pseudo-classneeds:before its declaration, and we do not add any space.
We will consider the most helpful state pseudo-classes (hover, focus, active and visited).
:hover
Triggers when a user points to an element with a mouse cursor. Great for creating interactive visual feedback.
index.html
styles.css
:focus
Activates when an element receives keyboard focus (usually via the Tab key). To ensure accessibility, hover and focus styles are often paired, giving mouse and keyboard users a consistent experience.
The difference between :hover and :focus
:hoverreacts to mouse cursor activity;:focusreacts to the keyboard activity ("Tab" key).
In the following example, we have the same element with different pseudo-classes. Please pay attention to the styles.css file. We can notice that we can add the same styles for the hover and focus effect by separating the selector and pseudo-class with ,.
index.html
styles.css
:active
Triggered while an element is being activated, commonly during a mouse click on buttons or links.
index.html
styles.css
:visited
Applies to links after the user has already visited their destination. Browsers typically display unvisited links in blue and visited ones in purple, unless you override these styles.
index.html
styles.css
1. Which pseudo-class is triggered when a user hovers their mouse over an element?
2. Which pseudo-class is applied to a link that has already been visited?
Thanks for your feedback!