Float
1. What is float
When the <a href="http://www.php.cn/wiki/919.html" target="_blank">float</a>
attribute of an element If it is not none
, a float will occur.
<p>float</p>
.float { float: left; width: 100px; height: 100px; background-color: #ddd; }
2. The impact of floating
Floating will cause elements to break away from the document flow. The specific performance is:
-
The parent element is highly collapsed, that is, it will not contain floating elements.
For example, the above code will appear asThe height of the parent element is collapsed
-
The text is wrapped.
Text wrapping effect
You can notice that the width of the.normal
element here covers.float
element, but there is no text under the.float
element, which means that the text is "squeezed" out. This is because although it will break away from the document flow, it will not Break away from the text flow. This effect is also the original intention of thefloat
attribute. The code is as follows:<p>float</p>
正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素
body { background-color: #ccc; } .float { float: left; width: 100px; height: 100px; background-color: #ddd; } .normal { background-color: #fff; }
The margins of floating elements will not be merged.
For related content about margin merging, you can click here. Once the
element is floated, it will become an inline block element, that is, <a href="http://www.php.cn/wiki/927.html" target="_blank">display</a>: inline-block
.
3. Floating application
The text wrapping mentioned above.
-
Write a three-column layout with fixed width on the left and right and adaptive middle.
<p>left</p> <p>right</p> <p>自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素</p>
body { background-color: #ccc; } .float { float: left; width: 100px; height: 100px; background-color: #ddd; } .left { float: left; } .right { float: right; } .mid { height: 100px; background-color: #fff; margin: 200px; /*故意加上了上下 margin 值*/ }
Here I deliberately added the
margin
value, you can see the effect:Three column layout
body
also falls down along with themargin
of.mid
. This can be explained by the merging of margins introduced earlier.ps: When I first wrote this three-column layout, the html was written like this
<p>left</p> <p>自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素</p> <p>right</p>
As above, write the middle adaptive element in the middle. In fact, this is more logical. But if you write it like this, it won't work. The elements on the right will fall down, because the
.mid
element is a block-level element and will occupy the entire line..left
will not fall down. Because it is originally a floating element separated from the document flow.
Clear floats
Clear on child elements
Here I only write methods that do not produce meaningless tags.
If there is a sibling element behind the floating element, you can add the
<a href="http://www.php.cn/wiki/917.html" target="_blank">clear</a>
attribute to its sibling element.
If the text surrounds that part of the code, addclear
:left<a href="http://www.php.cn/wiki/974.html" target="_blank"> or </a>clear:both
to.normal
. The specific usage ofclear
will not be elaborated here.-
Add pseudo-class or pseudo-element to the element to be cleared.
.float::after { content: ''; display: block; visiability: hidden; height: 0; clear: both; }
For information on the use of ::after, please see the MDN documentation.
Clear on the parent element, that is, BFC
BFC (Block Formatting Context), that is, block-level formatting context, its official explanation Yes:
Floating, Absolutely positioned elements (
<a href="http://www.php.cn/wiki/902.html" target="_blank">position</a>
isabsolute
orfixed
), inline block elementdisplay:inline-block
, tablecelldisplay:table-cell
, table titledisplay:table-caption
and<a href="http://www.php.cn/wiki/923.html" target="_blank">overflow</a>
elements whose attribute value is notvisible
(except that the value is propagated to the viewport<a href="http://www.php.cn/css/css-rwd-viewport.html" target="_blank">viewport</a>
) will create a new block-level formatting context.
In summary, it must meet one of the following conditions:
float
is notnone
position
is not<a href="http://www.php.cn/wiki/188.html" target="_blank">static</a>
orrelative
display
istable-cell
,table-caption
,inline-block
,flex
,inline-flex
overflow
is notvisible
As long as you add any of the above attributes to the parent element to meet the conditions, that is, adding BFC to the parent element can clear the floating of the child element.
The above is the detailed content of CSS Float and Float Clear (BFC) Simple Tutorial. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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

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.

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

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

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

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


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

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

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),

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.

Notepad++7.3.1
Easy-to-use and free code editor
