1、Box Model(盒模型)

  CSS中的Box Model分为两种:第一种是W3C的标准模型,另一种是IE的传统模型。它们的相同之处是对元素的width、height、padding、border、margin以及元素实际尺寸的计算关系,而它们的不同之处则是两者的计算方法不一致。

  1)、W3C的标准Box Model:

/*外盒尺寸计算(元素空间尺寸)*/
Element空间高度 = content height + padding + border + margin
Element空间宽度 = content weight + padding + border + margin
/*内盒尺寸计算(元素大小)*/
Element Height = content height + padding + border
Element Weight = content weight + padding + border

  2)、IE传统下的Box Model(IE6以下,不含IE6版本)

    

/*外盒尺寸计算(元素空间尺寸)*/
Element空间高度 = Content Height + margin(Height包含了元素内容高度、边框高度、内边距高度)
Element空间宽度 = Content Weight + margin(Height包含了元素内容宽度、边框宽度、内边距宽度)
/*内盒尺寸计算(元素大小)*/
Element Height = content Height(Height包含了元素内容高度、边框高度、内边框高度)
Element Weight = content Weight(Weight包含了元素内容宽度、边框宽度、内边框宽度)

  目前浏览器大部分元素都是基于W3C标准的Box Model,但对于form中的部分元素还是基于传统的Box Model上,比如:input中的submit、reset、button和select等元素,这样,如果我们给其设置border和padding,它也只会向内延伸。

2、Box-sizing : content-box | border-box | inherit

  1)、content-box:此值为其默认值,其让元素维持W3C标准的Box Model展示,也就是说元素的宽度(weight)和高度(height)等于元素边框(border)加上 元素的内边距(padding) 加上 元素内容的宽度(content width)或高度(content height)

  2)、border-box:此值让元素维持IE传统的Box Model(IE6以下版本)展示,也就是说元素的宽度(weight)和高度(height)等于元素内容的宽度(content weight)和高度(content height)。这里的content width和content height包含了元素的border、padding、content width和content height

  box-sizing现在的浏览器都支持,但IE家庭里只有IE8以上版本才支持,虽然现代的浏览器都支持,但有些浏览器还是需要加上自己的前缀,Mozilla需要加上 -moz-,Webkit内核需要加上-webkit-,Presto内核需要加上-0-,IE8需要加上-ms-,所以box-sizing兼容浏览器时需要加上各自的前缀。

/*Content box*/
Element {
  -moz-box-sizing:content-box;
  -webkit-box-sizing:content-box;
  -o-box-sizing:content-box;
  -ms-box-sizing:content-box;
  box-sizing:content-box;          
}
/*Border box*/
Element {
 -moz-box-sizing:border-box;
 -webkit-box-sizing:border-box;
 -o-box-sizing:border-box;
 -ms-box-sizing:border-box;
 box-sizing:border-box;                  
}

 

posted on 2017-04-28 10:51  柠檬小镇  阅读(267)  评论(0编辑  收藏  举报