I have the following div element:
.description { color: #b4afaf; font-size: 10px; font-weight: normal; }
<div class="description">Some text here</div>
Then I have a click function on an element to hide the above div:
$('#target').click(function(){ $(".description").hide(); });
When I hide the div, it collapses and stops taking up space. This messes up the layout of my page.
Is there a way to hide the div, but still maintain the space it was taking before? I don't want to change the font color because it would be still selectable.
The visibility: hidden rule, on the other hand, hides an element, but the element will still take up space on the web page. If you want to hide an element but keep that element's space on the web page, using the visibility: hidden; rule is best.
visibility:hidden hides the element, but it still takes up space in the layout. display:none removes the element from the document. It does not take up any space.
The visibility property specifies whether or not an element is visible. Tip: Hidden elements take up space on the page. Use the display property to both hide and remove an element from the document layout!
Use visibility css property for this
visibility:
The visibility property specifies whether the boxes generated by an element are rendered.
$(".description").css('visibility', 'hidden');
Demo: Fiddle
And another option for the sake of completeness. Toggle opacity
:
$(".description").css('opacity', 0); // hide $(".description").css('opacity', 1); // show
http://jsfiddle.net/KPqwt/
However using visibility
is prefered for this task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With