What Are the Default Top, Left, Botton or Right Values When Position:Absolute Is Used

What are the default top, left, botton or right values when position:absolute is used?

As you suspect, the initial values for all these properties is not 0; it is auto. You can find their property definitions in section 9.3.2 of the spec.

When an absolutely positioned box keeps all its offsets auto (i.e. you don't modify them), it doesn't go anywhere. It remains in the static position, which basically means its usual spot in the layout had it not been positioned at all. Section 10 has all the details (it even has entire paragraphs explaining what "static position" means), but you'll want to focus on 10.3.7:

The constraint that determines the used values for these elements is:

'left' + 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' + 'right' = width of containing block

If all three of 'left', 'width', and 'right' are 'auto': First set any 'auto' values for 'margin-left' and 'margin-right' to 0. Then, if the 'direction' property of the element establishing the static-position containing block is 'ltr' set 'left' to the static position and apply rule number three below; otherwise, set 'right' to the static position and apply rule number one below.

[...]

1. 'left' and 'width' are 'auto' and 'right' is not 'auto', then the width is shrink-to-fit. Then solve for 'left'

And 10.6.4:

For absolutely positioned elements, the used values of the vertical dimensions must satisfy this constraint:

'top' + 'margin-top' + 'border-top-width' + 'padding-top' + 'height' + 'padding-bottom' + 'border-bottom-width' + 'margin-bottom' + 'bottom' = height of containing block

If all three of 'top', 'height', and 'bottom' are auto, set 'top' to the static position and apply rule number three below.

[...]

3. 'height' and 'bottom' are 'auto' and 'top' is not 'auto', then the height is based on the content per 10.6.7, set 'auto' values for 'margin-top' and 'margin-bottom' to 0, and solve for 'bottom'

Default value for top, bottom, right, left position properties

Take top for example, from https://developer.mozilla.org/en/docs/Web/CSS/top

For absolutely positioned elements (those with position: absolute or position: fixed), it specifies the distance between the top margin edge of the element and the top edge of its containing block.

So, top: auto for an absolutely positioned element means whatever the position of the element currently is, and can be effected by the bottom property.

For relatively positioned elements (those with position: relative), it
specifies the amount the element is moved below its normal position.

So, top: auto for a relative positioned element it means the offset from it's original position, also based on the bottom property. If both are set to auto, then it won't offset anything

This would be the same for bottom, but opposite, and then similarly for the left/right relationship.

From http://vanseodesign.com/css/auto-positioning/

The default value for the top, right, bottom and left properties is
auto, which means the absolutely positioned box will appear exactly
where it would have had if it wasn’t positioned. Since it’s removed
from the flow it will overlap any elements in the normal flow that
follow it, though.

Also of note is this:

When both top and bottom are specified, as long as height is
unspecified, auto or 100%, both top and bottom distances will be
respected. Otherwise, if height is constrained in any way, the top
property takes precedence and the bottom property is ignored.

position: absolute without setting top/left/bottom/right?

The standard generally says if top/bottom, left/right are auto, then default them to their position: static values:

http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-width

http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-height

Position:absolute without top/left/bottom/right values

If you want an element to remain in its static position (where it would normally be if it were not positioned) but simply take it out of normal flow, simply specifying position: absolute is perfectly acceptable. The behavior is as described in sections 10.3.7 and 10.6.4 of the spec, and every browser behaves correctly.

You aren't required to give it any offsets if you don't want to move the element from its usual static position, and you aren't required to relatively position its parent element if you're not going to move the element anywhere since it'll remain in its static position anyway.

I just looked at your code again and noticed that you're applying percentage padding to your absposed element. You will need to relatively position the parent element if you want the percentage padding to be calculated based on the width of this parent element and not the initial containing block (where the root element resides):

#parent{
position:relative;
width:50%;
margin:10% auto;
background:gold;
height:20%;
}

Compare this fiddle with the original.

So, the main purpose of relatively positioning some ancestor of an absolutely-positioned element is for designating its containing block. Sections 9 and 10 have most of the gory details on the interactions between elements and their containing blocks. Depending on your layout you may not need to position anything else at all, however if your layout is complex enough you may find that there are side-effects resulting from whether or not you position any container elements, and which one you do position. I suspect the topic of containing blocks could be covered in a different question since it may very well be out of scope of this one.

What are the left and top values of an element and its child element if both have position:absolute

First, you should note that the initial values for top, left, right and bottom are auto and not 0. See section 9.3.2 of the specs

So to the question:

if I didn't specify the left & top for absolutely positioned
element whose parent is also absolutely positioned & has no margin or
padding, the left and top would be 0px, Is that correct as per css
specs?

The answer is no this isn't correct. In your example, it happens to be true because the child element is positioned there in the flow of the document (even without any positining).

As you can see in this example :

<div style="position:absolute; width:100px; height:100px;">  Some text  <span style="position:absolute;">Test child</span></div>

CSS absolute positioning- do I need to specify top, right, bottom AND left?

here , setting all coordonates to zero tells the element to cover the whole parent (positionned).if label is position:relative or fixed or absolute it will cover it .

it is similar to

top:0;
left:0;
height:100%;
width:100%;

But it can have another purpose for element sized, adding margin:auto; will center element into the parent positioned. if there is no parent positionned, then the viewport come the reference.
example

input,div {  position: absolute;  top: 0;  left: 0;  bottom: 0;  right: 0;  text-align: center;  line-height: 2.2em;  margin: auto;}
div { background: lime;/* show me */}
input { z-index: 1;/* let me be on top */}
<input type="text" value="i have a defaut size" /><div> well i have size that content, coordonates or height/width ,  shall give me </div>

Understand default placement of absolute positioned element

If you set the img to position: absolute, it will get out of the flow, but stay where it is in the normal flow. The document and other elements will behave like the absolutely positioned element does not exist.

Only by applying top, bottom, left or right values, will make it actually go somewhere

From the W3C wiki :

The box is taken out of the normal flow. The box's position (and
possibly size) is specified with the 'top', 'right', 'bottom', and
'left' properties.

W3C wiki CSS:position

Note: This has been replied in this SO question

Why does this CSS margin-top style not work?

You're actually seeing the top margin of the #inner element collapse into the top edge of the #outer element, leaving only the #outer margin intact (albeit not shown in your images). The top edges of both boxes are flush against each other because their margins are equal.

Here are the relevant points from the W3C spec:

8.3.1 Collapsing margins


In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.

Adjoining vertical margins collapse [...]

Two margins are adjoining if and only if:

  • both belong to in-flow block-level boxes that participate in the same block formatting context
  • no line boxes, no clearance, no padding and no border separate them
  • both belong to vertically-adjacent box edges, i.e. form one of the following pairs:

    • top margin of a box and top margin of its first in-flow child

You can do any of the following to prevent the margin from collapsing:

  • Float either of your div elements
  • Make either of your div elements inline blocks
  • Set overflow of #outer to auto (or any value other than visible)

The reason the above options prevent the margin from collapsing is because:

  • Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
  • Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
  • Margins of inline-block boxes do not collapse (not even with their in-flow children).

The left and right margins behave as you expect because:

Horizontal margins never collapse.



Related Topics



Leave a reply



Submit