0

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.

1
  • 3
    Logically correct and W3C valid way and compatible with IE6 is kind of an impossible requirement...
    – jeroen
    Nov 19, 2009 at 15:51

2 Answers 2

1

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%;
}
6
  • it gives Parse Error emphasized text in W3C validation Nov 19, 2009 at 14:59
  • "Congratulations! No Error Found.". It's valid
    – marcgg
    Nov 19, 2009 at 15:00
  • What does last part? and is your solution compatible with IE6 ,7, 8 and latest firefox, safari (MAC and windows, Google Chrome and opera)? Nov 19, 2009 at 15:02
  • 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 ( bytesizecss.com/blog/post/the-clear-fix ). Consider googling "css clearfix" and see if one fits better for you
    – marcgg
    Nov 19, 2009 at 15:08
  • this method generates content that really hasn’t any semantic value. Nov 19, 2009 at 16:08
1

<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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.