awesome-interview/book1/css-bfc/index.html

19 lines
21 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Docusaurus v2.0.0-beta.5">
<link rel="search" type="application/opensearchdescription+xml" title="HZFE - 剑指前端 Offer" href="/awesome-interview/opensearch.xml">
<link rel="preconnect" href="https://hm.baidu.com">
<script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="https://hm.baidu.com/hm.js?c7cd0fd77ac518cc6ef46461cdc9524b";var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(e,c)}()</script><title data-react-helmet="true">BFC 的形成和作用 | HZFE - 剑指前端 Offer</title><meta data-react-helmet="true" name="twitter:card" content="summary_large_image"><meta data-react-helmet="true" property="og:url" content="https://hzfe.github.io/awesome-interview/book1/css-bfc"><meta data-react-helmet="true" name="docsearch:language" content="en"><meta data-react-helmet="true" name="docsearch:version" content="current"><meta data-react-helmet="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-react-helmet="true" property="og:title" content="BFC 的形成和作用 | HZFE - 剑指前端 Offer"><meta data-react-helmet="true" name="description" content="相关问题"><meta data-react-helmet="true" property="og:description" content="相关问题"><link data-react-helmet="true" rel="shortcut icon" href="/awesome-interview/img/favicon.ico"><link data-react-helmet="true" rel="canonical" href="https://hzfe.github.io/awesome-interview/book1/css-bfc"><link data-react-helmet="true" rel="alternate" href="https://hzfe.github.io/awesome-interview/book1/css-bfc" hreflang="en"><link data-react-helmet="true" rel="alternate" href="https://hzfe.github.io/awesome-interview/book1/css-bfc" hreflang="x-default"><link data-react-helmet="true" rel="preconnect" href="https://61YWQXP6JY-dsn.algolia.net" crossorigin="anonymous"><link rel="stylesheet" href="/awesome-interview/assets/css/styles.304d13b7.css">
<link rel="preload" href="/awesome-interview/assets/js/runtime~main.50b7dd91.js" as="script">
<link rel="preload" href="/awesome-interview/assets/js/main.178fbfc5.js" as="script">
</head>
<body>
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
<div><a href="#" class="skipToContent_1oUP">Skip to main content</a></div><nav class="navbar navbar--fixed-top navbarHideable_2qcr"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Navigation bar toggle" class="navbar__toggle clean-btn" type="button" tabindex="0"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/awesome-interview/"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><b class="navbar__title">剑指前端 Offer</b></a></div><div class="navbar__items navbar__items--right"><a href="https://github.com/hzfe/awesome-interview" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link"><span>GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_3J9K"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></span></a><div class="searchBox_1Doo"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div class="main-wrapper docs-wrapper docs-doc-page"><div class="docPage_31aa"><button class="clean-btn backToTopButton_35hR" type="button"><svg viewBox="0 0 24 24" width="28"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z" fill="currentColor"></path></svg></button><aside class="docSidebarContainer_3Kbt"><div class="sidebar_15mo sidebarWithHideableNavbar_267A"><a tabindex="-1" class="sidebarLogo_3h0W" href="/awesome-interview/"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--light_3UqQ"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--dark_hz6m"><b>剑指前端 Offer</b></a><nav class="menu thin-scrollbar menu_Bmed menuWithAnnouncementBar_2WvA"><ul class="menu__list"><li class="menu__list-item"><a class="menu__link" href="/awesome-interview/about">关于我们</a></li><li class="menu__list-item"><a class="menu__link" href="/awesome-interview/">前言</a></li><li class="menu__list-item"><a class="menu__link menu__link--sublist menu__link--active" href="#">模拟题一</a><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/browser-cross-origin">浏览器:浏览器跨域</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/browser-repain-reflow">浏览器:浏览器的重排重绘</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/engineer-webpack-workflow">工程化Webpack 工作流程</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/frame-vue-data-binding">框架Vue 的数据绑定机制</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/frame-vue-computed-watch">框架Vue 的 computed 和 watch 的区别</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/js-closures">基础:闭包的作用和原理</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/js-module-specs">基础:前端模块化规范</a></li><li class="menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/awesome-interview/book1/css-bfc">样式BFC 的形成和作用</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/network-security">网络:前端安全</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/coding-promise">编码:实现一个 Promises/A+</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/algorithm-balanced-binary-trees">算法:平衡二叉树</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/topic-enter-url-display-xx">综合:浏览器从输入网址到页面展现的过程</a></li></ul></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题二</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题三</a></li></ul></nav><button type="button" title="Collapse sidebar" aria-label="Collapse sidebar" class="button button--secondary button--outline collapseSidebarButton_1CGd"><svg width="20" height="20" aria-hidden="true" class="collapseSidebarButtonIcon_3E-R"><g fill="#7a7a7a"><path d="M9.992 10.023c0 .2-.062.399-.172.547l-4.996 7.492a.982.982 0 01-.828.454H1c-.55 0-1-.453-1-1 0-.2.059-.403.168-.551l4.629-6.942L.168 3.078A.939.939 0 010 2.528c0-.548.45-.997 1-.997h2.996c.352 0 .649.18.828.45L9.82 9.472c.11.148.172.347.172.55zm0 0"></path><path d="M19.98 10.023c0 .2-.058.399-.168.547l-4.996 7.492a.987.987 0 01-.828.454h-3c-.547 0-.996-.453-.996-1 0-.2.059-.403.168-.551l4.625-6.942-4.625-6.945a.939.939 0 01-.168-.55 1 1 0 01.996-.997h3c.348 0 .649.18.828.45l4.996 7.492c.11.148.168.347.168.55zm0 0"></path></g></svg></button></div></aside><main class="docMainContainer_3ufF"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_3FnS"><div class="docItemContainer_33ec"><article><div class="tocCollapsible_1PrD tocMobile_3Hoh"><button type="button" class="clean-btn tocCollapsibleButton_2O1e">On this page</button></div><div class="markdown"><header><h1>BFC 的形成和作用</h1></header><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="相关问题"></a>相关问题<a class="hash-link" href="#相关问题" title="Direct link to heading">#</a></h2><ul><li>BFC 有什么用,如何触发</li><li>谈谈你对 BFC 的理解</li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="回答关键点"></a>回答关键点<a class="hash-link" href="#回答关键点" title="Direct link to heading">#</a></h2><p><code>盒模型</code> <code>视觉格式化模型</code> <code>包含块</code> <code>正常流</code></p><p><strong>BFC 是什么</strong></p><p>BFC 全称为 block formatting context中文为“块级格式化上下文”。它是一个只有块级盒子参与的独立块级渲染区域它规定了内部的块级盒子如何布局且与区域外部无关。</p><p><strong>BFC 有什么用</strong></p><ul><li>修复浮动元素造成的高度塌陷问题。</li><li>避免非期望的外边距折叠。</li><li>实现灵活健壮的自适应布局。</li></ul><p><strong>触发 BFC 的常见条件</strong></p><ul><li>&lt;html&gt; 根元素。</li><li>float 的值不为 none。</li><li>position 的值不为 relative 或 static。</li><li>overflow 的值不为 visible 或 clip除了根元素</li><li>display 的值为 table-celltable-caption或 inline-block 中的任意一个。</li><li>display 的值为 flow-root或 display 值为 flow-root list-item。</li><li>flex items即 display 的值为 flex 或 inline-flex 的元素的直接子元素(该子元素 display 不为 flexgrid或 table</li><li>grid items即 display 的值为 grid 或 inline-grid 的元素的直接子元素(该子元素 display 不为 flexgrid或 table</li><li>contain 的值为 layoutcontentpaint或 strict 中的任意一个。</li><li>column-span 设置为 all 的元素。</li></ul><p><strong>提示</strong><code>display: flow-root</code><code>contain: layout</code> 等是无副作用的,可在不影响已有布局的情况下触发 BFC。</p><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="知识点深入"></a>知识点深入<a class="hash-link" href="#知识点深入" title="Direct link to heading">#</a></h2><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="1-前置知识点"></a>1. 前置知识点<a class="hash-link" href="#1-前置知识点" title="Direct link to heading">#</a></h3><p><strong>盒模型box model</strong></p><p><strong>盒模型</strong>描述了一个由元素生成的矩形盒子,<strong>视觉格式化模型</strong>决定这些盒子的大小、位置以及属性(例如颜色、背景、边框尺寸等等)。</p><p>盒子的具体构成如下图所示:</p><p><img src="https://www.w3.org/TR/CSS2/images/boxdim.png" alt="Box model"></p><p><strong>术语解析</strong></p><p>由于视觉格式化模型描述中,有许多相近的术语,在此先行列出解释。</p><ul><li>box盒子一个抽象概念在画布上占据一块空间通常由元素element生成。一个元素可能生成多个盒子如伪元素、list-item 元素)。</li><li>principal box主要盒子元素生成的多个盒子中用来包含它的后代盒子和它生成的内容的盒子也是参与任何定位方案的盒子。</li><li>block-level box块级盒子参与 BFC 布局的盒子,通常由块级元素生成。</li><li>block container box块容器在 CSS2.2 中,除了 table box 或替换元素的主要盒子,一个块级盒子也是块容器,但不是所有的块容器都是块级盒子(如非替换内联块盒子)。块容器主要侧重于其子元素的定位、布局。</li><li>block box块盒子如果一个块级盒子同时也是块容器则可以称作块盒子。</li><li>block当没有歧义时作为 block box, block-level box 或 block container box 的简写。</li></ul><p>(注:盒子有“块盒子”和“块级盒子”,元素只有“块级元素”,而没有“块元素”)</p><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="2-视觉格式化模型visual-formatting-model"></a>2. 视觉格式化模型visual formatting model<a class="hash-link" href="#2-视觉格式化模型visual-formatting-model" title="Direct link to heading">#</a></h3><p>视觉格式化模型描述了用户代理如浏览器在可视化媒体如显示器上处理文档树document tree的过程。下面各小节是视觉格式化模型中与 BFC 强相关的描述:</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="21-包含块containing-block"></a>2.1 包含块containing block<a class="hash-link" href="#21-包含块containing-block" title="Direct link to heading">#</a></h4><p>大部分情况下包含块是一个由元素最近的祖先块容器的内容区域content area确定的矩形区域包含块本身不是盒子是一个矩形区域。元素的大小位置及偏移等布局信息根据包含块的尺寸进行计算。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="22-正常流normal-flow"></a>2.2 正常流normal flow<a class="hash-link" href="#22-正常流normal-flow" title="Direct link to heading">#</a></h4><p>正常流是浏览器的默认布局方式。在正常流中的盒子,属于 BFCIFC或其他格式化上下文之一。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="23-格式化上下文formatting-context"></a>2.3 格式化上下文formatting context<a class="hash-link" href="#23-格式化上下文formatting-context" title="Direct link to heading">#</a></h4><p><strong>格式化上下文</strong>是一系列相关盒子进行布局的环境。不同的格式化上下文有不同的布局规则。目前常见的格式化上下文有以下这些:</p><ul><li>块级格式化上下文BFCblock formatting context</li><li>内联格式化上下文IFCinline formatting context</li><li>弹性格式化上下文FFCflex formatting context在 CSS3 中定义。</li><li>栅格格式化上下文GFCgrid formatting context在 CSS3 中定义。</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="24-独立格式化上下文independent-formatting-context"></a>2.4 独立格式化上下文independent formatting context<a class="hash-link" href="#24-独立格式化上下文independent-formatting-context" title="Direct link to heading">#</a></h4><p>一个盒子要么建立一个新的独立格式化上下文,要么延续其包含块的格式化上下文。除了特殊说明,建立新的格式化上下文就是在建立一个独立格式化上下文。</p><p>当一个盒子建立一个独立格式化上下文时,它创建了一个新的独立布局环境。除了通过改变盒子本身的大小,盒子内部的后代不会影响格式化上下文外部的规则和内容,反之亦然。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="25-块级格式化上下文规范及解析"></a>2.5 块级格式化上下文规范及解析<a class="hash-link" href="#25-块级格式化上下文规范及解析" title="Direct link to heading">#</a></h4><p>根据 W3C CSS2.1 视觉格式化模型一章的定义BFC 相关规范描述如下:</p><ol><li>浮动元素,绝对定位元素,不是块级盒子的块级容器(如 inline-blocktable-cellstable-captions以及 overflow 值不为 visible 的块级盒子,都会为他们的内容创建 BFC触发 BFC 的条件)。</li><li>在 BFC 中,盒子从包含块的顶部开始,在垂直方向上一个接一个的排列。相邻盒子之间的垂直间隙由它们的 margin 值决定。在同一个 BFC 中,相邻块级盒子的垂直外边距会合并(注:参与 BFC 布局的都是块级元素)。</li><li>在 BFC 中每一个盒子的左外边缘margin-left会触碰到包含块的左边缘。即使是存在浮动元素也是如此除非该盒子建立了一个新的 BFC。</li></ol><p>结合规范第三点与<strong>独立格式化上下文</strong>的知识,我们可以有以下推论:</p><ol><li>BFC 内外互不影响。<ol><li>BFC 包含内部的浮动(解决内部浮动元素导致的高度塌陷)。</li><li>BFC 排斥外部的浮动(触发 BFC 的元素不会和外部的浮动元素重叠)。</li><li>外边距折叠的计算不能跨越 BFC 的边界。</li></ol></li><li>各自创建了 BFC 的兄弟元素互不影响(注:在水平方向上多个浮动元素加一个或零个触发 BFC 的元素可以形成多列布局)。</li></ol><p>通过 BFC 可以实现灵活健壮的自适应布局,在一行中达到类似 flexbox 的效果,示例如下:</p><p><a href="https://codepen.io/suzukaze-yoru/pen/ZEKKxJm?editors=1100" target="_blank" rel="noopener noreferrer">两栏自适应布局</a></p><p><img src="https://user-images.githubusercontent.com/4338052/126038001-1828c607-212c-4427-9006-6ee61f1c3979.gif" alt="two-col"></p><p><a href="https://codepen.io/suzukaze-yoru/pen/poPPLWZ?editors=1100" target="_blank" rel="noopener noreferrer">多列自适应布局</a></p><p><img src="https://user-images.githubusercontent.com/4338052/126038024-1160aa58-d21d-48a0-b076-650c4ba6f00d.gif" alt="multi-col"></p><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="参考资料"></a>参考资料<a class="hash-link" href="#参考资料" title="Direct link to heading">#</a></h2><ol><li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flow_Layout/Intro_to_formatting_contexts" target="_blank" rel="noopener noreferrer">块级格式化上下文</a></li><li><a href="https://developer.mozilla.org/en-us/docs/web/css/containing_block" target="_blank" rel="noopener noreferrer">包含块MDN</a></li><li><a href="https://www.w3.org/tr/css22/visudet.html#containing-block-details" target="_blank" rel="noopener noreferrer">包含块W3C</a></li><li><a href="https://developer.mozilla.org/en-us/docs/web/css/visual_formatting_model" target="_blank" rel="noopener noreferrer">视觉格式化模型MDN</a></li><li><a href="https://www.w3.org/tr/css22/visuren.html" target="_blank" rel="noopener noreferrer">视觉格式化模型W3C</a></li><li><a href="https://developer.mozilla.org/en-us/docs/web/css/css_box_model/introduction_to_the_css_box_model" target="_blank" rel="noopener noreferrer">盒模型MDN</a></li><li><a href="https://www.w3.org/tr/css22/box.html" target="_blank" rel="noopener noreferrer">盒模型W3C</a></li></ol></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages navigation"><div class="pagination-nav__item"><a class="pagination-nav__link" href="/awesome-interview/book1/js-module-specs"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">« 基础:前端模块化规范</div></a></div><div class="pagination-nav__item pagination-nav__item--next"><a class="pagination-nav__link" href="/awesome-interview/book1/network-security"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">网络:前端安全 »</div></a></div></nav></div></div><div class="col col--3"><div class="tableOfContents_35-E thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#相关问题" class="table-of-contents__link">相关问题</a></li><li><a href="#回答关键点" class="table-of-contents__link">回答关键点</a></li><li><a href="#知识点深入" class="table-of-contents__link">知识点深入</a><ul><li><a href="#1-前置知识点" class="table-of-contents__link">1. 前置知识点</a></li><li><a href="#2-视觉格式化模型visual-formatting-model" class="table-of-contents__link">2. 视觉格式化模型visual formatting model</a></li></ul></li><li><a href="#参考资料" class="table-of-contents__link">参考资料</a></li></ul></div></div></div><div class="row"><div class="col"><div class="react-utterences"><div>Loading script...</div></div></div><div class="col col--3"></div></div></div></main></div></div></div>
<script src="/awesome-interview/assets/js/runtime~main.50b7dd91.js"></script>
<script src="/awesome-interview/assets/js/main.178fbfc5.js"></script>
</body>
</html>