CSS pointer-events to allow clicks on underlying elements
Join the DZone community and get the full member experience.
Join For Freeever 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.
Published at DZone with permission of Robert Nyman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Operator Overloading in Java
-
Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
-
Mastering Time Series Analysis: Techniques, Models, and Strategies
-
From On-Prem to SaaS
Comments