:hover

The :hover pseudo-class is used to style elements when the user’s mouse pointer is hovering over them. This is often used to create interactive effects, such as changing the color, opacity, or border of an element when it is hovered over.

Syntax

selector:hover {
  /* CSS properties */
}
  • Selector: The element you want to style when hovered over.
  • CSS properties: The styles you want to apply to the element when it is hovered over.

Example

a:hover {
  color: blue;
  text-decoration: underline;
}

This example styles all <a> elements to have a blue color and an underlined text decoration when the user hovers over them.

Key Points

  • The :hover pseudo-class only applies to elements that can be hovered over, such as links, buttons, and other interactive elements.
  • The :hover pseudo-class is typically used in conjunction with other pseudo-classes, such as :link and :visited, to create more complex styling effects.
  • The :hover pseudo-class is not supported in all browsers or devices. However, it is widely supported in modern browsers.

Additional Tips

  • To make your hover effects more accessible, consider using a contrasting color or a different visual cue that is easy to see for users with visual impairments.
  • You can use CSS transitions to create smooth animations when hovering over elements.
  • For more advanced hover effects, you can use JavaScript to dynamically modify the styles of elements based on user interactions.