Avaricesoft’s Weblog

Just another WordPress.com weblog

Hide div and the white space it occupies

Posted by avaricesoft on July 8, 2008

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’;
}

regards,

www.avaricesoft.com

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

  1. Sudheendra said

    Thanks

  2. Faizan said

    Nice Post

  3. ahmed said

    best post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.