search
HomeWeb Front-endHTML TutorialCSS-float float_html/css_WEB-ITnose

Float is a powerful attribute, but it will also trouble us if we don’t know how it works. This article mainly introduces the principle and basic usage of float.

We can see Let’s go to the application of float in the printing world - magazines. Many magazine articles have an image on the left, and the text on the right floats around the image on the left. In the world of HTML/CSS, there are floating-style images, and the text will be wrapped around it. Around, just like the layout of a magazine. Pictures are just one of the many applications of the floating attribute. We can also use floating to achieve a two-column (multi-column) layout. In fact, we can use floating (block elements) on any HTML element ).

Definition of float

Definition from W3C:

A float is a box that is shifted to the left or right on the current line. The most interesting characteristic of a float (or “floated” or “floating” box) is that content may flow along its side (or be prohibited from doing so by the “clear” property). Content flows down the right side of a left-floated box and down the left side of a right-floated box.

To put it simply, the element that is set to float will float to the corresponding left or right, and the content will be filled to the corresponding floating element. Place.

Floating has four attribute values: left|right|inherit|none, which are left floating, right floating, inheritance (floating attribute value from parent element) and none.

Let’s take an example of magazine effect:

Magazine effect floating

The style of the picture is:

img {     float: right;    margin: 10px;}

Performance of floating

HTML has a rule: normal document flow. Under normal document flow, each block element (div, p, h1...) will wrap. And floating elements will wrap under normal document flow. The document flow is laid out first, and then separated from the normal document flow and displayed to the left or right of its parent element. Therefore, the floating element no longer wraps but is displayed next to the element.

Look at an example: no setting The style of the three floating blocks

block elements:

.block {     width: 200px;    height: 200px;}

These three blocks are displayed under the normal document flow, each element is displayed under the previous element Right below, now we set all three blocks to left float:

.block {     float: left;    width: 200px;    height: 200px;}

The effect can be seen here: left float

After floating, each block element will follow Behind the floating element. If the parent element does not have enough width, the floating element will wrap automatically. Look at this example, and then try to shrink the browser window, the floating block element will wrap automatically.

Clear floats

Floating elements are out of the normal document flow, so they will have an impact on the document structure after floating. Therefore, we must also Clear the float accordingly to restore the document structure to the normal document flow. There are many ways to clear the float. The first and most basic one is the corresponding clear attribute.

The attribute value of clear: left|right|both| inherit|none, the corresponding float corresponds to the corresponding clear float. Let's look at an example of floating. There are four block elements in total. The first two are floating and the last two are not floating. The structure is as follows:

<div class="block pink float"></div><div class="block blue float"></div><div class="block green"></div><div class="block orange"></div>

.block {    width: 200px;    height: 200px;}.float { float: left; }.pink { background: #ee3e64; }.blue { background: #44accf; }.green { background: #b7d84b; }.orange { background: #E2A741; }

Look here for the result

Hey, where did the green block go? Then look at the source code, select the HTML of the green block, it turns out that it is in pink Just below the color block. The two floated elements appear as we would expect, floating left and in a row, but they are out of the normal document flow, as if they are not there. So the first one is not floated The elements occupy their positions and are covered by the floating elements. So we need to clear the floats and let the green blocks display.

The code structure is as follows:

<div class="block pink float"></div><div class="block blue float"></div><div class="block green clear"></div><div class="block orange"></div>

.block {    width: 200px;    height: 200px;}.float { float: left; }.clear { clear: left; }.pink { background: #ee3e64; }.blue { background: #44accf; }.green { background: #b7d84b; }.orange { background: #E2A741; }

Through the clear:left css style, the green block is displayed. Correctly clearing floats can allow elements to float without affecting the normal document flow. The effect after clearing floats

Use float attribute for layout

Now that we are using DIV layout, DIV float is what most of us like to use to implement column layout. One method. Let’s look at a simple two-column layout.

The CSS is as follows:

#container {    width: 960px;    margin: 0 auto;}#content {    float: left;    width: 660px;    background: #fff;}#navigation {    float: right;    width: 300px;    background: #eee;}#footer {    clear: both;    background: #aaa;    padding: 10px;}

By setting #content to float left and #navigation to float right, The total width is #container to implement a two-column layout. It should be noted that #footer must clear the float, because there are both left and right floats at the same time, so use clear:both to clear the floats on both sides.

Let’s take a look at the result if #footer is not cleared of floats. Click here

Float priority

Floating elements are placed in different places in HTML The position will also produce different display results. Let's take a look at an example. Click here

In the example, the image floats to the right and is before the unfloated element P in the HTML structure. The code structure is as follows:

<div id="container">    <img  src="/static/imghwm/default1.png"  data-src="image.gif"  class="lazy" / alt="CSS-float float_html/css_WEB-ITnose" >    <p>This is some text contained within a small-ish box. I'm using it as an example of how placing your floated elements in different orders in your HTML can affect your layouts. For example, take a look at this great photo placeholder that should be sitting on the right.</p></div>

#container {    width: 280px;    margin: 0 auto;    padding: 10px;    background: #aaa;    border: 1px solid #999;}img {    float: right;}

Now we change the structure of HTML and put the floating element IMG behind the P element.

<div id="container">    <p>This is some text contained within a small-ish box. I'm using it as an example of how placing your floated elements in different orders in your HTML can affect your layouts. For example, take a look at this great photo placeholder that should be sitting on the right.</p>    <img  src="/static/imghwm/default1.png"  data-src="image.gif"  class="lazy" / alt="CSS-float float_html/css_WEB-ITnose" ></div>

Click here for specific effects

图片这时已经没有包含在#container里,因为浮动优先原则里,之前的例子都是浮动元素在未浮动元素之前,所以得到的结果都是在我们的预期之内,而现在我们没有按照这个规则写我们的HTML结构,所以图片溢出在了包含它的父元素之外,这是由于collapsing.

当一个父元素包含了一些浮动元素,而这些浮动元素并没有向它应该的方式围绕在未浮动元素周围时collapsing就出现了.正如上面的例子中,浮动元素IMG就像并不在#container中一样.

一个最常见去修复这个问题的方式就是在我们的浮动元素后面添加一个有清除浮动属性的元素,跟我们之前清除浮动的方式很像,只是新增了一个额外的元素,代码看下面:

<div id="container">    <p>This is some text contained within a small-ish box. I'm using it as an example of how placing your floated elements in different orders in your HTML can affect your layouts. For example, take a look at this great photo placeholder that should be sitting on the right.</p>    <img  src="/static/imghwm/default1.png"  data-src="image.gif"  class="lazy" / alt="CSS-float float_html/css_WEB-ITnose" >    <div   style="max-width:90%"></div></div>

但是新增了一行额外的代码并不是一个很好的方法,下面我们有几个其他的方法.

现在有一个HMTL,一个父元素有三个浮动的图片,代码结构如下:

<div id="container">    <img  src="/static/imghwm/default1.png"  data-src="image.gif"  class="lazy" / alt="CSS-float float_html/css_WEB-ITnose" >    <img  src="/static/imghwm/default1.png"  data-src="image.gif"  class="lazy" / alt="CSS-float float_html/css_WEB-ITnose" >    <img  src="/static/imghwm/default1.png"  data-src="image.gif"  class="lazy" / alt="CSS-float float_html/css_WEB-ITnose" ></div>

#container {    width: 260px;    margin: 0 auto;    padding: 10px 0 10px 10px;    background: #aaa;    border: 1px solid #999;}img {    float: left;    margin: 0 5px 0 0;}

我们可以很快的意识到父元素并没有包含浮动的图片,因为浮动元素并不在文档流中,所以对于父元素来说它是空的,结果看这里

现在我们用CSS来修复这个问题而不是添加额外的HTML标记.有一个方法能让父元素自己清除它包含的浮动元素的浮动-overflow:hidden.要注意的是overflow属性并不是为清除浮动而设计的,它可以隐藏内容和滚动条.现在我们在#container上使用这个属性.

#container {    overflow: hidden;    width: 260px;    margin: 0 auto;    padding: 10px 0 10px 10px;    background: #aaa;    border: 1px solid #999;}

显示结果戳这里

还有清除浮动的方法就是使用伪类选择器:

#container:after {    content: ".";    display: block;    height: 0;    clear: both;    visibility: hidden;}

总结:

浮动可以让你的布局变得随意和响应式,同时清除浮动的方式也有很多种,选择你最习惯和喜欢的方式,让布局变得更加优美.

 

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
Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone?What are the disadvantages of using native select on your phone?Apr 30, 2025 pm 03:12 PM

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

How to optimize collision handling of third-person roaming in a room using Three.js and Octree?How to optimize collision handling of third-person roaming in a room using Three.js and Octree?Apr 30, 2025 pm 03:09 PM

Use Three.js and Octree to optimize collision handling of third-person roaming in the room. Use Octree in Three.js to implement third-person roaming in the room and add collisions...

What problems will you encounter when using native select on your phone?What problems will you encounter when using native select on your phone?Apr 30, 2025 pm 03:06 PM

Issues with native select on mobile phones When developing applications on mobile devices, we often encounter scenarios where users need to make choices. Although native sel...

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

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools