DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

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

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
  1. DZone
  2. Coding
  3. Languages
  4. CSS pointer-events to allow clicks on underlying elements

CSS pointer-events to allow clicks on underlying elements

Robert Nyman user avatar by
Robert Nyman
·
Mar. 24, 10 · News
Like (0)
Save
Tweet
Share
19.17K Views

Join the DZone community and get the full member experience.

Join For Free

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

Element CSS

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: