:active

The :active pseudo-class targets elements that are currently being interacted with by the user through a pointing device (e.g., mouse, touchpad). This typically occurs when the user is pressing down on an element but hasn’t yet released the button.

Usage

  1. Target an Element:
    • Select the specific element you want to style when it’s active. This can be any HTML element, such as a <button>, <a>, <input>, etc.
  2. Apply Styles:
    • Inside the curly braces following the selector, define the CSS properties and their values that you want to apply to the element while it’s active.

Example

button:active {
  background-color: blue;
  color: white;
}

In this example, the button element will have a blue background and white text while it’s being clicked or pressed down.

Common Use Cases

  • Buttons: Changing the background color or border style to indicate that the button is currently being pressed.
  • Links: Underlining the link or changing its color to visually emphasize that it’s about to be clicked.
  • Interactive Elements: Applying specific styles to elements that are being interacted with, such as highlighting a selected option in a dropdown menu.

Additional Considerations

  • Specificity: Be mindful of specificity when using :active in conjunction with other selectors. More specific selectors will override less specific ones.
  • Mobile Devices: While :active works on both desktop and mobile devices, the behavior might differ slightly due to touchscreens and different interaction methods.
  • Accessibility: Ensure that the styles applied to :active elements are accessible to users with disabilities. Consider using color contrast and providing alternative visual cues.