Hide div and the white space it occupies

Problem
Most of the developers will say “I can use javascript and CSS ‘visibility’ property to do that”, but shortly after that they’ll realize that this is not enough. This solution will hide the div tag content, but the space it occupies will stay.

You can reproduce this behavior using the code below:

if (objDiv.style.visibility == 'visible' ||
   objDiv.style.visibility == '')
{
  objDiv.style.visibility = 'hidden';
}
else
{
  objDiv.style.visibility = 'visible';
}

where objDiv is the div object which you’re trying to hide.

Solution

It’s simple - initialize “display” property in addition. So the above code will look like:

if (objDiv.style.visibility == 'visible' ||
    objDiv.style.visibility == '')
{
  objDiv.style.visibility = 'hidden';
  objDiv.style.display = 'none';
}
else
{
  objDiv.style.visibility = 'visible';
  objDiv.style.display = 'block';
}
, , ,
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Technorati
  • digg
  • Reddit
  • SphereIt
  • DotNetKicks
  • YahooMyWeb

5 Responses to “Hide div and the white space it occupies ”


  1. 1 Георги Dec 20th, 2007 at 4:16 pm

    When you set the “display” property, the “visibility” is just additional code… You can skip it.

  2. 2 Ramesh Apr 11th, 2008 at 3:02 pm

    thanks Dude
    It really helped me a lot..am searching for this solution for a long time..

  3. 3 Alex May 1st, 2008 at 9:24 am

    This is fantastic. Thank you!

  4. 4 LEon May 1st, 2008 at 11:12 am

    thanks dude, was stumped with some CSS

  5. 5 Ron Jan 6th, 2009 at 11:03 am

    Actually, the use of the display attribute gets into some browser specific issues

Leave a Reply




Google

Blogroll


Blogroll Me!

Enter your email address:

Delivered by FeedBurner







Academics Blogs - Blog Top Sites