Home Web Front-end HTML Tutorial HTML 外边距折叠,以及消除的办法_html/css_WEB-ITnose

HTML 外边距折叠,以及消除的办法_html/css_WEB-ITnose

Jun 24, 2016 am 11:33 AM

有如下的HTML

<body>    <div class="page">        <div class="mains">            <div class="content"></div>        </div>     </div></body>
Copy after login

以及如下的css

html,body{  padding: 0px;  margin: 0px; width: 100%;  height: 100%;  }html{  background: aliceblue;  }body{  background:antiquewhite;  }.page {  width: 600px; height: 300px;  background: mediumaquamarine;}.mains { width: 300px; height: 200px;background: yellow;}.content{ width: 200px; height: 100px;background:mistyrose;}
Copy after login

在常规流中显示的效果如下(图1)

此时,为.content添加以下样式

.content{width: 200px; height: 100px;background:mistyrose; margin-top: 10px; }
Copy after login

显示效果(图2)

这里可能会出乎很多人的意料,按我们正常的理解,.content外部有.mains的包围,那.content应该是顶部距离.mains 10px才是,可实际情况他们并没有分开,甚至最终的结果感觉是body顶部距离html顶部10px(至于这感觉对不对呢?也许不完全如此).这就是“外边距折叠”

接下来我们再给.mains加上margin-top样式

.mains {width: 300px;height: 200px;background: yellow;margin-top: 10px;}
Copy after login

其显示效果与“图2”一样,那么我们修改.mians的margin-top的值

.mains {width: 300px;height: 200px;background: yellow;margin-top: 20px;}
Copy after login

显示效果如下(图3)

此时的效果并不是相距30px,而是取了两则中最大的值20px.

接着给.page加上“margin-top: 20px;”效果不变,给body加上也是一样的效果。但如果给html加上该样式

html{    background: aliceblue;    margin-top: 20px;   }
Copy after login

显示效果(图4)

从以上结果可得知,在常规流中,html里面的元素(包括body)的外边距都是针对html元素的边缘来确定的。而html元素的外边距是针对浏览器顶部决定的。从其得知,html元素其实生成了BFC.


那么问题来了,如果这样,那设置的html的背景色真的是html元素的吗?(因为设置html的margin-top: 20px;后背景色用的仍是对html元素设置的样式值)会不会其实是浏览器的背景色?


对于“图2”和“图3”,其实我们绝大多数情况要的不是这样的效果,要的其实是针对父元素的margin-top. 对于这种上下级元素而言,他们的margin是针对最近的BFC块来定位的,所以,我们只要能让其父元素生产BFC就可以实现我们想要的效果。例如我对.mains添加以下的样式:

overflow: hidden;
Copy after login

那么我们想要的效果也就出来了,如下所示:

如果我们不在.mains上添加,而是在.page上添加,那结果将会如下所示

将.mains和.page同时加上overflow: hidden; 其结果如下:

这不就是我们想要达到的效果吗~~


以上是针对“父子关系”的元素外边距折叠以及解决的办法,至于兄弟元素的折叠问题就相对简单多了。当然解决的办法也是一样。


推荐该链接供大家学习:

http://www.w3cplus.com/css/understanding-bfc-and-margin-collapse.html


根据w3c规范,两个margin是邻接的必须满足以下条件:

  • 必须是处于常规文档流(非float和绝对定位)的块级盒子,并且处于同一个BFC当中。

  • 没有线盒,没有空隙(clearance,下面会讲到),没有padding和border将他们分隔开

  • 都属于垂直方向上相邻的外边距,可以是下面任意一种情况

    • 元素的margin-top与其第一个常规文档流的子元素的margin-top

    • 元素的margin-bottom与其下一个常规文档流的兄弟元素的margin-top

    • height为auto的元素的margin-bottom与其最后一个常规文档流的子元素的margin-bottom

    • 高度为0并且最小高度也为0,不包含常规文档流的子元素,并且自身没有建立新的BFC的元素的margin-top和margin-bottom


以上的条件意味着下列的规则:

  • 创建了新的BFC的元素(例如浮动元素或者'overflow'值为'visible'以外的元素)与它的子元素的外边距不会折叠

  • 浮动元素不与任何元素的外边距产生折叠(包括其父元素和子元素)

  • 绝对定位元素不与任何元素的外边距产生折叠

  • inline-block元素不与任何元素的外边距产生折叠

  • 一个常规文档流元素的margin-bottom与它下一个常规文档流的兄弟元素的margin-top会产生折叠,除非它们之间存在间隙(clearance)。

  • 一个常规文档流元素的margin-top 与其第一个常规文档流的子元素的margin-top产生折叠,条件为父元素不包含 padding 和 border ,子元素不包含 clearance。

  • 一个 'height' 为 'auto' 并且 'min-height' 为 '0'的常规文档流元素的 margin-bottom 会与其最后一个常规文档流子元素的 margin-bottom 折叠,条件为父元素不包含 padding 和 border ,子元素的 margin-bottom 不与包含 clearance 的 margin-top 折叠。

  • 一个不包含border-top、border-bottom、padding-top、padding-bottom的常规文档流元素,并且其 'height' 为 0 或 'auto', 'min-height' 为 '0',其里面也不包含行盒(line box),其自身的 margin-top 和 margin-bottom 会折叠。





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.

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.

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.

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

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

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

See all articles