Treehouse

CSS pointer-events

By on  

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!

ydkjs-5.png

Recent Features

  • 5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome,...

Incredible Demos

  • Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • CSS Rounded Corners

    The ability to create rounded corners with CSS opens the possibility of subtle design improvements without the need to include images.  CSS rounded corners thus save us time in creating images and requests to the server.  Today, rounded corners with CSS are supported by all of...

Discussion

  1. ab

    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. Devo

    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

    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

    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.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!