What does the CSS rule “clear: both” do?

前端 未结 5 1533
轮回少年
轮回少年 2020-11-21 07:42

What does the following CSS rule do:

.clear { clear: both; }

And why do we need to use it?

5条回答
  •  日久生厌
    2020-11-21 08:01

    Mr. Alien's answer is perfect, but anyway I don't recommend to use

    because it just a hack which makes your markup dirty. This is useless empty div in terms of bad structure and semantic, this also makes your code not flexible. In some browsers this div causes additional height and you have to add height: 0; which even worse. But real troubles begin when you want to add background or border around your floated elements - it just will collapse because web was designed badly. I do recommend to wrap floated elements into container which has clearfix CSS rule. This is hack as well, but beautiful, more flexible to use and readable for human and SEO robots.

提交回复
热议问题