Table of contents
1.
Introduction
2.
Syntax
3.
Description
4.
Formal Definition
4.1.
Formal Syntax
5.
Property Values  
5.1.
`none`  
5.2.
 `auto`  
5.3.
Example - Using `none` Value  
6.
Examples
6.1.
Example 1: Default Pointer Event Behavior
6.2.
Example 2: Using pointer-events: none
6.3.
Example 3: Using pointer-events with SVG
7.
Browser Compatibility
8.
Frequently Asked Questions
8.1.
What happens when I set pointer-events to none? 
8.2.
Can I use pointer-events with non-interactive elements? 
8.3.
Are there performance issues with pointer-events? 
9.
Conclusion
Last Updated: Jan 25, 2025
Medium

CSS pointer-events property

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The CSS pointer-events property is a useful feature in web development that helps control how elements react to user actions like clicking, touching, or hovering. This property is often used to disable interactions with an element or let interactions pass through to other elements underneath it. It plays a key role in creating interactive layouts and improving user experience in web designs.

CSS pointer-events property

In this article, you will learn about the syntax of the pointer-events property, its different values, and how to use it step by step to make your web pages more user-friendly and efficient.

Syntax

These can control whether an element reacts to events like mouse clicks, hover effects, and pointer movements.

element {
  pointer-events: value;
}


The pointer-events property accepts several values, each with its own behavior. These values are:

  • auto (default): The element behaves as it normally would, responding to pointer events.
     
  • none: The element will not receive pointer events, making it "invisible" to mouse or touch interactions.
     
  • all: (Used for SVGs) Makes the element react to pointer events for all sub-elements.

Description

The pointer-events property is a powerful feature for controlling how elements interact with user input. By default, HTML elements like buttons and links respond to mouse or touch events. However, there are times when you might want an element to ignore these events, such as when an overlay is placed on top of a clickable element.

For example, if a modal window or a tooltip appears, you may want the background content to be non-interactive, but still allow the user to interact with the modal or tooltip. This is where pointer-events: none becomes useful.

Formal Definition

pointer-events is a CSS property that determines whether an element responds to pointer events. Pointer events include mouse clicks, touch input, or stylus actions. By changing the value of this property, you can specify whether the element will react to such inputs, be transparent to them, or behave in a custom way, especially in complex UI layouts.

Formal Syntax

/* Syntax for pointer-events */
element {
  pointer-events: value;
}


Values for pointer-events:

  • auto: The default behavior, where an element responds to pointer events.
     
  • none: The element ignores pointer events, making it unresponsive to clicks, hover, or other pointer actions.
     
  • visiblePainted, visibleFill, visibleStroke, and visible: Used with SVGs to control which parts of the SVG react to pointer events.
     
  • all: Allows SVG elements to respond to pointer events for all sub-elements.

Property Values  

CSS pointer events are controlled using the `pointer-events` property. This property has several values, but the most commonly used ones are `none` & `auto`. These values determine how an element responds to user interactions like clicks, hover, & touch. Let’s understand these values in detail.  

`none`  

When you set `pointer-events: none;` on an element, it becomes "invisible" to pointer actions. This means clicks, hover, & other interactions will pass through the element & affect whatever is beneath it. This is useful when you want to disable interactions with a specific element without removing it from the DOM.  


For example, imagine you have a transparent overlay on top of a button. Normally, clicking the overlay would prevent the button from being clicked. But with `pointer-events: none;`, the overlay will ignore clicks, allowing the button underneath to work as expected.  


For example:  

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Pointer Events Example</title>
  <style>
    .overlay {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5); / Semi-transparent black /
      pointer-events: none; / Clicks pass through this element /
    }
    .button {
      position: relative;
      padding: 10px 20px;
      background-color: blue;
      color: white;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="button">Click Me</div>
  <div class="overlay"></div>

  <script>
    document.querySelector('.button').addEventListener('click', () => {
      alert('Button clicked!');
    });
  </script>
</body>
</html>


In this example, the `.overlay` div covers the button but doesn’t block clicks because of `pointer-events: none;`. When you click the button, the alert will still show up.  

 `auto`  

The `auto` value is the default behavior for most elements. When you set `pointer-events: auto;`, the element will respond to pointer actions as usual. This is helpful when you want to re-enable interactions on an element that previously had `pointer-events: none;`.  

For instance, if you have a disabled element that you want to make interactive again, you can switch its `pointer-events` property back to `auto`.  

For example:  

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Pointer Events Example</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
      background-color: green;
      pointer-events: none; / Initially disabled /
    }
    .box.active {
      pointer-events: auto; / Re-enable interactions /
    }
  </style>
</head>
<body>
  <div class="box"></div>
  <button onclick="enableBox()">Enable Box</button>
  <script>
    function enableBox() {
      const box = document.querySelector('.box');
      box.classList.add('active');
      box.style.backgroundColor = 'red';
      box.addEventListener('click', () => {
        alert('Box clicked!');
      });
    }
  </script>
</body>
</html>


In this example, the `.box` div initially has `pointer-events: none;`, so it won’t respond to clicks. When you click the "Enable Box" button, the box’s `pointer-events` property is set to `auto`, & it becomes clickable.  

Example - Using `none` Value  

The `none` value for the `pointer-events` property is particularly useful when you want to disable interactions with an element without removing it from the page. Let’s understand a practical example where this can be applied.  

Scenario: Disabling Clicks on an Overlay  

Imagine you have a modal or a popup that appears on top of your webpage. You want users to interact with the modal but not with the content behind it. However, in some cases, you might want the opposite—where the modal is transparent, & users can interact with the content behind it. This is where `pointer-events: none;` comes into play.  

For example:  

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Pointer Events - None Example</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: f0f0f0;
    }


    .content {
      text-align: center;
    }


    .content button {
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
    }


    .overlay {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5); / Semi-transparent black /
      pointer-events: none; / Clicks pass through this element /
    }


    .modal {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background: white;
      padding: 20px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      pointer-events: auto; / Modal is clickable /
    }
  </style>
</head>
<body>
  <div class="content">
    <button onclick="showModal()">Show Modal</button>
    <p>Try clicking this button even when the overlay is active.</p>
  </div>


  <div class="overlay"></div>


  <div class="modal" style="display: none;">
    <h2>Modal Title</h2>
    <p>This is a modal dialog.</p>
    <button onclick="hideModal()">Close</button>
  </div>


  <script>
    function showModal() {
      document.querySelector('.overlay').style.display = 'block';
      document.querySelector('.modal').style.display = 'block';
    }


    function hideModal() {
      document.querySelector('.overlay').style.display = 'none';
      document.querySelector('.modal').style.display = 'none';
    }
  </script>
</body>
</html>


In this Code :    

1. HTML Structure:  

  • The `.content` div contains a button & some text.  
     
  • The `.overlay` div is a semi-transparent layer covering the entire screen.
      
  • The `.modal` div is a popup that appears in the center of the screen.  


2. CSS Styling:  

  • The `.overlay` has `pointer-events: none;`, which means clicks will pass through it to the elements below.  
     
  • The `.modal` has `pointer-events: auto;`, so it remains interactive.  


3. JavaScript Functionality:  

  • The `showModal()` function displays the overlay & modal when the button is clicked.  
  • The `hideModal()` function hides the overlay & modal when the "Close" button is clicked.  

How It Works  

  • When the "Show Modal" button is clicked, the overlay & modal appear.  
     
  • Even though the overlay covers the entire screen, you can still click the "Show Modal" button because of `pointer-events: none;`.  
  • The modal itself is interactive, allowing you to click the "Close" button.  

Examples

Let’s look at a few practical examples of how pointer-events can be used.

Example 1: Default Pointer Event Behavior

In this example, we have a button that responds to a click event.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pointer Events Example 1</title>
    <style>
        button {
            background-color: blue;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <button onclick="alert('Button Clicked!')">Click Me</button>
</body>
</html>


Output

Output

Explanation

In this code, when the button is clicked, an alert is triggered, and the user sees the message "Button Clicked!". This is the default behavior where pointer-events is set to auto.

Example 2: Using pointer-events: none

In this example, we use pointer-events: none to prevent the button from responding to clicks.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pointer Events Example 2</title>
    <style>
        button {
            background-color: blue;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
        }

       .disabled-button {
            pointer-events: none;
            background-color: gray;
        }
    </style>
</head>
<body>
    <button class="disabled-button" onclick="alert('Button Clicked!')">Click Me</button>
</body>
</html>


Output

Output

Explanation

The button is still visible, but it won't respond to any pointer events (clicks, hover, etc.) because pointer-events is set to none. This is useful when you want to make an element non-interactive temporarily.

Example 3: Using pointer-events with SVG

You can use pointer-events in SVG elements to control interaction. Here’s an example of how it works:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SVG Pointer Events</title>
    <style>
        svg {
            width: 200px;
            height: 200px;
        }
        circle {
            fill: red;
            pointer-events: none;
        }
    </style>
</head>
<body>
    <svg>
        <circle cx="100" cy="100" r="50"></circle>
        <rect x="50" y="50" width="100" height="100" fill="blue" onclick="alert('Rectangle clicked!')"></rect>
    </svg>
</body>
</html>


Output

Output

Explanation

In this case, the circle will not respond to pointer events (it ignores clicks) due to pointer-events: none, while the rectangle remains interactive and shows an alert when clicked.

Browser Compatibility

CSS pointer events are well-supported across modern browsers, but there are some variations in behavior for certain features like SVGs.

  • Chrome: Full support for pointer-events in both HTML and SVG elements.
     
  • Firefox: Full support, including all values like visiblePainted, visibleFill, and others for SVG.
     
  • Safari: Full support, including pointer-events: none for all HTML elements.
     
  • Edge: Full support for CSS pointer-events.
     

However, it’s important to test cross-browser to ensure consistent behavior, especially when using advanced SVG features.

Frequently Asked Questions

What happens when I set pointer-events to none? 

When you set pointer-events: none on an element, it will not respond to any pointer events like clicks, hover, or touch interactions. This can be useful for making elements like overlays non-interactive.

Can I use pointer-events with non-interactive elements? 

Yes, pointer-events can be used with any HTML or SVG element, including non-interactive ones. It helps to control which elements should be able to respond to user input.

Are there performance issues with pointer-events? 

No, using pointer-events: none or other values does not significantly impact performance. It's a lightweight property, and its usage is quite efficient for managing element interactions.

Conclusion

In this article, we discussed the CSS pointer events property, which allows you to control how elements respond to pointer interactions. We learned about its syntax, the different values it can take, and how to use it in both HTML and SVG elements. Through practical examples, we demonstrated how pointer events can make elements either interactive or non-interactive. Understanding this property is essential for creating more interactive, user-friendly web interfaces.

Live masterclass