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
1 What is MVCC The full name of MVCC is: Multiver...
The JD carousel was implemented using pure HTML a...
This article example shares the specific code of ...
Preface: During database operation and maintenanc...
Note: sg11 Our company only supports self-install...
Swap memory mainly means that when the physical m...
Table of contents 1 Review 2 Five strategies for ...
This article does not have any quibbles, it is jus...
One day, the leader put forward a requirement to ...
Friends who have bought space and built websites s...
Table of contents Some basic configuration About ...
Preface: After the automation is written, it need...
Table of contents Preface: 1. Create index method...
Written in the opening: Absolute said: "Rela...