Today I will talk to you about clearing floats. Before talking about clearing floats, you need to understand what floats are. I will not give you a detailed introduction to floats here. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; } .box{ width: 1000px; margin: 0 auto; border: 8px solid black; } .box::after{ content: ""; clear: both; display: block; } .box .left{ width: 50%; height: 300px; background-color: red; float: left; } .box .right{ width: 50%; height: 300px; background-color: blue; float: right; } </style> </head> <body> <div class="box"> <div class="left"></div> <div class="right"></div> </div> </body> </html> From here we can see that when we use floating, the width and height of the parent cannot be stretched by the child, so the layout will be different from what you imagined. There are many solutions here. The first one: Add a div to the parent element and clear the float for the added div <div class="clear"></div> clear{ clear: both; } Second type: We can set the height of the parent div, which is also possible. .box{ width: 1000px; height: 300px; margin: 0 auto; border: 8px solid black; } The third We can add overflow:hidden; attribute to the parent, which will also work. .box{ overflow: hidden; width: 1000px; margin: 0 auto; border: 8px solid black; } The fourth We can use position: absolute or display: inline-block to clear floating. .box{ /* position: absolute; */ display: inline-block; width: 1000px; margin: 0 auto; border: 8px solid black; } In fact, it is enough to know the other four types, but you must know how to use the fifth type. The other four types can clear floating elements, but they will bring unnecessary trouble. Take the second type as an example. If the parent needs to add child elements later, we also have to modify the height of the parent, which will cause a lot of trouble. The fifth type is also the most practical one. The fifth Use pseudo elements to clear floats. We can add pseudo elements to the parent. .box::after{ content: ""; clear: both; display: block; } This is the end of this article about the example code of CSS clear float clear:both. For more related CSS clear both clearing floating content, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: JS uses clip-path to implement dynamic area clipping function
>>: Linux system AutoFs automatic mount service installation and configuration
Single page application characteristics "Ass...
1. Download 1. Click the latest download from the...
Table of contents 1. Configure bridging and captu...
Create a table create table order(id varchar(10),...
When learning kubernetes, we need to practice in ...
First of all, we need to know what a state manage...
Syntax composition: 1 Annotation information 2 Co...
In the horizontal direction, you can set the cell...
This article example shares the specific code of ...
What are the lifecycle functions of React compone...
This article shares with you the solution to the ...
This article records the installation and configu...
Docker error 1. Check the cause docker logs nexus...
statement : This article teaches you how to imple...
I once promised that I would keep writing until pe...