什么是CSS的盒子模式呢?为何叫它是盒子?先说说咱们在网页设计中常听的属性名:内容(content)、填充(padding)、边框(border)、边界(margin), CSS盒子模式都具有这些属性。浏览器
其实我更喜欢叫padding为内边距margin为外边距。app
一个形象的比喻:测试
把盒子模型比作一个快递箱子。this
content就是箱子里的东西。spa
padding就是箱子内的content和箱子边框border的距离。(固然你须要假设箱子里的东西是悬浮的)设计
margin就能够理解为这个箱子和相邻的箱子的距离。两个箱子的实际距离为两个箱子各自的边距的和。code
上面其实还漏掉了一个offset,下面是实际六拉起的盒子模型截图:orm
标准盒子模型:offset=width+padding+borderblog
IE盒子模型:offset=width+padding+border+marginelement
可是这里有一个问题,测试时候发现每一个IE产生的值都不同,有些奇葩的值让我都不知道为何~表示略蛋疼。
问我为何有IE盒子模型?我只想说这都是微软造孽啊!!!!
一般状况下,使用 标准盒子模型能作到更好的兼容性,比较 微软本身搞一套的标准除了他本身没别人用了。
IE的浏览器都用IE盒子模型么?
首先先了解下<!DOCTYPE>这个东西。传送门
已知这个标签能够控制浏览器如何解释页面的。也通用能够控制页面是用怪异模式仍是标准模式解析。
看看Jquey是如何判断的:
// Construct the test element div = document.createElement("div"); container.appendChild( div ); // Check if table cells still have offsetWidth/Height when they are set // to display:none and there are still other visible table cells in a // table row; if so, offsetWidth/Height are not reliable for use when // determining if an element has been hidden directly using // display:none (it is still safe to use offsets if a parent element is // hidden; don safety goggles and see bug #4512 for more information). // (only IE 8 fails this test) div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>"; tds = div.getElementsByTagName( "td" ); isSupported = ( tds[ 0 ].offsetHeight === 0 ); tds[ 0 ].style.display = ""; tds[ 1 ].style.display = "none"; // Check if empty table cells still have offsetWidth/Height // (IE <= 8 fail this test) support.reliableHiddenOffsets = isSupported && ( tds[ 0 ].offsetHeight === 0 ); // Figure out if the W3C box model works as expected div.innerHTML = ""; div.style.width = div.style.paddingLeft = "1px"; jQuery.boxModel = support.boxModel = div.offsetWidth === 2;
奇怪的是1.8之后Jquery移除了这个属性,难道这样判断也失效了吗?但愿有知道的朋友能够解惑。