Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS @media pointer Property

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

Photo Credit to CodeToFun

πŸ™‹ Introduction

The @media rule in CSS allows developers to apply styles based on the characteristics of the device or viewport, such as its width, height, or resolution.

The pointer media feature within the @media rule provides information about the user's primary input mechanism. This can be useful for tailoring the user interface based on whether the user is using a mouse, touch screen, or other pointing device.

πŸ’‘ Syntax

The @media pointer feature can be used within media queries to apply styles based on the type of pointer device. It typically takes one of the following values:

Syntax
Copied
Copy To Clipboard
@media (pointer: value) {
  /* CSS rules here */
}

🏠 Property Values

  • none: Indicates that no pointing device is available, which may be relevant for devices that rely solely on keyboard input or other non-pointer interfaces.
  • coarse: Indicates a pointing device with limited accuracy, such as a touch screen. This value is typically used to apply styles suited for touch interactions.
  • fine: Indicates a pointing device with high accuracy, such as a mouse or trackpad. This value is used for devices that support precise pointing and clicking.

πŸŽ›οΈ Default Value

The @media pointer feature does not have a default value in the CSS specification. Instead, it is based on the type of device being used to access the website. If no media query is applied, the default behavior will depend on the device's input method.

πŸ“ Example Usage

πŸ“œ Using pointer: coarse for Touch Screens

In this example, we'll apply different styles for touch screens to improve usability on devices with coarse pointers.

example.scss
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 pointer Example</title>
  <style>
    /* Styles for all devices */
    button {
      padding: 10px 20px;
      font-size: 16px;
    }
    
    /* Styles specifically for touch screens (coarse pointer) */
    @media (pointer: coarse) {
      button {
        padding: 20px 40px;
        font-size: 18px;
      }
    }
    
    /* Styles specifically for devices with a fine pointer (e.g., mouse) */
    @media (pointer: fine) {
      button:hover {
        background-color: #007bff;
        color: white;
      }
    }
  </style>
</head>
<body>
  <h1>Button with Different Styles Based on Pointer Device</h1>
  <button>Click Me</button>
</body>
</html>

πŸ–₯️ Browser Compatibility

The pointer media feature is supported in most modern browsers. It’s a good idea to test your website across different devices and browsers to ensure that your styles work as intended for all users.

πŸŽ‰ Conclusion

The @media pointer property is a valuable tool for creating responsive designs that adapt to different input methods.

By using this feature, you can enhance user experience by tailoring your styles to accommodate the precision of the user's pointing device. Experiment with different pointer values to see how they can improve the accessibility and usability of your web projects.

πŸ‘¨β€πŸ’» 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