Clean Up Empty Elements with CSS 3
By default, WordPress wraps HTML comments with paragraph tags:
<p><!-- --></p>
WordPress also employs various template tags that may, in certain situations, result in empty HTML elements such as paragraphs tags:
<p></p>
<p><!-- --></p>
Other cases where empty elements are generated by WordPress involve images, line breaks, nested lists and other complex markup scenarios.
Besides invalid markup, the problem with this is that your CSS may be applying styles to the empty elements, potentially breaking your layout or disrupting the presentational harmony of your design.
For specifically targeted elements, such as paragraph elements for navigational template tags, dealing with this issue is as simple as this:
#nav {
display: none;
}
Either that or you could get all “Google-homepage” and use nothing but style-neutral span
s and div
s to markup your content. Unfortunately neither of these solutions is effective at alleviating the occasional, unpredictable WordPress-generated empty paragraph elements.
Fortunately, the new :empty
pseudo-selector of CSS 3 is the perfect solution for neutralizing all empty HTML elements. Here are some examples..
To neutralize all empty paragraphs (and only empty paragraphs), throw down the following slice of CSS goodness:
p:empty {
display: none;
}
Or, to cover your bases and hide all empty elements, add this bad boy to your stylesheet and enjoy complete satisfaction:
*:empty {
display: none;
}
In my opinion, this declaration should be included in all CSS reset stylesheets, but I will leave that decision up to you.
But..
Note that, for now, this technique only works in supportive browsers, which seems to be anything based on Mozilla. Unfortunately, Safari applies the rule to all elements of the specified element type. Hopefully this will be addressed in an upcoming version. And of course, Internet Explorer has absolutely no idea what we’re talking about here.
13 responses
-
So, in safari with
p:empty { display: none; }
All paragraphs are hidden ? Is that what you’re saying ?
And how does IE respond to this ? Ignoring ? Or bad interpretation :)
-
In reply to your Safari question: exactly. (I just tried it.)
-
-
And of course, Internet Explorer has absolutely no idea what we’re talking about here.
lol… good article.
-
Good trick! but Then it doesn’t work for safari?
-
I checked my css reference, Safari since 3.0 has full support of
:empty
.
FF 3 has full support, but FF 2 and lower is buggy:- The selector
body:empty
always matches the body element. - The selector continues to match an element even after content has been added dynamically.
And (this was strange to me) Opera have no support at all.
- The selector
-
It had to be IE that doesn’t support it at all. Whats the Opera support like?
-
Very good article. I’m not sure if I like the fact that Safari decides to ignore the :empty part of the p:empty . Sure it might be just a matter of time till it gets fixed but then there’s still old versions out there. It’s one of the cool features that I feel could be really sweet in 5 years but for now I feel it’s just too risky to use if Safari is going to make your page hidden :)
oh the joys of getting things validated…
-
I tested it with IE8 and FF 3.5 and both see it.
Thanx a lot!
-
Problem is, that it won’t help with things like that:
<p> </p>
Other that that it sure is useful.