盒模型
CSS 布局:盒模型
BoxModel
在

而这些属性我们可以把它转移到我们日常生活中的盒子
如果 box-sizing
为默认值,width
, min-width
, max-width
, height
, min-height
与 max-height
控制内容大小。
内边距区域
内边距与内容边界之间的空间可以由 padding-top
, padding-right
, padding-bottom
, padding-left
和简写属性 padding
控制。
边框区域border-width
及简写属性 border
控制。
外边距区域大小由 margin-top
, margin-right
, margin-bottom
, margin-left
及简写属性 margin
控制。
在 外边距合并 的情况下,由于盒之间共享外边距,外边距不容易弄清楚。
最后,请注意,对于非替换的行内元素来说,尽管内容周围存在内边距与边框,但其占用空间line-height
属性决定。
- 每个
HTML 标记都可看作一个盒子; - 每个盒子都有:边界、边框、填充、内容四个属性;
- 每个属性都包括四个部分:上、右、下、左;这四部分可同时设置,也可分别设置;
标准文档流与元素分类
层叠上下文与层叠顺序
height & width
line-height: 行高
何为行高?顾名思意指一行文字的高度。具体来说是指两行文字间基线之间的距离。基线实在英文字母中用到的一个概念,我们刚学英语的时使用的那个英语本子每行有四条线,其中底部第二条线就是基线,是

代码:



offsetHeight & clientHeight & scrollHeight
clientHeight: 返回以Pixels 为单位的元素的内部高度,包括内边距但是不包括滚动轴的高度、边以及外边距。offsetHeight: 包括元素的边宽、内边距以及水平滚动轴的高度。scrollHeight: 返回整个元素内容的高度,包括没有被容纳在当前屏幕中的部分。
譬如下面这个
<element
><!-- *content*: child nodes: -->
| content A child node as text node | of
<div id="another_child_node"></div>
| the ... and I am the 4th child node | element
</element>
具体的例子可以参考JSFiddle,这里形象地对
min-_ & max-_
overflow
margin & padding
Percentage Value: 百分比值效果,无论垂直还是水平,百分比值始终参考宽度
当margin
设置成百分数的时候,其top right bottom left
的值是参照父元素盒子的宽度进行计算,在margin
writing-mode: horizontal-tb;
和 direction: ltr;
的情况下,当书写模式变成纵向的时候,其参照将会变成包含块的高度。我们可以以如下的例子进行说明
<div class="demo1">
<div>这个div设置:margin:10% 5%</div>
</div>
.demo1 {
height: 500px;
width: 980px;
margin: 0 auto;
background: red;
overflow: hidden;
}
.demo1 div {
margin: 10% 5%;
background: white;
}
据以往的理解,.demo1 div
的margin
应该是:50px 49px 50px 49px
,但是运行以后,通过查看盒模型示意图,却发现是:98px 49px 98px 49px
。

为什么要选择宽度做参照而不是高度呢?这其实更多的要从
The percentage is calculated with respect to the width of the generated box’s containing block. Note that this is true for ‘margin-top’ and ‘margin-bottom’ as well. If the containing block’s width depends on this element, then the resulting layout is undefined in CSS 2.1.
display:box
.box {
display: -moz-box; /*Firefox*/
display: -webkit-box; /*Safari,Opera,Chrome*/
display: box;
}
容器属性
box-pack: 子元素主轴对齐
.box{
-moz-box-pack: center; /*Firefox*/
-webkit-box-pack: center; /*Safari,Opera,Chrome*/
box-pack: center;
}
.box{
box-pack: start | end | center | justify;
/*主轴对齐:左对齐(默认) | 右对齐 | 居中对齐 | 左右对齐*/
}
box-align: 子元素交叉轴对齐
.box{
-moz-box-align: center; /*Firefox*/
-webkit-box-align: center; /*Safari,Opera,Chrome*/
box-align: center;
}
.box{
box-align: start | end | center | baseline | stretch;
/*交叉轴对齐:顶部对齐(默认) | 底部对齐 | 居中对齐 | 文本基线对齐 | 上下对齐并铺满*/
}
box-direction: 子元素显示方向
.box{
-moz-box-direction: reverse; /*Firefox*/
-webkit-box-direction: reverse; /*Safari,Opera,Chrome*/
box-direction: reverse;
}
.box{
box-direction: normal | reverse | inherit;
/*显示方向:默认方向 | 反方向 | 继承子元素的 box-direction*/
}
box-orient: 子元素行内排列方式
.box{
-moz-box-orient: horizontal; /*Firefox*/
-webkit-box-orient: horizontal; /*Safari,Opera,Chrome*/
box-orient: horizontal;
}
.box{
box-orient: horizontal | vertical | inline-axis | block-axis | inherit;
/*排列方向:水平 | 垂直 | 行内方式排列(默认) | 块方式排列 | 继承父级的box-orient*/
}
box-lines: 子元素换行
.box{
-moz-box-lines: multiple; /*Firefox*/
-webkit-box-lines: multiple; /*Safari,Opera,Chrome*/
box-lines: multiple;
}
.box{
box-lines: single | multiple;
/*允许换行:不允许(默认) | 允许*/
}
子元素属性
box-flex: 是否允许缩放
.item{
-moz-box-flex: 1.0; /*Firefox*/
-webkit-box-flex: 1.0; /*Safari,Opera,Chrome*/
box-flex: 1.0;
}
.item{
box-flex: <value>;
/*伸缩:<一个浮点数,默认为0.0,即表示不可伸缩,大于0的值可伸缩,柔性相对>*/
}
box-ordinal-group: 子元素显示次序
.item{
-moz-box-ordinal-group: 1; /*Firefox*/
-webkit-box-ordinal-group: 1; /*Safari,Opera,Chrome*/
box-ordinal-group: 1;
}
.item{
box-ordinal-group: <integer>;
/*显示次序:<一个整数,默认为1,数值越小越排前>*/
}