CSS pointer-events to allow clicks on underlying elements

Ever placed an element on top of another element, but wanted the one under to be clickable? Now it’s doable, with CSS pointer-events!

(This article is also available in Chinese)

CSS pointer-events

Pointer-events is something that originally stems from the SVG world and is already present in a number of web browsers. However, it is now also available for any HTML element with the help of a little CSS. The property is called pointer-events (duh), and basically you can set it to auto, which is normal behavior and none, which is the interesting value.

Applying it to an element

If you have set the CSS of an element to pointer-events: none, it won’t catch any click on it at all, but instead just let the event fall through to the element below it. Like this:

<style>
	.overlay {
		pointer-events: none;
	}
</style>
	
<div id="overlay" class="overlay"></div>

Web browser support

Pointer-events are supported in Firefox 3.6+, Safari 4 and Google Chrome so far. I feel certain Opera will catch up soon, too – with IE, I have no idea if they plan to support it or not.

A little demo

I’ve put together a little demo of pointer-events in action, where you can test it out yourself. As you can see, the grey box on the right-hand side prevents clicks on the links below it. However, if you click the checkbox to disable pointer events for it, they will instead be triggered on the underlying links.

The complete code for the demo looks like this:

	<!DOCTYPE html>
	<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>CSS pointer events</title>
		<style>
			.container {
				position: relative;
				width: 370px;
				font: 15px Verdana, sans-serif;
				margin: 10px auto;
			}

			.overlay {
				position: absolute;
				right: 0px;
				top: 0;
				width: 40px;
				height: 40px;
				background: rgba(0, 0, 0, 0.5);
			}
			.pointer-events-none {
				pointer-events: none;
			}
		</style>
		<script>
			window.onload = function () {
				document.getElementById("enable-disable-pointer-events").onclick = function () {
					document.getElementById("overlay").className = "overlay " + ((this.checked)? "pointer-events-none" : "");
				};
			};
		</script>
	</head>
	<body>


	<div class="container">
		<a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, <a href="http://twitter.com">Twitter</a>, 

		<div id="overlay" class="overlay"></div>

		<p>
			<input id="enable-disable-pointer-events" type="checkbox">
			<label for="enable-disable-pointer-events">Disable pointer events for grey box</label>
		</p>
	</div>



	</body>
	</html>

Real-world case

If you go to the start page of Twitter, and not being logged in, you will see a number of tags listed at the bottom. On the right side, they have an element with a faded image as an overlay to create that effect, but unfortunately the links below aren’t clickable. If they were to add only one line of CSS, it would be…

Now, if you have a need for this, now you have a very simple solution. :-)

Posted in CSS,Developing,Technology,Web browsers |

46 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>