Html/css: Make a div "invisible" to clicks?
To make a div unresponsive to mouse clicks, you can use the CSS property pointer-events: none;. Implement it directly or by assigning a class:
In your HTML:
The div is now "invisible" to clicks, allowing them to pass through to elements below.
Under the hood: pointer-events explained
The pointer-events property dictates whether or not the element will respond to mouse/touch events. When set to 'none', it instructs the element to ignore pointer actions and allows the events to access elements underneath. This is supported in most of the modern browsers: Firefox 3.6+, Chrome 2+, IE 11+, and Safari 4+.
Alternative methods for mouse events
When pointer-events: none; isn't the right fit, JavaScript can be DP (Designated Player).
Using JavaScript to dodge clicks
Temporarily vanish the overlay div to interact with elements underneath:
Delegating clicks with JavaScript
Detect the click and delegate it to underlying baby:
Unbinding events with jQuery
Still tight with jQuery? Unbind events from a div:
Remember, for performance reasons, direct DOM manipulation is often given preference over jQuery.
Accessibility and the story of unclickable divs
Creating unclickable elements can fix layout issues, but make sure your changes don't mess with accessibility. Because, well, we care.
Tackling possible setbacks and their solutions:
Ensuring consistent UI behavior
pointer-events: none; can sometimes be a party pooper and interfere with the user interaction as expected. Always test your UI to confirm elements are accessible and behaving as they should be.
Handling dynamic content
If your overlay div has a mood of its own and keeps changing with user interaction, you might need to toggle the pointer-events property at runtime.
Cross-browser compatibility
Confirm your implementation doesn't play an uno reverse card and break the functionality in outdated or less popular browsers. Just have a Plan B ready.
Was this article helpful?