CSS pointer-events
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 toauto
, the click event will fire the designated functionality. Basically, the click event respects thepointer-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!
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.