Open ivanberry opened 8 years ago
.bar
向上移动20px,但是.baz
依旧还是当作它在原来的地方,这很好的证明了relative
的元素是_normal flow_的一员。
绝对定位是最后一种定位方案,它们很容易理解,绝对定位元素对布局其他元素而言,基本是不影响的,从根本上是做到了“人畜无害”。甚至她们中的浮动元素也不会影响到父元素外的其他元素。绝对定位有两类,区别在”绝对“的参考不同:
absolute
: 参考与最近的非static
父元素fixed
: 参考与viewport,打死不改spe中是这么描述的:
absolute: The box's position (and possibly size) is specified with the 'top', 'right', 'bottom', and 'left' properties. These properties specify offsets with respect to the box's containing block. Absolutely positioned boxes are taken out of the normal flow. This means they have no impact on the layout of later siblings. Also, though absolutely positioned boxes have margins, they do not collapse with any other margins.
fixed: The box's position is calculated according to the 'absolute' model, but in addition, the box is fixed with respect to some reference.
1, 定位
CSS布局就是将一组HTML元素映射成放置在x,y轴上的一系列矩形盒子.而x,y坐标位置是由_定位方式_确定的(positioning scheme).CSS2.1中定义三类不同得定位方式:normal flow, float 还有absolute position.元素位置得确定最先由定位方式确定,一旦定位方式确定,元素位置可由特殊的布局方法修改如:
display: table
等等.CSS3中更是引入了flexbox
和grid
.定位方式(CSS2.1)
文档所有元素都属于normal flow,一旦定义了
float
或者position
属性,就会从normal flow中移除.normal flow由包含有两方面内容:
包含元素中的子元素的相对位置是由同级子元素格式化环境决定的,在normal flow中可能是_block_或_inline_格式化环境.
Block-level boxes are boxes that participate in a block formatting context. Each block-level element generates a principal block-level box that contains descendant boxes and generated content and is also the box involved in any positioning scheme. Some block-level elements may generate additional boxes in addition to the principal box [for example,]: 'list-item' elements. These additional boxes are placed with respect to the principal box.
几乎所有块级元素都是块级包含容器,它就是一系列其他拥有特定环境得其它盒子.
行级元素定义:
An inline box is one that is both inline-level and whose contents participate in its containing inline formatting context. A non-replaced element with a 'display' value of 'inline' generates an inline box. Inline-level boxes that are not inline boxes (such as replaced inline-level elements, inline-block elements, and inline-table elements) are called atomic inline-level boxes because they participate in their inline formatting context as a single opaque box.
简单粗暴地说: 你可以认为文档流中的竖直叠加(块级格式化环境)和水平叠加(行级格式化环境).
由上述可知,文档流中的元素要么是处于行级格式化环境,要么就是块级格式化换进,没有其它.但是当你混合使用divs和a等时,那是怎么实现的呢?这就需要一个将行级元素变成块级元素的机理:匿名盒子.
匿名盒子
两种情况会产生匿名盒子:
匿名块级盒子:
块级匿名盒子还会产生一些少见得情况中:
匿名行级盒子:
当块级包含元素包含有未包含在行级元素内的文字时会产生匿名行级元素:
匿名盒子的存在非常重要,它决定文档流中毗邻得块级元素和行级元素之间得布局,当块级元素和行级元素毗邻存在时,行级元素会假装自己包裹在一个匿名块级元素当中.
文档流中得布局由上述得两种格式化环境确定,在表现上,它们可以简单地认为是竖直叠加和水平叠加
竖直叠加
In a block formatting context, each box's left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). This is true even in the presence of floats (although a box's line boxes may shrink due to the floats), unless the box establishes a new block formatting context (in which case the box itself may become narrower due to the floats).
简单总结就是: 块级格式化环境中,盒子的布局在竖直方向一个接着一个排布,竖直距离由
margin
决定,当然相邻块级元素间的竖直外边距会合并(不是相加,而是表现为大的外边距).块级元素中重要的两点:上述例子中:
float
也不例外块级格式化环境总体而言是很简单的,但是行级格式化环境就相对而言复杂了.
normal flow: 行级块级格式化环境
简单来说:行级格式化环境中,盒子水平叠加在一行或多行,没一行就是一个行盒子,它们的宽度由包含的元素决定,高度由
line-height
决定.行级格式化环境中的对齐可能才是难点所在:
行级格式化环境中得水平对齐
首先需要明确,只有当行盒子中还有未利用空间时,属性定义才会有意义.标准中提到:
行级格式化环境的竖直对齐
vertical-height
决定行盒中行内盒子的对齐.为了了解竖直对齐,首先要知道行内盒子和行盒子高度是怎么来的.行盒高度:
简单说来: 行内元素得高度由字体高度*
line-height
值决定.还有好多细节,我们先不深究,待议
文档流:相对位置
position: relative
被认为normal flow的一部分.也就是说: 相对定位的元素和未定义
position
属性得元素一样,只不过能通过top
,left
,right
,bottom
来偏移.