The pointer-events
property allows for control over how HTML elements respond to mouse/touch events – including CSS hover/active states, click/tap events in Javascript, and whether or not the cursor is visible.
.avoid-clicks { pointer-events: none; }
While the pointer-events
property takes eleven possible values, all but three of them are reserved for use with SVG. The three valid values for any HTMl element are:
none
prevents all click, state and cursor options on the specified HTML elementauto
restores the default functionality (useful for use on child elements of an element withpointer-events: none;
specifiedinherit
will use thepointer-events
value of the element’s parent
<p>This is some basic flow content. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magni eos ipsum sunt repellat nisi modi voluptatum ipsa eligendi minima cumque. Accusantium laudantium autem quae earum eaque expedita quia molestiae in.</p> <div class="avoid-clicks">try selecting text through me</div>
.avoid-clicks { display: block; width: 8em; height: 8em; background: rgba(51,51,51,0.85); position: absolute; top: 1em; left: 4em; padding: 0.75em; pointer-events: none; color: whitesmoke; } body { font: 14px/1.4 "Trebuchet MS", sans-serif; padding: 3em; max-width: 600px; background: whitesmoke; } p { padding: 0.75em; background: #ddd; }
As demonstrated above, the prime use case for pointer-events
is to allow click or tap behaviour to “pass through” an element to another element below it on the Z axis. For example, this would be useful for graphic overlays, or hiding elements with opacity
(eg. tooltips) and still allowing text-selection on the content below it.
Points of Interest
- “The use of pointer-events in CSS for non-SVG elements is experimental. The feature used to be part of the CSS3 UI draft specification but, due to many open issues, has been postponed to CSS4.” — Mozilla MDN
- “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.” — David Walsh
Browser Support
Chrome | Safari | Firefox | Opera | IE | Android | iOS |
---|---|---|---|---|---|---|
Yep | 4.0 | 3.6 | 15.0 | 11+ | Yep | Yep |
The support is a bit deeper in some browsers when used on <svg>
, for instance, IE 9 supports that.
Leave a Reply