Pointer Events
Enable pointer events for child elements
by jwerre
HTML
<div>
<p>Hover Here</p>
<p>— OR —</p>
<p>
<button id="trigger">Click Me</button>
</p>
</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
CSS
div {
font-family: sans-serif;
font-weight: bold;
font-size: 150px;
text-align: center;
color: white;
line-height: 300px;
width: 300px;
height: 300px;
float: left;
cursor: pointer;
border: 3px white solid;
background-color: DodgerBlue;
}
div:nth-child(even) {
clear: both;
}
div + div:hover {
color: DodgerBlue;
background: GreenYellow;
}
div:first-child {
/*
This will disable ALL child events as well
*/
pointer-events: none;
position: absolute;
font-size: 14px;
color: DodgerBlue;
line-height: 30px;
padding-top: 110px;
box-sizing: border-box;
top: 150px;
left: 150px;
border: none;
box-shadow: 0 0 20px rgba(0, 0, 0, .5);
background-color: GreenYellow;
border-radius: 100%;
}
div:first-child > * {
/*
Reenable child events: without this click event will not work
*/
pointer-events: auto;
}
JavaScript
document.getElementById("trigger")
.addEventListener("click", function(e) {
alert('top div clicked')
});