[英]What does single `position: absolute` do to a <div> tag?
I've a simple, silly question. 我有一个简单而愚蠢的问题。 What does
position: absolute
do to a div
tag? position: absolute
对div
标签有什么作用?
Here's an example. 这是一个例子。 Let's say I've three
<div>
tags: 假设我有三个
<div>
标签:
<div id="a"></div>
<div id="b" style="position: absolute"></div>
<div id="c"></div>
From my experiments position: absolute
somehow "pushes" #b
outside of regular block element flow and it appears between #a
and #c
, with #a
directly following #c
and #b
just floating there below or above them. 从我的实验
position: absolute
来看position: absolute
#b
position: absolute
以某种方式“推动” #b
到规则块元素流之外,并且出现在#a
和#c
之间,其中#a
紧随#c
和#b
浮动在它们下面或上面。
What exactly is happening? 到底是什么情况?
正如您所注意到的, position: absolute
将元素从正常布局流程中完全删除,使您可以随意将其放置在所需的位置。
The position
attribute controls the location based on which ever parent element has a position
defined. position
属性根据哪个父元素定义了position
来控制position
。 Unless a value is set explicitly for an element on the page the first parent with position
is the <body>
element. 除非为页面上的元素显式设置一个值,否则第一个具有
position
父元素是<body>
元素。
Once you set a position
value for an element you can then place elements within it. 一旦为元素设置了
position
值,就可以将元素放置在其中。
As a rule I always set the nearest parent to have position: relative
which is essentially the normal position it would have. 通常,我总是将最近的父级设置为具有
position: relative
,这实际上是它将具有的正常位置。 Then I can position elements within it. 然后,我可以在其中放置元素。
position: absolute
will place the element using co-ordinates based on the top
, bottom
, left
and right
attribute values. position: absolute
将使用基于top
, bottom
, left
和right
属性值的坐标来放置元素。
When you add the position:absolute;
添加
position:absolute;
property to block b
, you basically ripped that div div out of the layout's flow. 属性到
block b
,您基本上将div div从布局的流程中删除了。 The absolute div (block b) ignores it's next neighboring element(s) in the layout and vice versa. 绝对div(方框b)会忽略布局中的下一个相邻元素,反之亦然。
As you can see here: http://jsfiddle.net/AndrewL32/65sf2f66/12/ block C
is positioned right after block A
as though block B
doesn't exist. 如您在此处看到的: http : //jsfiddle.net/AndrewL32/65sf2f66/12/
block C
紧接在block A
,就好像block B
不存在一样。 In the same way, block B
ignore block C
and position itself next to A. 以相同的方式,
block B
忽略block C
并将其自身定位在A旁边。
An element with position: absolute;
位置为:绝对的元素; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
相对于最近定位的祖先定位(而不是相对于视口定位,如固定)。
However;
然而; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.
如果绝对定位的元素没有祖先,它将使用文档主体,并随页面滚动一起移动。
With CSS positioning, you can place an element exactly where you want it on your page. 通过CSS定位,您可以将元素准确地放置在页面上所需的位置。
When you are going to use CSS positioning, the first thing you need to do is use the CSS property position to tell the browser if you're going to use absolute or relative positioning. 当您要使用CSS定位时,您需要做的第一件事就是使用CSS属性位置来告诉浏览器您要使用绝对定位还是相对定位。
Both Postion are having different features.In Css Once you set Position then you can able to use top,right,bottom,left attributes. 这两个Postion具有不同的功能。在CSS中,一旦设置了Position,就可以使用top,right,bottom,left属性。
Absolute Position 绝对位置
An absolute position element is positioned relative to the first parent element that has a position other than static. 绝对位置元素相对于具有非静态位置的第一个父元素放置。
Relative Position 相对位置
A relative positioned element is positioned relative to its normal position. 相对定位的元素相对于其正常位置进行定位。
To position an element relatively, the property position is set as relative. 为了相对定位元素,将属性位置设置为相对。 The difference between absolute and relative positioning is how the position is being calculated.
绝对位置和相对位置之间的差异是位置的计算方式。
According to Positioning schemes , 根据定位方案 ,
In CSS 2.1, a box may be laid out according to three positioning schemes:
在CSS 2.1中,可以根据三种定位方案来布置盒子:
- Normal flow .
正常流量 。
- Floats .
浮游物 。
- Absolute positioning .
绝对定位 。
Therefore, adding psotion: absolute
to your element will make it positioned according to the absolute scheme, instead of the normal flow one. 因此,在元素上添加
psotion: absolute
将使它根据绝对方案而不是正常流程一个进行定位。
According to The position
property , position: absolute
means this: 根据
position
属性 , position: absolute
表示:
The box's position (and possibly size) is specified with the
top
,right
,bottom
, andleft
properties.框的位置(可能还有尺寸)由
top
,right
,bottom
和left
属性指定。 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.
同样,尽管绝对定位的框具有边距,但它们不会与其他任何边距一起折叠 。
Also, according to Absolute positioning , 另外,根据绝对定位 ,
In the absolute positioning model, a box is explicitly offset with respect to its containing block.
在绝对定位模型中,框相对于其包含的块显式偏移。 It is removed from the normal flow entirely (it has no impact on later siblings).
它已从正常流中完全删除(它对以后的同级没有影响)。
An absolutely positioned box establishes a new containing block for normal flow children and absolutely (but not fixed) positioned descendants.
绝对定位的框为正常流子和绝对(但不固定)定位的后代建立了一个新的容纳块。
However, the contents of an absolutely positioned element do not flow around any other boxes.
但是,绝对定位的元素的内容不会在其他任何框周围流动。
They may obscure the contents of another box (or be obscured themselves), depending on the stack levels of the overlapping boxes.
它们可能会使另一个盒子的内容模糊(或自身被遮盖),具体取决于重叠盒子的堆叠高度 。
Absolutely positioning is related to display
and float
, as explained in Relationships between 'display', 'position', and 'float' . 绝对定位与
display
和float
相关,如“显示”,“位置”和“浮动”之间的关系所述 。 Basically, 基本上,
inline-table
becomes table
; inline-table
的显示变为table
; and inline
, table-row-group
, table-column
, table-column-group
, table-header-group
, table-footer-group
, table-row
, table-cell
, table-caption
, inline-block
become block
inline
, table-row-group
, table-column
, table-column-group
, table-header-group
, table-footer-group
, table-row
, table-cell
, table-caption
, inline-block
变成block
According to Layered presentation , absolutely positioned elements are painted in another layer: 根据分层表示 ,绝对定位的元素将绘制在另一层中:
Within each stacking context, the following layers are painted in back-to-front order:
在每个堆叠上下文中,以下各层按从前到后的顺序绘制:
- the background and borders of the element forming the stacking context.
构成堆叠上下文的元素的背景和边界。
- the child stacking contexts with negative stack levels (most negative first).
堆栈级别为负的子堆栈上下文(首先为负)。
- the in-flow, non-inline-level, non-positioned descendants.
流入,非内联级别,未定位的后代。
- the non-positioned floats.
未定位的浮点数。
- the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
流内,行内,非定位后代,包括内联表和内联块。
- the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
堆栈级别为0的子堆栈上下文和堆栈级别为0的已定位后代。
- the child stacking contexts with positive stack levels (least positive first).
子堆栈上下文的堆栈级别为正(最低优先级为第一)。
The position (and the size, if width
or height
are auto
) of an absolutely positioned element is determined by its containing block and its top
, right
, bottom
and left
properties. 绝对定位的元素的位置(以及大小,如果
width
或height
为auto
)由其包含的块及其top
, right
, bottom
和left
属性确定。 If those are not specified, their value will be auto
. 如果未指定,则其值为
auto
。 The algorithms are explained in detail here and here . 此处和此处将详细说明算法。 Basically:
基本上:
position: static
and float: none
. position: static
和float: none
。 auto
width is calculated by shrink-to-fit: min(max(preferred minimum width, available width), preferred width). auto
宽度是通过收缩拟合来计算的:min(max(max(首选最小宽度,可用宽度),referred宽度)。 auto
height is calculated as usual for block formatting context roots auto
高度,用于块格式化上下文根
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.