Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Enhancing Styles with User Action Pseudo-Classes | Getting Started with CSS
CSS Fundamentals

bookEnhancing 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;
}
  • selector can be any selector we considered in the previous chapters;
  • pseudo-class needs : before its declaration, and we do not add any space.
Note
Note

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

index.html

styles.css

styles.css

copy

: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

  • :hover reacts to mouse cursor activity;
  • :focus reacts 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

index.html

styles.css

styles.css

copy

:active

Triggered while an element is being activated, commonly during a mouse click on buttons or links.

index.html

index.html

styles.css

styles.css

copy

: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

index.html

styles.css

styles.css

copy

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?

question mark

Which pseudo-class is triggered when a user hovers their mouse over an element?

Select the correct answer

question mark

Which pseudo-class is applied to a link that has already been visited?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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?

bookEnhancing 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;
}
  • selector can be any selector we considered in the previous chapters;
  • pseudo-class needs : before its declaration, and we do not add any space.
Note
Note

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

index.html

styles.css

styles.css

copy

: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

  • :hover reacts to mouse cursor activity;
  • :focus reacts 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

index.html

styles.css

styles.css

copy

:active

Triggered while an element is being activated, commonly during a mouse click on buttons or links.

index.html

index.html

styles.css

styles.css

copy

: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

index.html

styles.css

styles.css

copy

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?

question mark

Which pseudo-class is triggered when a user hovers their mouse over an element?

Select the correct answer

question mark

Which pseudo-class is applied to a link that has already been visited?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
some-alt