Table of Contents
Float" >Float
1. What is float
2. The impact of floating
3. Floating application
Clear floats" >Clear floats
Clear on child elements
Clear on the parent element, that is, BFC
Home Web Front-end CSS Tutorial CSS Float and Float Clear (BFC) Simple Tutorial

CSS Float and Float Clear (BFC) Simple Tutorial

Apr 04, 2017 am 10:59 AM

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 class="float">float</p>
Copy after login
.float {
  float: left;
  width: 100px;
  height: 100px;
  background-color: #ddd;
}
Copy after login

2. The impact of floating

  1. 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 as

    CSS Float and Float Clear (BFC) Simple Tutorial

    The height of the parent element is collapsed

  • The text is wrapped.

    CSS Float and Float Clear (BFC) Simple Tutorial

    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 the float attribute. The code is as follows:

    
      <p class="float">float</p>
      

    正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素正常元素

    Copy after login
    body {
      background-color: #ccc;
    }
    .float {
      float: left;
      width: 100px;
      height: 100px;
      background-color: #ddd;
    }
    .normal {
      background-color: #fff;
    }
    Copy after login
  • 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.

      <body>
        <p class="left float">left</p>
        <p class="right float">right</p>
        <p class="mid">自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素</p>
      </body>
      Copy after login
      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 值*/
      }
      Copy after login

      Here I deliberately added the margin value, you can see the effect:

      CSS Float and Float Clear (BFC) Simple Tutorial

      Three column layout

      body also falls down along with the margin 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

      <body>
        <p class="left float">left</p>
        <p class="mid">自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素自适应宽度元素</p>
        <p class="right float">right</p>
      </body>
      Copy after login

      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, add clear:left<a href="http://www.php.cn/wiki/974.html" target="_blank"> or </a>clear:both to .normal. The specific usage of clear 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;
      }
      Copy after login

      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> is absolute or fixed), inline block element display: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 not visible (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:

    1. float is not none

    2. position is not <a href="http://www.php.cn/wiki/188.html" target="_blank">static</a> or relative

    3. display is table-cell, table-caption, inline-block, flex, inline-flex

    4. overflow is not visible

    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!

    Statement of this Website
    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

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

    In Vue.js, the placeholder attribute specifies the placeholder text of the input element, which is displayed when the user has not entered content, provides input tips or examples, and improves form accessibility. Its usage is to set the placeholder attribute on the input element and customize the appearance using CSS. Best practices include being relevant to the input, being short and clear, avoiding default text, and considering accessibility.

    What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

    The span tag can add styles, attributes, or behaviors to text. It is used to: add styles, such as color and font size. Set attributes such as id, class, etc. Associated behaviors such as clicks, hovers, etc. Mark text for further processing or citation.

    What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

    REM in CSS is a relative unit relative to the font size of the root element (html). It has the following characteristics: relative to the root element font size, not affected by the parent element. When the root element's font size changes, elements using REM will adjust accordingly. Can be used with any CSS property. Advantages of using REM include: Responsiveness: Keep text readable on different devices and screen sizes. Consistency: Make sure font sizes are consistent throughout your website. Scalability: Easily change the global font size by adjusting the root element font size.

    How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

    There are five ways to introduce images in Vue: through URL, require function, static file, v-bind directive and CSS background image. Dynamic images can be handled in Vue's computed properties or listeners, and bundled tools can be used to optimize image loading. Make sure the path is correct otherwise a loading error will appear.

    What is node in js What is node in js May 07, 2024 pm 09:06 PM

    Nodes are entities in the JavaScript DOM that represent HTML elements. They represent a specific element in the page and can be used to access and manipulate that element. Common node types include element nodes, text nodes, comment nodes, and document nodes. Through DOM methods such as getElementById(), you can access nodes and operate on them, including modifying properties, adding/removing child nodes, inserting/replacing nodes, and cloning nodes. Node traversal helps navigate within the DOM structure. Nodes are useful for dynamically creating page content, event handling, animation, and data binding.

    What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

    Browser plug-ins are usually written in the following languages: Front-end languages: JavaScript, HTML, CSS Back-end languages: C++, Rust, WebAssembly Other languages: Python, Java

    How to set unknown attributes in vscode vscode method to set unknown attributes How to set unknown attributes in vscode vscode method to set unknown attributes May 09, 2024 pm 02:43 PM

    1. First, open the settings icon in the lower left corner and click the settings option. 2. Then, find the CSS column in the jumped window. 3. Finally, change the drop-down option in the unknownproperties menu to the error button.

    What do ref and id in vue do? What do ref and id in vue do? May 02, 2024 pm 08:42 PM

    In Vue.js, ref is used in JavaScript to reference a DOM element (accessible to subcomponents and the DOM element itself), while id is used to set the HTML id attribute (can be used for CSS styling, HTML markup, and JavaScript lookup).

    See all articles