Home  >  Article  >  Web Front-end  >  Detailed explanation of the mainstream method of clearing floats by the after pseudo-class in CSS!

Detailed explanation of the mainstream method of clearing floats by the after pseudo-class in CSS!

藏色散人
藏色散人Original
2018-10-23 17:18:233344browse

This article mainly introduces how to use css after pseudo-class to clear floating .

First of all, everyone needs to understandWhat is css floating?

Make the element break away from the document flow, move in the specified direction, and stop when it encounters the parent boundary or adjacent floating elements.

It can also be understood like this: a floating box can move left or right until its outer edge touches the border of the containing box or another floating box. Because the floated box is not in the document's normal flow, a block box in the document's normal flow behaves as if the floated box does not exist.

So why do we clear floats?

This is because floating elements sometimes affect the overall layout and cause some bugs.

The following code:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>css浮动示例</title>
<head>
    <style>
        .demo{
       
            background: #ffffff;
            border: 1px solid black;
            margin: 50px auto;
            padding: 50px;
        }
        .demo1{
    float: left;
    width: 100px;
    height: 50px;
    color: white;
    background: #1094f2;
    border: 1px solid black;
        }
        .demo2{
    float: left;
    width: 100px;
    height: 50px;
    color: white;
    background: #9492ff;
    border: 1px solid black;


         }
 </style>
</head>
<body>
<div class="demo">
    <div class="demo1">demo1</div>
    <div class="demo2">demo2</div>
</div>

</body>
</html>

The effect is as shown below:

Detailed explanation of the mainstream method of clearing floats by the after pseudo-class in CSS!

You can see in the picture that we are setting the floating attribute float for demo1.2 After :left, the parent div is not opened. And we did not set the width and height dimensions for the demo.

Here we must first understand what the floating attribute is in css?

float attributeDefines in which direction the element floats. Historically this property has always been applied to images, causing the text to wrap around the image, but in CSS, any element can be floated. A floated element creates a block-level box, regardless of what type of element it is.

So when we encounter some floats that affect the layout, how do we clear them?

The following will introduce to you the most mainstream cleaning method, using css after pseudo-class.

Just add the following code:

.demo:after{
    clear: both;
    content: &#39;&#39;;
    display: block;
}

The final effect is as follows:

Detailed explanation of the mainstream method of clearing floats by the after pseudo-class in CSS!

In fact, the main principle is:

Use :after and :before to insert two element blocks into the element to achieve the effect of clearing floats.

This article is about the specific method of clearing floats using the after pseudo-class. It is very simple and easy to understand. I hope it will be helpful to friends in need! 】

If you want to learn more about front-end related knowledge, you can follow the PHP Chinese website CSS video tutorial, Bootstrap tutorial and other related tutorials. Welcome everyone to refer to and learn!

The above is the detailed content of Detailed explanation of the mainstream method of clearing floats by the after pseudo-class in CSS!. For more information, please follow other related articles on 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