This article mainly explains how to use clearfix and clear in HTML, for those who are just beginning to understand CSS. I will not write about the styles of clearfix and clear here. Let's talk about the usage of these two classes. First, let's look at an example: Copy code The code is as follows:<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"/> <title>How to use clearfix and clear in HTML</title> <link rel="stylesheet" type="text/css" href="/css/base.css" media="all"/> <style type="text/css"> .fl{float:left;} .demo{background:#ccc;} .item1{background:#f90;height:100px;width:100px;} .item2{background:#fc0;height:200px;width:100px;} </style> </head> <body> <div class="demo"> <div class="fl item1"></div> <div class="fl item2"></div> </div> </body> </html> We all know that using floats will cause many unknown problems. From the above example, we can find that the height of class="demo" is not stretched by the div inside. This is because the div inside floats and leaves the document. Because demo itself has no height, we cannot see its gray background. Of course, we can just give demo a height, but that gets away from the purpose of this article (sometimes we want the height of the outer div to be determined by the content inside). Since the problem is caused by floating, all you need to do is clear the floating. I believe experts have many ways to clear floating, such as overflow:hidden. Next I will introduce how to use clearfix and clear to clear floats. Copy code The code is as follows:<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"/> <title>How to use clearfix and clear in HTML</title> <link rel="stylesheet" type="text/css" href="/css/base.css" media="all"/> <style type="text/css"> .fl{float:left;} .demo{background:#ccc;} .item1{background:#f90;height:100px;width:100px;} .item2{background:#fc0;height:200px;width:100px;} </style> </head> <body> <h2>Use clear to clear floats</h2> <div class="demo"> <div class="fl item1"></div> <div class="fl item2"></div> <div class="clear"></div> </div> <h2>Clear floats with clearfix</h2> <div class="clearfix demo"> <div class="fl item1"></div> <div class="fl item2"></div> </div> </body> </html> We found that clearfix is mainly used in the parent layer of the floating layer, while clear is mainly used between floating layers and at the same level as the floating layer. If you want to expand the height of the parent layer, clear should be placed last. It is difficult to say which of these two methods is better. We can only say that specific needs should be met. Some people may ask, why does writing the clearfix style like this clear the float? What do these styles mean? This requires students to have a certain understanding of CSS pseudo-classes |
<<: Tutorial on using the hyperlink tag in HTML
>>: SystemC environment configuration method under Linux system
Table of contents 1. How to switch 2. Dynamically...
This article shares the installation and configur...
1: Throughput (Requests per second) A quantitativ...
Many websites have a navigation bar fixed at the ...
1. What are the templates for ASP.NET Web applicat...
This article shares the encapsulation code of Jav...
The hyperlink <a> tag represents a link poin...
MySQL 5.7 installation We are learning MySQL data...
Abstract: Whether at work or in an interview, opt...
This article lists the most commonly used image c...
Autotrash is a command line program that automate...
Exposing network ports In fact, there are two par...
The most common, most commonly used and most gener...
question CSS fixed positioning position:fixed is ...
Learning to use CSS3's 3D effects to create a...