Reputation: 1858
We are trying to get our HTML much more semantic and the one thing that seems to linger in our HTML that has to do with presentation is
<div class="clear"></div>
For example, if we have the following semantic html:
<div class="widgetRow">
<div class="headerInfo">My header info</div>
<div class="widgetPricing">$20 to $30</div>
<div class="footerInfo">My footer info</div>
</div>
And I have headerInfo and footerInfo both floated left in the CSS and widgetPricing floated right (just as an example).
The Question:
My widgetRow div doesn't have any height or width. Is it wrong to add <div class="clear"></div>
right after .footerInfo ? It seems that I'm not being semantic at that point.
The More Generic Question
When writing semantic HTML, is it ok to put a div in your HTML whose only job is to clear the floats?
Upvotes: 5
Views: 2863
Reputation: 371
A part from the excellent answers above, I would add one more approach:
4. Use display:inline-block + vertical-align:top
This one may become also tricky, specially in IE7 and older, because, according to http://www.quirksmode.org/css/display.html
IE 6/7 accepts the value only on elements with a natural display: inline
It is widely supported nowadays, for certain elements it will even work with IE6/7, and you will achieve the same effect as floating elements, but without the clearing issue. In some cases you could even make slight changes to your markup (that will remain semantic) to use a native inline element. A hack could also be used only for IE, see IE7 does not understand display: inline-block
Anex: Another possible issue with overflow:hidden
Another con to the overflow:hidden approach that I recently experienced: when using absolute positioned elements inside an overflow:hidden element, those will be cropped by the container. So, for instance, a CSS dropdown menu will not work well.
Upvotes: 0
Reputation: 116
There are several ways to clear floats:
1 . Use CSS pseudo :after class
.container:after { clear:both; content:"."; display:block; height:0; visibility:hidden; }
Apply the container class to your "widgetRow" div. This approach is probably the most semantic, but it is not supported on all browsers, specifically IE7 and below. browser support for :after
2 . Use overflow:auto or overflow:hidden
.container { overflow:auto; }
.container { overflow:hidden; }
Again, apply the container class to your "widgetRow" div. This approach may be a little more semantic, but it could also come back to bite you especially when viewed on smaller displays. overflow:auto could trigger a horizontal scrollbar while overflow:hidden could hide the element all together. problems using oveflow to clear floats
3 . Use clear:both
.clear { clear:both; }
This is the approach you are using assuming your clear class is like the one above. This is the only approach I know of that is compatible in all browsers and won't give you undesirable side effects. So, depending on what browsers you support, I would probably stick with what you have.
Upvotes: 7
Reputation: 25766
There's three basic ways i know of clearing floats.
clear:both;
like you have specified. overflow: auto
to the parent div. Pros and cons of these
pros
cons
pros
cons
pros
cons
Upvotes: 0
Reputation: 123367
No. Empty markup introduced only for visual/styling purposes should be avoided (it makes also the page hard to mantain/scale)
You could instead use some non-structural clearing methods like easyclearing (also used by H5BP) adding some extra style to float wrappers
Upvotes: 2
Reputation: 4536
Have you tried using the awesome clearfix hack? You don't need to add redundant, non-semantic empty elements this way.
To answer your more general question - no it's not semantically valid to add empty elements for layout purposes. However, nobody's going to get hurt if you have a few empty <div>
tags! :)
Upvotes: 0