search
HomeWeb Front-endCSS TutorialFloat attribute in css

Float attribute in css

Feb 25, 2017 pm 02:39 PM

1. Characteristics of Float

1. Apply text around images

2. Create a block-level box

3. Multi-column floating layout

4. The width and height of floating elements are adaptive, but their values ​​can be set.

2. Core solved problems

Text surrounds the image: img tag and multiple text tags are placed in a container, if img floats , the text label will surround the image.

<img src="/static/imghwm/default1.png"  data-src="../img/a.jpg"  class="lazy"     style="max-width:90%" alt="">
        <p>001文件内容文件内容文内容文件内容<br/>
        文件内容文件内容文件内容文件内容文件内内容文件内容<br/>
        文件内容文件内容文件内容文件内容文件内容<br/> 
        文件内容文件内容文件内容文件内容文内容文件内容文件内容<br/>
        <p>p标签文件内容文件内容文件内容文件内容文件内容文容文件内容文件内容文件内容</p>
        <p>p标签文件内容文件内容文件内容文件内容文件内容文件内容</p>
        文件内容文件内容文件内容文件件内容文件内容文件内容<br/>
        文件内容文件内容文件内容文件内容文件内容文件内容文件内容文件内容<br/> 
        文件内容文件内容文件内容文件内容文件内容文件内内容文件内容<br/>
        文件内容文件内容文件内容文件容文件内容文件内容<br/>
        文件内容文件内容文件内容文件容文件内容文件内容<br/>
        </p>

Float attribute in css

2.1 This is a question

Floating elements vs. normal elements Adjacent, and there is no clear float between the floating element and the normal element. At this time, the normal element will be covered by the floating element, but the contained content will be displayed around the floating element.

<p style="width: 100px; height: 200px; background: red;float: left;" >001</p> 
<p style="width: 100px; height: 200px; background: gray;float: none;" ><p>002</p></p>

001 floats, 002 does not float, but the 002 element itself is covered by 001, but the content is displayed around 001.

Float attribute in css

3. Non-core and main application areas

Column layout: let the blocks be horizontal first Arrange, and then start a new line with the excess part.

Main Features

1. Parent height collapse (this is also a serious problem)

.wrap{
           background:red; 
           padding:10px;
           width:auto;
        }
        .left{
            background:gray;
            width:200px;
            height:100px;
            float:left;
        }
        .right{
            background:yellow;
            width:100px;
            height:100px;
            float:left;
        }

<p class="wrap">
        <p class="left">left</p>
        <p class="right">right</p>
</p>

Float attribute in css

2. The width and height become adaptive sub-elements, but the width and height settings are valid

.wrap{
           background:red; 
           padding:10px;
           float:left;
        } 
        .left{
            width:100px;
            background:gray;
        }
        .right:{
            width:200px;
            background:yellow;
        }

<p class="wrap">
        <p class="left">left</p>
        <p class="right">right</p>
</p>

Float attribute in css

2. Solve the problem of high collapse

First we You need to understand the two basic concepts of BFC and IFC, because they are closely related to browser rendering.

1.BFC (block-level formatting context)

It is an independently rendered area, stipulating how to lay out the interior of the area, and has nothing to do with the outside. The main rules are as follows:

1.1 The internal boxes will be placed vertically, one after another

1.2 The vertical distance of the Box is determined by margin, and the margins of two adjacent boxes belonging to the same BFC will overlap

1.3 The BFC area will not overlap with float

.head{
            background:pink;
            margin: 20px 0px;
            height:100px;
        }
        .wrap{
           background:red; 
           padding:10px;
           margin:20px 0px;
           overflow:hidden;
        } 
        .left{
            width:100px;
            background:gray; 
            margin:10px 0px;
        }
        .right:{
            width:200px;
            background:yellow;  
            margin:20px 0px;
        }

<p class="head">head</p>
    <p class="wrap">
        <p class="left">left</p>
        <p class="right">right</p>
    </p>

Float attribute in css

.head and .wrap have 20px margins at the top and bottom, but they overlap; between

.head and .left, .head has a 20px margin. .left has a 10px margin and no overlap occurs because .wrap creates a BFC (overflow:hidden).

1.4 The left margin of each box is in contact with the left side of the containing border box (the same is true for the right side), and the same is true for floats

2. IFC (row-level formatting context)

Boxes are placed horizontally one after the other starting from the top of the containing block. The space occupied by the horizontal margins, borders, and padding are all put together (display is inline, inline-block; elements with inline characteristics have the following characteristics). The rules are as follows:

2.1 The width and height cannot be specified

2.2 Margin, Padding, and border are invalid in the vertical direction

2.3 The left side of the line box is close to the left side of the containing block, and the line The right side of a box is flush with the right side of its containing box, and a float can be inserted between the edge of the containing block and the line box.

2.4 The height of the inline box is determined by line-height.

For examples in this section, please refer to the inline element in the display chapter.

3. Solution

It is mainly implemented based on the principle of BFC. Because BFC renders the entire area, the width and height will also be calculated. This is also the legendary solution for clearing floats

3.1 Method of creating BFC for parent container

3.1.1 Method of creating BFC

a) Float values ​​other than none;

b) Overflow values ​​other than visible;

c) Display values ​​are table-cell, table-caption, inline-block, flex, inline-flex, etc.

d) Position value is absloute, fixed

e) Fieldset element

3.1.2 Clear float

a) Float, overflow, and display can be cleared in three ways. However, although position and fieldset create bfc, they cannot clear the float (that is, they cannot solve the problem of height collapse). The main reason is that both position and fieldset require child elements to expand the height of the parent container, but there is no height after the child element is floated, so it fails.

b) Float, overflow, display sample code:

.wrap{
            background: gray;
            padding: 10px;
            overflow: auto;
        }
        .left, .right{
            background: red;
            float: left;
            width: 200px;
            height: 100px;
        }
        .right{
            background: yellow;
        }
        .footer{
            background: pink;
        }

<p class="wrap" >
        <p class="left">left</p>
        <p class="right">right</p>
    </p>
<p class="footer">footer</p>

Float attribute in css

3.1.3 最后一个子元素clear:both

利用clear:both触发父容器重新计算高度的原理实现,示例代码如下:

.wrap{
            background: gray;
            padding: 10px; 
        }
        .left, .right{
            background: red;
            float: left;
            width: 200px;
            height: 100px;
        }
        .right{
            background: yellow;
        }
        .footer{
            background: pink;
        }
        .clear{
            clear: both;
            zoom: 1;
        }

<p class="wrap" >
        <p class="left">left</p>
        <p class="right">right</p>
        <p class="clear"></p>
    </p>
<p class="footer">footer</p>

3.1.4 After添加最后一个子元素

利用css的:after伪元素实现,动态插入元素并清除浮动:

.wrap{
            background: gray;
            padding: 10px; 
        }
        .wrap:after{
            content: &#39;&#39;;
            display: block;
            overflow: hidden;
            clear: both;
        }
        .left, .right{
            background: red;
            float: left;
            width: 200px;
            height: 100px;
        }
        .right{
            background: yellow;
        }
        .footer{
            background: pink;
        }

<p class="wrap" >
        <p class="left">left</p>
        <p class="right">right</p> 
    </p>
<p class="footer">footer</p>

4. 总结

1. 利用bfc方式清除浮动,简单、浏览器支持良好,但在IE6-版本支持存在问题。但是存在以下局限性,要适环境而用:

    a) Overflow方式:滚动条会被隐藏,如果子内容超高则存在显示不全的问题;

    b) Float方式:让父容器浮动,那么就存在对父容器同辈元素的影响;

    c) Dipslay方式:让父容器变为table或者flex等,都存在不明确的影响,大家都不推荐使用。

2. 最佳解决方案:利用:after添加一个伪元素并给予clear:both和zoom:1来实现清除浮动,兼容性好,对环境影响最小。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

更多Float attribute in css相关文章请关注PHP中文网!

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
A Little Reminder That Pseudo Elements are Children, Kinda.A Little Reminder That Pseudo Elements are Children, Kinda.Apr 19, 2025 am 11:39 AM

Here's a container with some child elements:

Menus with 'Dynamic Hit Areas'Menus with 'Dynamic Hit Areas'Apr 19, 2025 am 11:37 AM

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

Improving Video Accessibility with WebVTTImproving Video Accessibility with WebVTTApr 19, 2025 am 11:27 AM

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

Weekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteWeekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteApr 19, 2025 am 11:25 AM

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

Making width and flexible items play nice togetherMaking width and flexible items play nice togetherApr 19, 2025 am 11:23 AM

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

Position Sticky and Table HeadersPosition Sticky and Table HeadersApr 19, 2025 am 11:21 AM

You can't position: sticky; a

Weekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryWeekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryApr 19, 2025 am 11:18 AM

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

IndieWeb and WebmentionsIndieWeb and WebmentionsApr 19, 2025 am 11:16 AM

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

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.