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

Use the after pseudo-class to clear floats

高洛峰
高洛峰Original
2017-03-01 14:57:192467browse

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:

 c7c162f4346c5f9b366c4224b512a308
     ef97f43c01124fc78febd898df2aef5eleft94b3e26ee717c64999d7867364b1b4a3
     d63265fd15dd0bd32166bea3d879ea48right94b3e26ee717c64999d7867364b1b4a3
 94b3e26ee717c64999d7867364b1b4a3

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!

Statement:
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