


1. Clear float and closed float
The so-called clear float means that the display is correct. It avoids the feature of document flow automatically wrapping floating elements (it is common to set the clear:both; attribute in the footer part);
and closing the float does indeed solve the problem of height collapse, making the wrap element have a height. Suspicious wrapping into floated elements. Therefore, it is more appropriate to call it closed floating.
2. The principle of closed float
There are many commonly used methods to clear floats, which can be divided into two categories
- Add an empty element at the end of the floating element and set clear :both attribute
- parent element sets overflow or display:table;
So what is the principle? Before doing this, you need to understand hasLayout and Block formatting contexts .
The so-called Block formatting contexts refer to block-level formatting contexts, referred to as BFC.
So how to trigger BFC?
It should be noted that display: table It does not create a BFC by itself, but it will generate anonymous boxes, and display:table-cell in the anonymous box can create a new BFC. In other words, it is the anonymous box that triggers the block-level formatting context, and Not display:table. Therefore, the BFC effects created by display:table and display:table-cell are different.
The fieldset element currently does not have any information about this triggering behavior in www.w3.org, and it will not appear until the HTML5 standard. Some browser bugs (Webkit, Mozilla) mention this triggering behavior, but there is no official statement. In fact, even though fieldsets can create new block-level formatting contexts in most browsers, developers should not take this for granted. CSS 2.1 does not define which properties apply to form controls, nor how to style them using CSS. User agents may apply CSS properties to these properties. Developers are advised to consider this support as experimental and may be further standardized in later versions of CSS.
BFC features:
1) Block-level formatting context will prevent margin overlay
When two adjacent block boxes are in the same When inside a block-level formatting context, the vertical margins between them overlap. In other words, if two adjacent block boxes do not belong to the same block-level formatting context, their margins will not overlap.
2) Block-level formatting context will not overlap floating elements
According to regulations, the border of a block-level formatting context cannot overlap the margins of the elements inside it. This means that the browser will create implicit margins for the block-level formatting context to prevent it from overlapping the floating element's margins. For this reason, adding negative margins to a block-level formatting context next to a float will not work (Webkit and IE6 have a problem with this - see this test case).
3) Block-level formatting context can usually contain floats
For details, see: W3C CSS2.1 - 10.6.7 'Auto' heights for block formatting context roots
In layman’s terms: the element that creates a BFC is an independent box. The sub-elements inside will not affect the layout of the outside elements, and vice versa. At the same time, the BFC still belongs to the normal flow in the document.
At this point, you may understand why overflow:hidden or auto can be closed and floated. It is because the parent element creates a new BFC. Regarding Zhang Xinxu's article "Some Understanding of Overflow and Zoom "Clear Float"", I think it is not rigorous enough and has no basis for using packages to explain the principle of closed float. And it is said that "Browsers such as Firefox do not have the concept of haslayout", then modern browsers have BFC. In terms of performance, hasLayout can be equivalent to BFC.
The display engine of IE6-7 uses an internal concept called layout. Since this display engine itself has many defects, it directly leads to many display bugs of IE6-7. When we say that an element "gets layout", or that an element "owns layout", we mean its Microsoft-proprietary property hasLayout http://msdn.microsoft.com/worksh... rties/haslayout .asp is set to true for this purpose. IE6-7 uses the concept of layout to control the size and positioning of elements. Those elements that have layout are responsible for the size and positioning of themselves and their child elements. If an element's hasLayout is false, its size and position are controlled by its nearest ancestor that has a layout.
Conditions for triggering hasLayout:
In IE7, overflow has also become a layout trigger:
IE8 uses a brand new display engine, which is said to not use the hasLayout attribute, thus solving many hated bugs.
To sum up:
Close the float by creating a new BFC in browsers that support BFC (IE8, firefox, chrome, safari);
In browsers that do not support BFC The browser (IE6-7) closes the float by triggering hasLayout.
3. Closed floating method?? Keep improving
Seven methods of closed floating (the seventh one is: after pseudo-element) have been listed above. Through the principles analyzed in the third section, we I found that in fact, more attributes: display: table-cell, display: inline-block, etc. can be closed and floated as long as the BFC is triggered. Comparing from all aspects, the closed floating after pseudo-element is undoubtedly a relatively better solution. Let’s talk about this method in detail below.
.clearfix:after {content:"."; display:block; height:0; visibility:hidden; clear:both; }
.clearfix { *zoom:1; }
1) display:block causes the generated elements to be displayed as block-level elements, occupying the remaining space;
2) height:0 to prevent the height of the generated content from destroying the original layout.
3) visibility:hidden makes the generated content invisible and allows content that may be covered by the generated content to be clickable and interactive;
4) Generate content via content: "." As the last element, whether the content is dotted or anything else is OK. For example, there is a classic content in oocss: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX". In some versions, the content in the content may be empty. A trace of coldness is not recommended. Firefox until 7.0 content:"" will still produce extra gaps;
5) zoom: 1 triggers IE hasLayout.
Through analysis, we found that except for clear: both used to clear floats, the other codes are just to hide the content generated by content. This is why other versions of closed floats have font-size: 0. line-height: 0.
Optimization plan one:
Compared to the empty label closed floating method code seems to be a bit redundant. Through query, it was found that there are A "zero-width space", that is, U 200B. This character itself is invisible, so we can completely omit visibility: hidden
.clearfix:after {content: "200B"; display:block ; height:0; clear:both; }
.clearfix { *zoom:1; }.
Excellence Plan 2:
By Nicolas Gallagher Dashi proposed, original text: A new micro clearfix hack, this method does not solve the gap problem in Firefox.
/* For modern browsers */
.cf:before,.cf:after {
content:"";
display:table;
}
.cf:after { clear:both; }/* For IE 6/7 (trigger hasLayout) */
.cf { zoom:1; }
It should be noted that:
The above method uses the :before pseudo-element. Many people are a little confused about this. When do I need to use before? Why is there no option one? In fact, it is used to handle margin overlap. Since the internal element float creates a BFC, the margin-top of the internal element and the margin-bottom of the previous box overlap. If this is not what you want, then you can add before. If it is just a simple closed float, after is enough! It's not like what Da Mo said in his article "Clear Float": But when you only use clearfix:after, there will be a vertical margin overlay bug in cross-browser compatibility. This is not a bug, but a feature that BFC should have.
Please see the elegant Demo
For more information, please see: "Clearfix Improvement and Overflow:hidden Detailed Explanation [Translation]"
In actual development, improvement plan 1 has Unicode characters that are not suitable for GB2312-encoded pages with embedded CSS. Using plan 7 can completely solve our needs. Improvement plan 2 awaits your further practice. Schemes 3 and 4 close the float through overflow, which actually creates a new block-level formatting context, which will lead to a series of changes in its layout and behavior relative to the float. Clearing the float is just one of a series of changes. Just a function. Therefore, it is unwise to change the global characteristics in order to close the float. The risk is a series of bugs, such as focus in early versions of Firefox, truncation of absolutely positioned layers, etc. Always understand that if you just need to close the float, overflow should not be used, rather than "use with caution" as some articles say.
It took me three days to write this article. If you feel that this article is helpful to you, your message is the greatest support for me. At the same time, due to limited energy, you are welcome to point out the errors and deficiencies in the article and encourage us!
Reference:
Control Block Formatting Context
Reprinted from: http://www.iyunlu.com/view/css-xhtml/55.html

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
