HTML does absolute position create new positioning system for its children? - html

as far as i know, absolute positioned element (.e.g.span style="position:absolute") will position based on its ancestor's position context, but this absolute positioned element WILL NOT create a new positioning system for its children, is that right?????
if #1 is correct, can someone tell me why parent is allowed to set to absolute position in this document? (just search exactly this line in quote will bring you to the relevant paragraph "Specify the parent container as position:relative or position:absolute."
http://phrogz.net/css/vertical-align/index.html
parent in that link must be 'relative' right, but not absolute, why does it say absolute is ok? because only relative creates new positioning context, so in that link bullet #3, the top 50% should not work

(1) is not correct. Elements with positioning defined will be positioned relative to the nearest ascestor that has its position set to anything other than static (the defautl).
from: http://www.w3.org/TR/CSS2/visuren.html#absolute-positioning:
In the absolute positioning model, a box is explicitly offset with respect to its containing block. .... An absolutely positioned box establishes a new containing block for normal flow children and absolutely (but not fixed) positioned descendants
Note also: relatively and fixed positioned elements also establish new containing blocks for absolutely positioned elements.

Related

why is relative positioned element starting from top corner of window?

I am quite confused with this relative and absolute positioning when the parent or grand parent has a position values. can someone clear me out these two cases:
Case 1:
grandparent - absolute
parent - relative
child - absolute
Case 2:
grandparent - absolute
parent - relative
child - absolute
but here parent is independent of grandparent, like below:
<div class="grand-parent">
</div>
<div class="parent">
<div class="child">
</div>
</div>
Below is a fiddle which i have worked on:
https://jsfiddle.net/4KUWc/32/
Questions:
I don't see any difference between the two cases above. Should the
case 2, parent div not start after grand parent? Why is it taking
(0,0)
If relative has a parent of absolute, will it behave accordingly to
the absolute parent or is not dependent on it.
Can we have a relative children with absolute as parent?
position:absolute
...elements have their relational positioning attributes (i.e. top, bottom, right,...) calculated from their closest ancestor with a position value other than static (which is default). If no such ancestor exists, <body> will be used.
position:fixed
...elements have their relational positioning attributes calculated from their closest viewport (by default: <body>, but a 3d transformed element acts as viewport for its children - see Unfixing fixed elements with transforms).
Both of the above place the element, including all its children, out of the content flow of their parent. All subsequent elements in DOM will act like this element and its children do not exist in DOM.
position:relative
...elements have their relational positioning attributes calculated from the position they would normally occupy in their parents content flow if no relational positioning attribute was applied. Unlike fixed and absolute, relative position does not place the element out of its parent contents flow.
Simply, there is no magic happening. Just understand how each position work. Here's shortest summary I can think of:
All elements by default have position: static , which means they will stack on each other (or inside each other if they're nested).
If an element has display: none or has width: 0px and height: 0px then it will not take space, so other elements will just be rendered as the hidden element never existed.
The CSS attributes top + bottom + right + left + z-index are ignored if the element position is static (the default). To work correctly, the element must be positioned relative, absolute, or fixed.
static and relative are alike, except that relative may consider the attributes top + bottom + right + left + z-index.
relative is positioned relative to its parent position.
Element with position absolute or fixed are alike, with one difference: absolute will be positioned relatively to the body, while fixed will be positioned relatively the window (note #AndreiGheorghiu's answer about fixed position and 3D transform).
absolute element will be relative to the nearest parent with position other than static. If all its parents are static, it will be relative to the body.

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?
Here's an example. Let's say I've three <div> tags:
<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.
What exactly is happening?
As you've noticed, position: absolute completely removes the element from normal layout flow, leaving you free to position it wherever you want.
The position attribute controls the location based on which ever parent element has a position defined. Unless a value is set explicitly for an element on the page the first parent with position is the <body> element.
Once you set a position value for an element you can then place elements within it.
As a rule I always set the nearest parent to have position: relative which is essentially the normal position it would have. 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.
When you add the position:absolute; property to block b, you basically ripped that div div out of the layout's flow. The absolute div (block b) ignores it's next neighboring element(s) in the layout and vice versa.
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. In the same way, block B ignore block C and position itself next to 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.
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.
Both Postion are having different features.In Css Once you set Position then you can able to use top,right,bottom,left attributes.
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:
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.
According to The position property, position: absolute means this:
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.
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'. Basically,
An absolutely positioned box is not floated
An absolutely positioned box is blockified as follows: a display of inline-table becomes 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
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.
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. If those are not specified, their value will be auto. The algorithms are explained in detail here and here. Basically:
The element will be placed at the same position it would have if it had position: static and float: none.
An auto width is calculated by shrink-to-fit: min(max(preferred minimum width, available width), preferred width).
An auto height is calculated as usual for block formatting context roots

Static versus relative positioning in CSS

Relative positioning is the same as the default static positioning, except you can use offset values (top, left, etc.).
Relative seems objectively more powerful that static positioning.
Is there any disadvantage to resetting in CSS all elements to be relative (and overriding for absolute as required)?
Relative positioning establishes a new context for absolutely positioned descendants (which are positioned with the respect to the edges of their closest ancestor that is not statically positioned).
Absolute positioned elements are relative to the closest relative container. If you set everything to relative then it will be a PITA to use absolute positioning in cases where you want your absolute element to be positioned relative to an ancestor further up the tree, because the parent of the absolute element will always be relative.

How do I create a fixed position div inside a containing div?

JSFiddle
How can I get the 'Tree' to be positioned at the top inside the second row?
I have read some sources on this, some claiming that fixed is positioned relative to its outer container, other relative to the browser window. On position:absolute, I've read that it's relative to the browser window, but w3schools claim that it's relative to the first positioned element. All of this is making my head a little fuzzy, and despite playing around with both properties I have not been able to really get my head around this.
position:fixed always refer to the browser window. A fixed positioned element won't change it's position when scrolling the browser window.
If you want a relatively positioned element inside another element, you must give the container (parent) element a position:relative;, and the element inside it a position:absolute;. This way the absolutely positioned element coordinates will be based on the parent (relatively positioned) element.
Also, when trying to use absolute and relative positioning for a layout, it's better not to use tables.
Update
The absolute positioned element will search through the DOM tree, looking for the nearest ancestor that is positioned as well. If it finds none, it will position itself relative to the browser window. – Justus Romijn
Update 2
just give position:relative; to the td containing the absolute positioned element, like this
http://jsfiddle.net/E2gYN/5/

What is the difference between relative and absolute tags of div element?

What is the difference between relative and absolute tags of div element ?
Absolute positioning positions an element relitive to the first parent that has a position oher than static, and removes the element from the document flow. Relative positioning allows you to move an element from its "normal" positon, leaving the space where it was originally in the content.
Relative is generally for small adjusents in page layout, absolute is for exact positioning.
If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. The div stays within the flow of the document, but just moves relative to the position that it is specified at within the document.
When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go. It's not relative to the place you where the div actually occurs in the document.