Using Hover Effects Responsively: Alternatives and Code Examples


Media Queries in CSS

  • These characteristics can include things like screen size, resolution, orientation, device type (phone, tablet, desktop), and even pointer capabilities.
  • Media queries are a feature in CSS that allows you to apply styles conditionally based on specific characteristics of the device or environment where the webpage is being viewed.

@media.hover

  • Conversely, touchscreens on mobile devices typically lack precise hovering ability.
  • On devices with a mouse or trackpad, hovering is generally possible because the cursor can be moved and positioned over elements.
  • Specifically, "@media.hover" targets the hover capability of the primary input device.

Values of "@media.hover"

  • The media query "@media.hover" can have two values:
    • hover: none - This applies styles when the device cannot hover or hovering is inconvenient, like on most touchscreens.
    • hover: hover (the default value) - This applies styles when the device has a hover capability, typically desktops and laptops.

Example Usage

/* Style elements only when the device can hover */
@media (hover: hover) {
  .myButton:hover {
    background-color: blue;
  }
  .myMenu:hover {
    display: block;
  }
}

/* Style elements differently for touch devices */
@media (hover: none) {
  .myButton {
    background-color: green;
  }
  .myMenu {
    display: flex;
  }
}

In this example, the button will change its background color to blue on hover for devices with a mouse, while it will have a green background color for touch devices. Similarly, the menu will only display when hovered over on desktops but will always be displayed on touch devices (potentially with a different layout using flex).

  • For more advanced hover detection with touch devices, you can explore the any-pointer media query which provides different values for coarse touch (like fingers) and fine touch (like pens).
  • While "@media.hover" is a valid syntax, browser support for it might be limited. It's generally recommended to use the more widely supported any-hover media query which offers the same functionality.


Using "@media.hover" for hover effects

/* Show dropdown menu on hover for desktops */
@media (hover: hover) {
  .dropdown {
    display: inline-block;
  }

  .dropdown:hover .dropdown-content {
    display: block;
  }
}

/* Style dropdown menu differently for touch devices (always visible) */
@media (hover: none) {
  .dropdown {
    display: block;
  }

  .dropdown-content {
    display: none;
  }

  .dropdown:active .dropdown-content { /* Show on touch */
    display: block;
  }
}

This code creates a dropdown menu that appears on hover for desktops and laptops. On touch devices, the menu is always hidden but can be revealed by tapping the dropdown element (using the :active pseudo-class).

Using "any-hover" for broader hover detection

/* Apply hover styles when any hovering method is available */
@media (any-hover: hover) {
  .product-card:hover {
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  }
}

/* Optional styles for touch devices (no hover effect) */
@media (any-hover: none) {
  .product-card {
    opacity: 0.8; /* Subtle style for touch */
  }
}

Using "any-pointer" for advanced hover detection

/* Apply hover styles for precise input (mouse/pen) */
@media (any-pointer: fine) {
  .image-container:hover {
    opacity: 0.8;
  }
}

/* Different styles for coarse touch input (fingers) */
@media (any-pointer: coarse) {
  .image-container {
    opacity: 1;
  }
}

This code utilizes any-pointer to differentiate between precise hovering (like a mouse or pen) and coarse touch interaction (like fingers). Images will have a slight opacity reduction on hover for precise input devices, while maintaining full opacity for touch.



    • This is the most widely supported alternative to "@media.hover".

    • It checks for any type of hovering capability, including mouse, pen, or other precise pointing devices.

    • Syntax:

      @media (any-hover: hover) { /* Styles for devices with hover */ }
      @media (any-hover: none) { /* Styles for devices without hover */ }
      
  1. Focus States and Keyboard Navigation

    • This approach focuses on keyboard accessibility, making elements visually distinct when focused using the Tab key.
    • It's important for a well-rounded user experience, especially on touch devices.
    • Use the :focus pseudo-class to style elements when they receive focus.
  2. "prefers-reduced-motion" Media Query

    • While not directly related to hover, this media query can be helpful for touch devices where rapid hover effects might be undesirable.

    • It allows you to adjust styles for users who prefer reduced motion animations.

    • @media (prefers-reduced-motion: reduce) {
        .myElement {
          transition: none; /* Disable animation */
        }
      }
      

Choosing the Right Alternative

  • Use prefers-reduced-motion for a smoother user experience on touch devices with motion preferences.
  • Always prioritize accessibility by including focus states.
  • For more granular control and touch-specific styling, consider JavaScript or a combination of approaches.
  • For basic hover detection with good browser support, use any-hover.