Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS @media hover Property

Posted in CSS Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 22 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
CSS @media hover Property

Photo Credit to CodeToFun

πŸ™‹ Introduction

The @media (hover) property in CSS is part of media queries and is used to detect whether the user’s input device supports hover capability.

This can be useful in determining how to style interactive elements like buttons and links, based on whether a device can hover or not. For example, desktop computers typically support hover, while touch devices like phones usually do not.

πŸ’‘ Syntax

The @media (hover) property uses the following basic syntax:

Syntax
Copied
Copy To Clipboard
@media (hover: hover) {
  /* Styles for devices that support hover */
}

@media (hover: none) {
  /* Styles for devices that do not support hover */
}
  • hover: hover: Targets devices that support hover (like desktops).
  • hover: none: Targets devices that don’t support hover (like touchscreens).

🏠 Property Values

  • hover: Applies styles to devices that support hover interactions.
  • none: Applies styles to devices that do not support hover interactions.

πŸŽ›οΈ Default Value

The @media (hover) feature does not have a default value since it depends on the user’s device and its input capabilities.

πŸ“ Example Usage

πŸ“œ Example 1: Changing Button Style Based on Hover Support

In this example, the background color of the button will change to blue only if the device supports hover (like on desktops). On touch devices (without hover), the background will stay green.

example.css
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS @media hover Example</title>
  <style>
    button {
      padding: 10px 20px;
      background-color: green;
      color: white;
      border: none;
      cursor: pointer;
    }

    @media (hover: hover) {
      button:hover {
        background-color: blue;
      }
    }

    @media (hover: none) {
      button {
        background-color: green; /* Ensures consistent style on non-hover devices */
      }
    }
  </style>
</head>
<body>
  <h1>Button with Hover Media Query</h1>
  <button>Hover me!</button>
</body>
</html>

πŸ–₯️ Browser Compatibility

The @media (hover) property is supported by most modern browsers including Chrome, Firefox, Safari, Edge, and Opera. It's advisable to test for compatibility across different devices (desktops, tablets, phones) to ensure that the feature behaves as expected.

πŸŽ‰ Conclusion

The CSS @media (hover) property is a valuable tool when designing responsive and interactive user interfaces. It allows you to detect whether a device can hover and apply styles accordingly. This is especially useful when designing for both desktop and touch devices, ensuring an optimal user experience across different platforms.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy