views:

84

answers:

2

What is Logically correct and W3C valid way to clear float?

  1. zoom:1 is not valid by W3C and IE8 don't have hash layout problem
  2. overflow:hidden and overflow:hidden were not made to do this,as the spec intended overflow to be used
  3. <div class="clear"/> is not semantically correct and i don't want to add extra markup.
  4. clearfix hack generates content that really hasn’t any semantic value.

I've asked many questions and read many articles on this issue but haven't find best way.

A: 

Using a clearfix

.clearfix:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}

.clearfix {
    display: inline-block;
}

html[xmlns] .clearfix {
    display: block;
}

* html .clearfix {
    height: 1%;
}
marcgg
is this W3C valid css?
metal-gear-solid
it gives Parse Error *emphasized text* in W3C validation
metal-gear-solid
remove the last part, error in c/p
marcgg
"Congratulations! No Error Found.". It's valid
marcgg
What does last part? and is your solution compatible with IE6 ,7, 8 and latest firefox, safari (MAC and windows, Google Chrome and opera)?
metal-gear-solid
It's just an example of clearfix. It's hacky to handle IE and co and I'm not sure how it works but it works. There are a lot of variations on the subject ( http://bytesizecss.com/blog/post/the-clear-fix ). Consider googling "css clearfix" and see if one fits better for you
marcgg
this method generates content that really hasn’t any semantic value.
metal-gear-solid
Your hack would be better placed in a IE-only CSS (e.g., http://www.quirksmode.org/css/condcom.html)
BryanH
Is this solution will work well with IE8?
metal-gear-solid
yes I suppose. You can try it out
marcgg
+1  A: 

<div class="clear"/> is not semantically correct and i don't want to add extra markup.

That's why often a <br class="clear"> is been used. You basically want to have a linebreak between the float and the next element. True, it's not nice in the markup, but that's the payoff of using floats.

Alternatively you can also just set a clear: both; on the subsequent block element which you'd like to put in the next line.

BalusC