html - CSS layout clear both affects the flow -


why <h1> tag clear both @ bottom, if rid of "clear both", works fine, how can work clear both?

<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=gbk"/> <style> #left{     float:left;     width:200px;     height:400px;     background:#cfc; } #main{     margin-left:210px;     background:#f9c;     height:400px; } h1{     clear:both; } </style> </head> <body> <div id="left"></div> <div id="main">     <h1>test</h1> </div> </body> </html> 

your questions:

1- why tag clear both @ bottom?

according css specs clear:

both
requires top border edge of box below bottom outer edge of right-floating , left-floating boxes resulted elements earlier in source document.

so clear:both; on h1 clears float on earlier element, not part of containing div of h1.

2- how can work clear both?

if need keep clear:both; on h1 , keep @ beginning of container div, simplest way add overflow:auto; container.

demo

#main {     margin-left:210px;     background:#f9c;     height:400px;     overflow:auto; } 

Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

php - Boolean search on database with 5 million rows, very slow -

css - Text drops down with smaller window -