search
HomeWeb Front-endCSS TutorialHow to clear floating collapse in css

How to clear floating collapse in css

Jun 20, 2018 pm 04:12 PM
cssfloat

This article mainly introduces the method of clearing floating collapse in CSS. It has certain reference value. Now I share it with you. Friends in need can refer to it.

In the process of using CSS , you will more or less encounter the problem of clearing floats. Therefore, this article summarizes 4 methods for clearing floating collapse in p css. I believe it will be helpful for everyone to learn or use p css. Friends in need can take a look below.

What is floating?
The definition says: A floating box can be moved left or right until its outer edge touches the border of the containing box or another floating box. In fact, it means changing the fast-level elements into being able to line up with other fast-level elements.

Floating elements have 4 characteristics:
1. Floating elements will break away from the standard document flow and no longer distinguish between blocks and lines.

2. Floating elements will stick to each other.

3. Floating elements have the effect of "word width".

4. Shrink. A floating element, if width is not set, will automatically shrink to the width of the text.

Similarly on the road of front-end Siege Lion, we are sincerely trying our best to learn and make progress. However, first of all, the wars between major browser manufacturers continued, which made those of us who are studying miserable and miserable. I have to think of various hack methods to solve it. (Yes, this is IE6 that is being complained about!)

Secondly, the document standard flow will also dig a big pit after floating. If a beginner is not careful, the collapsed and out-of-control sub-elements will run around everywhere, making you miserable. So in order to go further and further on the front-end road, let me explain to you four methods to clear floating.

But when we talk about the routine, we also reproduce the big pit that collapsed after the float.

Floating Collapse Pit:
Let’s take a look at a small demo: now there are two p, and p does not have any attributes. There is a li in each p, and these li are floating. First, the html skeleton part

The following is the css style:

Seeing this, if you have never suffered a big loss from floating collapse, you will definitely think that the effect on this webpage is like this

But in fact the browser The final rendering looks like:

The li in the second p is pasted to the edge of the last li in the first p.

The reason is that p has no height and cannot give its own floating children a container.

So the li in the second p is pasted to the edge of the last li in the first p!

This phenomenon is also called floating collapse phenomenon!

After talking about floating collapse, let me return to the topic of today. 4 ways to clear float collapse

Method 1: Add height to the parent of the floated element

If an element is to be floated, then it The ancestor element must have a height. The height of the box can be used to close the float.

As long as it is floated in a box with a height, this float will not affect the subsequent floated elements. So it’s about clearing the impact of floating.

So you only need to set a height: 40px for the superior ul or p of li; (as long as it is the superior element of the floating element.), then the second p can be squeezed down and placed in the first box shown below.

Method 2: clear:both;
But in the actual development process, height rarely appears. Why? Because it can be boosted by content! That is to say, the method 1 we just explained is rarely used in work.

So someone here may have a big idea: Can you clear the float without writing height? Also let the floats not affect each other?

In fact, there is such a solution, which is to add a clear: both to his superior; first of all, Clear means clear, and both means that both left and right floats are cleared. In layman's terms, it means eliminating the influence of others on me.

Although this can eliminate the floating collapse phenomenon, it also has a fatal problem. That is, between two p's, the margin value is invalid and cannot be set. To reiterate, it refers to between two p's.

Method 3: Partition Wall Method
Since using clear: both will cause the margin between the two ps to fail, then someone will start to have a lot of imagination later. . Put an empty box in the middle, and then give that empty box clear: both; (To make it easier for everyone to see the effect, there is no empty box here, but a box with height and color.)

After adding a wall like this, the second p can fall down without interfering with the elements above. Moreover, the second p can still adjust the distance between the two p (the "wall" p is not included) through magin-top, so the partition method spreads as a new routine.

Method 4: Evolved version of the partition method-inner wall method
There is a kind of person in this world - perfectionist, in their Flaws in the worldview are absolutely not allowed. Then in the front-end industry, there are also a group of front-end siege lions who pursue perfectionism. They were completely unacceptable for the small flaws of the partition method, so this group of perfectionist front-end siege lions began to evolve the perfect transformation of his partition method.

So the "inner wall method" was born. Let's take a look at the code structure first:

Just change the position of the wall, and it's perfect Solve the problem that the first p cannot adjust the distance from the following p through margin-bottm.

So for a long time, the "inner wall method" became the mainstream method for major companies to eliminate floating.

Method 5: overflow: hidden;
Overflow means "overflow", and hidden means "hidden".

Too much content overflows the box

overflow:hidden;The content that overflows the box border is hidden.

The original intention of this style is to clear text that overflows outside the box. However, some front-end siege lion engineers discovered that it can be used as a folk remedy. The writing is simple and crude, easy to understand. It is an additional extension in the W3C document, just like the person who invented the motorcycle would never have thought that the subsequent motorcycle stunts could raise the motorcycle head and drive.

So this overflow: hidden; can also be regarded as an "ancestral old folk remedy" that can cure the old problem of floating and collapse. Of course, since it is a "home remedy", there must be reasons why it cannot be solved under some special circumstances. As a result, it just became unorthodox in terms of "folk remedies".

The special case is that when positioning, if the positioned area exceeds the box, overflow: hidden; will cut off the excess part. So in addition to this old problem, overflow: hidden; its simple and crude writing method still has the ability to compete with the "inner wall method".

Summary:

In fact, there are many ways to clear floats, such as adding a float to the collapsed element and adding an absolute positioning ...wait for some methods.

But adding float will only move the level of the collapsed object up. Adding absolute positioning to the collapsed object will also be affected by other positioned areas. Therefore, in addition to eliminating positioning, these routines that require additional additions of other styles are not within the scope of our concern.

We will only talk about these four mainstream routines for clearing floating collapse in the front-end development process. After listening to these four routines, which method will you use to clear floating collapse in the future? It is estimated that many friends will use the "inner wall method" to remove floating ones. Indeed, the inner wall method was very popular in the past. However, as the times continue to advance, many companies no longer simply use the "inner wall method" in the actual development process

I think some people will complain here, it is not that the mainstream clears the floating 4 A trick? These four routines currently have no problems with the "inner wall method". Is it possible to use unpopular ones?

Don’t worry, the comprehensive method of clearing floats now used in enterprises, also known as the inner wall method 2.0


Add to the floating and collapsed boxes A: after pseudo-class

This method has appeared in various large-scale projects. It can be said to be a panacea for clearing floating! Originally there were four good routines, but now in addition to those four routines, there are several routines for clearing floats. I guess some friends are going to faint.

Let us use mind mapping to help you sort out your thoughts!

The reason why I have made such a big circle for you guys. I’m just afraid to tell everyone this method directly. There will definitely be some friends who are wondering why it is written like this. Now I believe that after reading the routine introduction in this article, you will have a deeper understanding of the method of clearing floats.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to solve the problem of invalid adjustment of z-index attribute on div layer

About turning on hardware acceleration in CSS3 Usage and pitfalls

The above is the detailed content of How to clear floating collapse 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
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use