search
HomeWeb Front-endHTML Tutorialmargin-top失效, css + div 布局之 CSS盒模型 外边距合并问题_html/css_WEB-ITnose

问题表述:

那天晚上一个同学给我发来一个问题,原型大概是“一个maindiv嵌套了一个div.class="headr1" 的div 和一个div.class="header2" 的这样两个 div,他给maindiv 设定了background="black" header1设定background=“red” 。header2 设定background=“blue” 同时给heder1 设定margin-top:10px; 他预想的是内层 div.heder1 的上边距 maindiv 上边有10px的距离 但事实上 出现一个问题   ,内层div 并没有出现他所预想的那种效果,实际效果是 maindiv 仍然紧贴内层div 整个maindiv 上边框距离浏览器上边 增加了10px 的外边距”
问题页面代码:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  2 <html xmlns="http://www.w3.org/1999/xhtml">  3 <head>  4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  5 <title>无标题文档</title>  6 <style type="text/css">  7 *{  8 margin:0;  9 padding:0; 10 } 11 #maindiv{ 12  13 width:800px; 14 height:800px; 15 background-color:#00FFFF; 16 <!-- overflow:hidden; --> 17  18 } 19 .header1{ 20 width:600px; 21 height:100px; 22 margin-top:80px; 23 background-color:#FF0000; 24 overflow:hidden; 25  26 } 27 .header2{ 28 border: 1px solid #00FFFF; 29 width:600px; 30 height:100px; 31 margin-top:20px; 32 background-color:#666666; 33 overflow:hidden; 34  35 } 36 </style> 37 </head> 38  39 <body> 40 <div id="maindiv"> 41   <div class="header1">  42  新华网马尼拉4月23日电(记者赵洁民)中国驻菲律宾大使馆发言人张华23日对媒体说, 43 中国并非如某些人所说的那样,正使中菲在南海黄岩岛的紧张局势升级,而是正使那里的局势降级。 44 张华说,中国在黄岩岛海域的渔政310船和海监船084号22日已离开黄岩岛海域。目前, 45 只有一艘海监船仍在黄岩岛海域执法。他指出:“中国撤出两艘舰船再一次证明,中国并非如某些人说的那样, 46 正使黄岩岛的紧张局势升级,而是使那里的局势降级。” 47 这位发言人的谈话是针对菲律宾一些媒体关于“中国正在使黄岩岛局势升级”的误导。 48 张华强调,中国愿意通过外交手段友好解决这一事件。 49     </div> 50   <div class="header2"> 51 新华网马尼拉4月23日电(记者赵洁民)中国驻菲律宾大使馆发言人张华23日对媒体说, 52 中国并非如某些人所说的那样,正使中菲在南海黄岩岛的紧张局势升级,而是正使那里的局势降级。 53 张华说,中国在黄岩岛海域的渔政310船和海监船084号22日已离开黄岩岛海域。目前, 54 只有一艘海监船仍在黄岩岛海域执法。他指出:“中国撤出两艘舰船再一次证明,中国并非如某些人说的那样, 55 正使黄岩岛的紧张局势升级,而是使那里的局势降级。” 56 这位发言人的谈话是针对菲律宾一些媒体关于“中国正在使黄岩岛局势升级”的误导。 57 张华强调,中国愿意通过外交手段友好解决这一事件。 58 </div> 59 </div> 60  61 </body> 62  63 </html>问题页面效果如图:

在该页面中<!-- overflow:hidden; --> 被注释掉了,此时页面显示的效果是问题描述的效果,上边有空出一大块,最外层的DIV也跟着加了80px外边距。如果把这句去掉则是我的那个同学预想的效果。这个也是我在查阅文档以及上网求助后找到的一个解决方法。这个问题被归结为"外边距合并问题" 。
问题产生的原因:

在经过我用谷歌浏览器的查看DOM 结果后我发现还是因为一些浏览器的代码渲染差别造成的,我就到http://w3school.com.cn/ 上寻求解释。终于在这里


我看出了问题的存在(盒子没有获得 haslayout  造成 margin-top无效),我到百度上又搜索了一下别人是不是也遇到了同样的问题,综合各家说法我最后总结出以下几个问题的重点:

在常规文档流中,2个或以上的块级盒模型相邻的垂直margin会被折叠。最终的margin值计算方法如下:
a、全部都为正值,取最大者;
b、不全是正值,则都取绝对值,然后用正值减去最大值;
c、没有正值,则都取绝对值,然后用0减去最大值。
注意:相邻的盒模型可能由DOM元素动态产生并没有相邻或继承关系。 相邻的盒模型中,如果其中的一个是浮动的(floated),垂直margin不会被折叠,甚至一个浮动的盒模型和它的子元素之间也是这样。 设置了overflow属性的元素和它的子元素之间的margin不会被折叠(overflow取值为visible除外)。 设置了绝对定位(position:absolute)的盒模型,垂直margin不会被折叠,甚至和他们的子元素之间也是一样。 设置了display:inline-block的元素,垂直margin不会被折叠,甚至和他们的子元素之间也是一样。 如果一个盒模型的上下margin相邻,这时它的margin可能折叠覆盖(collapse through)它。在这种情况下,元素的位置(position)取决于它的相邻元素的margin是否被折叠。
a、如果元素的margin和它的父元素的margin-top折叠在一起,盒模型border-top的边界定义和它的父元素相同。
b、另外,任意元素的父元素不参与margin的折叠,或者说只有父元素的margin-bottom是参与计算的。如果元素的border-top非零,那么元素的border-top边界位置和原来一样。
一个应用了清除操作的元素的margin-top绝不会和它的块级父元素的margin-bottom折叠。
注意,那些已经被折叠覆盖的元素的位置对其他已经被折叠的元素的位置没有任何影响;只有在对这些元素的子元素定位时,border-top边界位置才是必需的。 根元素的垂直margin不会被折叠。 解决方案:
在父层div加上:overflow:hidden; 把margin-top外边距改成padding-top内边距; 父元素产生边距重叠的边有不为 0 的 padding 或宽度不为 0 且 style 不为 none 的 border
父层div加: padding-top: 1px;
让父元素生成一个 block formating context,以下属性可以实现  * float: left/right  * position: absolute  * display: inline-block/table-cell(或其他 table 类型)  * overflow: hidden/auto 父层div加:position: absolute;

就此截住!

注:本文的总结也是网上其他前辈的辛劳我这里知识借用了一下,在此做出声明:我参考的博文地址有:
http://blog.sina.com.cn/s/blog_6bec36f9010110w9.html

http://hi.baidu.com/jmtbai/blog/item/a91a136ca2d098eb42169456.html

还有一点是我觉得有必要说一下的就是这个效果在IE里面出现在谷歌里面不出现,我找到了一点资料是关于IE浏览器关于渲染的原理:
hasLayout:http://baike.baidu.com/view/2945869.htm

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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment