Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

CSS pointer-events to allow clicks on underlying elements

DZone's Guide to

CSS pointer-events to allow clicks on underlying elements

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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!

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. :-)

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Robert Nyman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}