CSS display none

greg's picture

He has: 1,581 posts

Joined: Nov 2005

What is the point?

I can't for the life of me think why you would want to have code that you don't actually use in anyway.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Often so you can dynamically re-show it using Javascript. (The best way to do this is to have it visible by default then hide onload with javascript)

Sometimes so you can hide something in code outside of your control e.g. in CMS templates or in user stylesheets.

Those are two that I use most often...

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Mainly for conditional display.

You might want to show some element only when some event occurs (say, a drop-down element in a navigation menu).

Or you might want to include text for screen readers or CSS-less browsers, in a manner analogous to the noscript tag. Screen reader behaviour is flaky, though.

Incidentally, I wonder if SEs still give any weight to text that isn't displayed. Probably not, or much less than normal text. Might be worth checking.

It's also useful in development since you can turn on or off displaying some elements easily, rather than deleting or commenting out large parts of HTML code.

Smiling

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I think the SE's apply the same rules they would to other text. If it's normal image replacement for headers, for example, then that's not a problem. If it looks like keyword spam then they would discount it.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

He's a strange little character this display none!
So it's like not displaying something to then use JS or whatever to display it.
Seems like the wrong way to go about things to me.

Here is something don't display it but if X=Y display what was hidden.
Why not just if X=Y display this->

As for SE's, I presumed it wont affect keywords unless you display it, as it isn't in the HTML unless you "unhide" it, and then it's just the same as having something there all along.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Here is something don't display it but if X=Y display what was hidden.
Why not just if X=Y display this->

Because the HTML document retains its full content regardless of applying scripting and styling. That some parts are not displayed (like a dropdown menu) is an extra feature. I would think separating content from scripting is a better approach than mixing them.

it isn't in the HTML unless you "unhide" it

Actually, it is in the HTML whether the style is applied or not. Styling does not affect the semantic structure of the document. Neither does scripting in this case, supposing it doesn't modify the document tree beyond swapping the CSS properties.

Smiling

He has: 629 posts

Joined: May 2007

You could use it to fix a lot of IE 6 bugs:

* html body { display: none; }

Laughing out loud

silverwing's picture

He has: 79 posts

Joined: May 2008

webwiz wrote:
You could use it to fix a lot of IE 6 bugs:

* html body { display: none; }

:D

That's what I use it for.... except, usually not in the body....

~silverwing

He has: 629 posts

Joined: May 2007

... On a more serious(?) note, here's a practical example:

CSS
p.ie-warning {
display: none;
_display: block;
}

HTML
<p class="ie-warning">Your web browser sucks!</p>

Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;

silverwing's picture

He has: 79 posts

Joined: May 2008

I'm working on a new drupal theme for my site and I use a logo instead of the h1 sitename, but I keep the h1 in the code and set it to display:none just in case someone wants to look at my site without the stylesheet.

~silverwing

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.