Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide an element without taking up space and still respond to events?

display:none takes the element away from the layout flow and thus not taking up space on the page but its events get disabled.

visibility:hidden hides the element, but the element still takes up space.

I need a way to hide a file input element without taking up space and responding when I call its .click() event.

Example

like image 992
tamakisquare Avatar asked Dec 13 '22 06:12

tamakisquare


2 Answers

Simply setting opacity to 0 should work. The element won't show up, and it wont take space either. And its events will work.

When giving opacity, also specify the opacity counterparts of all browsers (-moz.., -webkit, filter: ..) etc.. to ensure cross-browser compatibility.

EDIT

Your style should look something like:

.mydiv {
  position: absolute;
  left: 10px; /* change as needed */
  top: 10px; /* change as needed */
  opacity: 0;
}

Working demo here: http://jsfiddle.net/t2BHg/6/

like image 99
techfoobar Avatar answered May 17 '23 13:05

techfoobar


Setting display: none; does not disable events for an element, but it will prevent it from being clicked because the element has no pixel dimensions to click into. You can still call its onclick events programmatically. See an example

like image 38
Michael Berkowski Avatar answered May 17 '23 14:05

Michael Berkowski