Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide div but keep the empty space

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.

like image 658
Y2theZ Avatar asked May 01 '13 10:05

Y2theZ


People also ask

How do I hide a div but keep the space?

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.

How can you hide an element but still keep its space in document?

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.

Which hiding element property in CSS hides item but the space for that item remains reserved?

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!


2 Answers

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

like image 76
Arun P Johny Avatar answered Sep 21 '22 05:09

Arun P Johny


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.

like image 37
dfsq Avatar answered Sep 19 '22 05:09

dfsq