In the past, when clearing floats, I always added
<div style="clear:both;"></div>
after the element that I want to clear, or wrote it in the br tag to solve the problem, but this will Add unsemantic tags. The following is implemented using the after pseudo-class. It is compatible with multiple browsers.
.clearfix:after{ content:""; display:block; height:0; clear:both; visibility:hidden; }
is compatible with IE6 and IE7 because ie6 and ie7 cannot be used. after pseudo-class. Add the following code
.clearfix{zoom:1}
Generally, if there is a float inside the parent layer, it may cause the height of the parent layer to be 0. Just add clearfix.
HTML:
<p class="parent clearfix"> <p class="left">left</p> <p class="right">right</p> </p>
CSS:
.clearfix{zoom:1} .clearfix:after{ content:""; display:block; height:0; clear:both; visibility:hidden; } .parent{ background-color:red; width:120px; } .left{ float:left; background-color:pink; height:60px; } .right{ float:right; background-color:#abcdef; }
More use after pseudo-class clearing For floating related articles, please pay attention to the PHP Chinese website!