CSS pointer-events

Written by David Walsh on June 25, 2012 · 11 Comments

The responsibilities taken on by CSS seems to be increasingly blurring with JavaScript. Consider the -webkit-touch-callout CSS property, which prevents iOS's link dialog menu when you tap and hold a clickable element. The pointer-events property is even more JavaScript-like, preventing:

  • click actions from doing anything
  • the default cursor pointer from displaying
  • CSS hover and active state triggering
  • JavaScript click events from firing

One CSS property is capable of doing just that!

The CSS

The pointer-events property can have many values, but many of them are only applicable to SVG*: auto, none, visiblePainted*, visibleFill*, visibleStroke*, visible*, painted*, fill*, stroke*, all*, and inherit. The none value prevents the click, state, and cursor actions:

.disabled { pointer-events: none; }

A few quick notes about pointer-events:

  • If children of the element have pointer-events explicitly enabled, clicks will be allowed on those child elements.
  • If you add a click event listener to an element, then remove the pointer-events style (or change its value to auto, the click event will fire the designated functionality. Basically, the click event respects the pointer-events value.

I first noticed pointer-events used on the Firefox Marketplace website for disabled buttons. A bonus in this case is styleability; think about how awful elements with the disabled attribute look! Of course, don't use pointer-events on elements that could trigger critical functionality -- the style can simply be remove via the console!

Comments

  1. Nice feature, I didn’t know about it. A small remark: I was able to fire the onclick event in the demo page by tabbing into the link and pressing Enter; it looks like it does not ignore keyboard events, only mouse events.

  2. I disagree to some extent with the idea of the article, though happy to see the actual functionality being publicized since it’s useful. It seems very much the domain of CSS to control whether something is intractable. This is absolutely the purview of CSS and it’s only even a question because this control has mostly been denied to the CSS component of implementation. JavaScript (should be) is responsible for describing what happens between the action and the outcome. Describing when a graphical element has no impact on functionality, and in fact should be entirely ignored by the application, seems to be directly the purview of CSS.

  3. actually whats the use of this feature…where can we use this feature can you please explain me…

    • One possible use is to disable pointer events of a absolute div with a transparent background above a scrollable area.

  4. What do u mean “the style can simply be remove via the command line!”. Is the cmd line refer to browser console ?

  5. Ole Jorgensen June 28, 2012

    I testet it for disabling elements in an Div, just like you would disable input elements, it worked great and did the functionality I wanted. But IE is a bitch, I could not get it to work there.
    Have you got any insights on this, is it possible to get it to work in IE and did I just do something wrong?

  6. Chris Van July 3, 2012

    Nice article. They’re awesomes, yes. Sadly, Opera doesn’t support `pointer-events` at all: http://caniuse.com/pointer-events

  7. Another extremely useful effect of this is that it helps speed up animations, especially those that are triggered on scroll.

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
Node.js CSS Compressor: clean-css
Newer
Detecting Mozilla App Install Capabilities with JavaScript