I am trying to hide an element on my page but I still want it to be there. I want a button that still can be clicked but I want it to be invisible.
4 Answers
There are three ways to hidden elements and keep the page position. You can get more information about between Normal flow and css style(ie: opcity, visibility) attribute relation.
- visibility
- opcity
- transparent
$('div').click(function (){
alert(this.innerHTML)
})
div{
border: 1px solid #000;
}
.one{
visibility:hidden;
}
.two{
opacity: 0;
}
.three{
background: transparent;
color: transparent;
}
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
1
<div class="one">visibility</div>
2
<div class="two">opticy</div>
3
<div class="three">transparent</div>
4
</body>
</html>

- 1,847
- 13
- 25
-
js cannot find the elements if `visibility:hidden` and `display:none` are used, `opacity` worked for me perfectly! `[+1]` – kapitan Aug 16 '22 at 02:18
-
Well done including 3 different ways to do it. – Shant Dashjian Mar 06 '23 at 16:40
Use opacity:0, if you want still to access the children elements of the div you need to be invisible.
.opacityZero {
opacity:0;
}
To clarify, below an explanation of the 3 properties:
Visibility
Using the visibility property in CSS, you can toggle the visibility of an element using the values hidden or visible. If you make the element hidden, it will be hidden from the user and user can't access it's child, but still in the DOM, the space of the element will be consumed.
Opacity
If you use opacity:0 to hide an element, at first glance it looks like hidden, but still the user can access the child element. It also consume the space of the element in the DOM. If you have a link as a child element it will be still clickable if you set opacity to 0. This is the way to go for you.
Display
If you use display:none, the browser will consider as if the element is not available in the DOM as the result it won't consume the space.
DEMO
$(".opacityZero, .visibilityHidden").click(function() {
alert("Button is clicked");
});
.opacityZero {
opacity:0;
}
.visibilityHidden {
visibility:hidden;
margin-top:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="opacityZero">
<button >I am invisible</button>
</div><span>↑ Click above (Opacity:0 - children clickable</span>
<div class="visibilityHidden">
<button>I am invisible</button>
</div><span>↑ Click above (Visibility hidden - children not clickable</span>

- 18,769
- 10
- 104
- 133

- 7,018
- 3
- 38
- 54
Just make your button transparent:
$("#invisible").click(function() {
alert("Invisible is clicked");
});
#invisible {
background: transparent;
color: transparent;
border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="invisible">I am invisible</button> <- Click here

- 5,699
- 2
- 26
- 28