3-3 css3的box-sizing
本节编程练习不计算学习进度,请电脑登录imooc.com操作

css3的box-sizing

Box-sizing 是 CSS3 的Box属性之一,那他当然也遵循CSS的Box model原理。CSS中Box model是分为两种,第一种是W3C的标准模型,另一种是IE的传统模型,他们相同之处都是对元素计算尺寸的模型,具体说就是对元素的width,height,padding,border以及元素实际尺寸的计算关系。

W3C的标准 Box Model:

外盒尺寸计算(元素空间尺寸)

Element空间高度 = content height + padding + border + margin
Element空间宽度 = content width + padding + border + margin

内盒尺寸计算(元素大小)

Element Height = content height + padding + border (Height为内容高度)
Element Width = content width + padding + border (Width为内容宽度)

 

IE传统下 Box Model(IE6以下,不含 IE6 版本或“QuirksMode下IE5.5+”):

外盒尺寸计算(元素空间尺寸)

Element空间高度 = content Height + margin (Height包含了元素内容宽度,边框宽度,内距宽度)
Element空间宽度 = content Width + margin (Width包含了元素内容宽度、边框宽度、内距宽度)

内盒尺寸计算(元素大小)

Element Height = content Height (Height包含了元素内容宽度,边框宽度,内距宽度)
Element Width = content Width (Width包含了元素内容宽度、边框宽度、内距宽度)


box-sizing属性定义盒元素尺寸的计算方法:


正常情况下offsetWidth,offsetHeigth获取元素的尺寸是足够了,但是某些元素比如SVG,MathML返回尺寸出错(这里不考虑)。即便如此CSS3还增加了一个box-sizing选择盒子模型,于是jQuery里面就引入augmentWidthOrHeight这个方法来处理因为box-sizing设置导致的尺寸问题,augmentWidthOrHeight方法其实就是对盒子模型的一个处理,所以jQuery获取一个元素的widht/height 都是,ele.offsetWidt/offsetHeigth + augmentWidthOrHeight()方法才可以。

 

关于augmentWidthOrHeight方法

1.8增加了对css属性box-sizing的支持,需要注意与1.7.2的区别了。
1.7.2及以前的版本无论是否定义box-sizing: border-box返回的都是盒模型中元素内容的宽度或高度,不包括padding和border。
augmentWidthOrHeight方法是特别针对盒子模型的处理,例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "border-box"。这可令浏览器呈现出带有指定宽度和高度的框,并把边框和内边距放入框中。

任务

  1. box-sizing
  2. <!doctype html>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
  6. <script src="http://code.jquery.com/jquery-latest.js"></script>
  7. <style type="text/css">
  8. .imgBox img{
  9. width: 40px;
  10. height: 40px;
  11. padding: 20px;
  12. border: 20px solid orange;
  13. margin: 10px;
  14. }
  15. #contentBox{
  16. -moz-box-sizing: content-box;
  17. -webkit-box-sizing: content-box;
  18. -o-box-sizing: content-box;
  19. -ms-box-sizing: content-box;
  20. box-sizing: content-box;
  21. }
  22.  
  23. #borderBox{
  24. -moz-box-sizing: border-box;
  25. -webkit-box-sizing: border-box;
  26. -o-box-sizing: border-box;
  27. -ms-box-sizing: border-box;
  28. box-sizing: border-box;
  29. }
  30.  
  31. #paddingBox{
  32. -moz-box-sizing: padding-box;
  33. -webkit-box-sizing: padding-box;
  34. -o-box-sizing: padding-box;
  35. -ms-box-sizing: padding-box;
  36. box-sizing:padding-box;
  37. }
  38.  
  39. </style>
  40. <title>jQuery.width</title>
  41. </head>
  42. <body>
  43.  
  44. <div class="imgBox" > <img id="contentBox" src="" alt="" />contentBox </div>
  45. <div class="imgBox" > <img id="borderBox" src="" alt="" />borderBox </div>
  46. <div class="imgBox" > <img id="paddingBox" src="" alt="" />paddingBox </div>
  47. <ul></ul>
  48.  
  49. <script>
  50.  
  51. var contentBox = document.getElementById('contentBox');
  52. var borderBox = document.getElementById('borderBox');
  53. var paddingBox = document.getElementById('paddingBox');
  54.  
  55. $('body').append('<li>contentBox:' + contentBox.offsetWidth + '</li>\n <li>borderBox:' + borderBox.offsetWidth + '</li>\n <li>paddingBox:' + paddingBox.offsetWidth + '</li>')
  56.  
  57. </script>
  58. </body>
  59. </html>
下一节