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-3.png

Recent Features

  • 5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

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!