CSS: Is a hidden object clickable?

CssVisibilityDisplay

Css Problem Overview


If the visibility property of the style of an HTML element is set to hidden, is it still clickable?

When the display property is set to none, the element is not even part of the DOM tree, so that is not a problem. But I was wondering if a hidden element still responds to mouse events.

Css Solutions


Solution 1 - Css

With display: none it is still part of the DOM. It just isn't rendered in the viewport.

As for clicks on elements with visibility: hidden, the events are not fired.

jsFiddle.

$('div').click(function() {
    alert('Hello')
});

div {
    width: 100%;
    height: 100%;
    visibility: hidden; 
}

<div>abc</div>

Solution 2 - Css

Making div hidden or display none just makes it un clickable to user. But in real its still an element in dom and you can click it with another java script/jquery like this.

$('div').click(function() {
    alert('Hello')
});
$('div').click();

jsfiddle enter image description here

Solution 3 - Css

No.

An element such as a hyperlink can't be clicked (and the link followed) if the visibility is set to hidden. Similarly, onclick events won't be fired.

Solution 4 - Css

An HTML element with visibiality: hidden is not visible but it still takes up space on the page. You can't trigger events (click, hover, ...) by using mouse, but you can use javascript to access it through DOM and trigger events.

Solution 5 - Css

You can use useRef in react, add ref to your input and use it to call onClick like below

  import React, { useRef } from "react";
  const input = useRef();
  <input ref={input} type="file" />

and then call whatever you want like click method

 input.current.click();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioneuphoria83View Question on Stackoverflow
Solution 1 - CssalexView Answer on Stackoverflow
Solution 2 - CssJin ThakurView Answer on Stackoverflow
Solution 3 - CssADWView Answer on Stackoverflow
Solution 4 - CssQuang ChứView Answer on Stackoverflow
Solution 5 - CssMehrad FarahnakView Answer on Stackoverflow