Home > Web Front-end > CSS Tutorial > Use the after pseudo-class to clear floats

Use the after pseudo-class to clear floats

高洛峰
Release: 2017-03-01 14:57:19
Original
2463 people have browsed it

In the past, when clearing floats, I always added

 <div style="clear:both;"></div>
Copy after login

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;
}
Copy after login

is compatible with IE6 and IE7 because ie6 and ie7 cannot be used. after pseudo-class. Add the following code

 .clearfix{zoom:1}
Copy after login

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>
Copy after login

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;
}
Copy after login

More use after pseudo-class clearing For floating related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template