问题表述:
那天晚上一个同学给我发来一个问题,原型大概是“一个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无效),我到百度上又搜索了一下别人是不是也遇到了同样的问题,综合各家说法我最后总结出以下几个问题的重点:
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

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.