Table of Contents
什么是BFC?
Box:CSS布局的基本单位
Formatting Context
BFC定义
BFC布局规则
哪些元素会生成BFC
BFC作用与原理
1. 自适应两栏布局
显示效果:
根据BFC布局规则第3条:
根据BFC布局规则第四条:
2. 清除内部浮动
根据BFC布局规则第六条:
效果如下:
3. 防止垂直margin重叠
根据BFC布局规则第二条:
总结
其实以上的几个例子都体现了BFC布局规则第五条:
参考
Home Web Front-end HTML Tutorial BFC(Block formatting contexts)初探_html/css_WEB-ITnose

BFC(Block formatting contexts)初探_html/css_WEB-ITnose

Jun 21, 2016 am 08:58 AM

前端工程师求职面试的时候,或多或少都会问到一点BFC(Block formatting contexts)相关的知识。在平时的开发中我也遇到过BFC方面的问题,但是没有好好整理过这方面的知识,所以别人问我的时候,我都不敢说自己知道这么个概念o(╯□╰)o。这两天,我仔细阅读了一些文章来填补这块知识漏洞。

什么是BFC?

在理解BFC是什么之前,需要了解Box、Formatting Context的概念

Box:CSS布局的基本单位

Box是CSS布局的对象和基本单位,直观点来说,就是一个页面是由很多个Box组成的。元素的类型和display属性,决定了这个Box的类型。不同类型的Box,会参与不同的Formatting Context(一个决定如何渲染文档的容器),因此Box内的元素会以不同的方式渲染。盒子分类:

  • block-level box:display属性为block, list-item, table的元素,会生成block-level box。并且参与block formatting context;

  • inline-level box:display属性为inline, inline-block, inline-table的元素,会生成inline-level box。并且参与inline formatting context;

  • run-in box:display属性为run-in的元素,根据上下文决定对象是内联对象还是块级对象。CSS3属性!

  • Formatting Context

    Formatting Context是W3C CSS2.1规范中的一个概念。它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用。最常见的Formatting Context有Block Formatting Context(BFC)和Inline Formatting Context(IFC)。

    BFC定义

    BFC(Block Formatting Context)块级格式化上下文,它是一个独立的渲染区域,只有Block-level box参与,它规定了内部的Block-level Box如何布局,并且与这个区域外部毫不相干。

    BFC布局规则

  • 内部的Box会在垂直方向,一个接一个地放置

  • Box垂直方向的距离由margin决定,属于同一个BFC的两个相邻Box的margin会发生重叠

  • 每个元素的margin box的左边,与包含块border box的左边相接触(对于从左往右的格式化,否则相反)

  • BFC的区域不会与float box重叠

  • BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此

  • 计算BFC的高度时,浮动元素也参与计算

  • 哪些元素会生成BFC

  • 根元素

  • float属性不为none

  • position为absolute或fixed

  • display为inline-block, table-cell, table-caption, flex, inline-flex

  • overflow不为visible

  • BFC作用与原理

    1. 自适应两栏布局

    1

    <style>    body {    width: 300px;    position: relative;  }  .aside {    width: 100px;    height: 150px;    float: left;    background-color: #f66;  }  .main {    height: 200px;    background-color: #fcc;  }</style><body>    <div class="aside"></div>  <div class="main"></div></body>

    Copy after login

    显示效果:

    根据BFC布局规则第3条:

    1

    每个元素的margin box的左边, 与包含块border box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。

    Copy after login

    因此,虽然存在浮动的元素aside,但main的左边依然会与包含块的左边相接触

    根据BFC布局规则第四条:

    1

    BFC的区域不会与float box重叠

    Copy after login

    我们可以通过通过触发main生成BFC, 来实现自适应两栏布局:

    1

    .main {    height: 200px;    background-color: #fcc;    overflow: hidden;  }

    Copy after login

    当触发main生成BFC后,这个新的BFC不会与浮动的aside重叠。因此会根据包含块的宽度,和aside的宽度,自动变窄。效果如下:

    2. 清除内部浮动

    1

    <style>    .par {    border: 5px solid #fcc;    width: 300px;  }  .child {    border: 5px solid #f66;    width: 100px;    height: 100px;    float: left;  }</style><body>    <div class="par">    <div class="child"></div>    <div class="child"></div>  </div></body>

    Copy after login

    显示效果:

    根据BFC布局规则第六条:

    1

    计算BFC的高度时,浮动元素也参与计算

    Copy after login

    为达到清除内部浮动,我们可以触发par生成BFC,那么par在计算高度时,par内部的浮动元素child也会参与计算

    1

    .par {    border: 5px solid #fcc;    width: 300px;    overflow: hidden;  }

    Copy after login

    效果如下:

    3. 防止垂直margin重叠

    1

    <style>    p {    width: 200px;    line-height: 100px;    margin: 100px;    font-weight: bold;    color: #f55;    background-color: #fcc;    text-align: center;  }</style><body>    <p>heihei</p>  <p>gagaga</p></body>

    Copy after login

    显示效果

    两个p之间的距离为100px,发生了margin重叠

    根据BFC布局规则第二条:

    1

    Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠margin重叠规则:* 两个相邻的外边距都是正数时,折叠结果是它们两者之间较大的值* 两个相邻的外边距都是负数时,折叠结果是两者绝对值的较大值* 两个外边距一正一负时,折叠结果是两者的相加的和

    Copy after login

    我们可以在p外面包裹一层容器,并触发该容器生成一个BFC。那么两个P便不属于同一个BFC,就不会发生margin重叠了

    1

    <style>    .wrap {    overflow: hidden;  }  p {    width: 200px;    line-height: 100px;    margin: 100px;    font-weight: bold;    color: #f55;    background-color: #fcc;    text-align: center;  }</style><body>    <p>heihei</p>  <div class="wrap"><p>gagaga</p></div></body>

    Copy after login

    显示效果:

    总结

    其实以上的几个例子都体现了BFC布局规则第五条:

    1

    BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此!

    Copy after login

    因为BFC内部的元素和外部的元素绝对不会互相影响,因此, 当BFC外部存在浮动时,它不应该影响BFC内部Box的布局,BFC会通过变窄,而不与浮动有重叠。同样的,当BFC内部有浮动时,为了不影响外部元素的布局,BFC计算高度时会包括浮动的高度。避免margin重叠也是这样的一个道理。

    参考

  • 前端精选文摘:BFC 神奇背后的原理

  • 我对BFC的理解

  • 谈外margin collapsing(外边距叠加)

  • 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

    Video Face Swap

    Video Face Swap

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

    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)

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

    GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

    How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

    The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

    How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

    To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

    Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

    The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

    See all articles