CSS set absolute position - css

When I set position: absolute and top: 0px the element does not actually positions absolutely, but it positions relatively to its parent (with position: relative).
Is there a simple way to set element position relative to the whole document not to the parent?
UPDATE
Can I do this if my element is already attached to a div with position:relative?
It's a problem to reattach element to the body in my case.

If you want your div to be positioned absolute to parent than simply place it between the <body> tag (Here I assume that you are using position: relative for body) and not inside any positioned relative child element else your positioned absolute div will be positioned absolute to that element and not the body(Which you are referring as parent here)

An element with a position: absolute will position itself absolutely to the first parent element with another position style.
You need to bring the element outside any elements with position styles. You may as well put it at the end of the body HTML if it's going to be positioned absolutely anyway.

Related

How can I set 'absolute' or 'fixed' with width: 100% to its parent div not the window size?

I am trying to have a banner like below
using 'absolute' or 'fixed'.
However, it becomes like this when I tried with 'left: 0 right: 0 top: 0'
or with width '100%, then it pushes the right. How can I set 'A' like in the first picture? I don't want to use relative or static since it pushes the content downward.
You can't do it with fixed, but you can set the parent div (the black box in your example) to have position: relative; and that will make the absolute positioned child position itself according to the parent div.
Why is this?
Absolute - the element is positioned absolutely to its first positioned parent. -- DZone
What this means is that the absolute positioned element will position itself according to the nearest ancestor with an assigned position, besides static.
I hope this is clear enough, but let me know if it's not.

Why the absolutely placed element moves up when we scroll down the page and fixed positioned element does not?

When we give an element a position property of absolute with a vertical and horizontal offset (ex bottom, left) it is positioned relative to the closest parent element that has a position property of relative.
If no parent element has a relative property than the absolutely positioned element is positioned with respect to the browser window.
The definition of fixed positioning is quite the same it says, "Fixed positioning is a type of absolute positioning in which the element is positioned with respect to the browser window."
The main question I have is it that why the absolutely positioned element moves up when the page is scrolled down and the fixed positioned element does not when both of them is placed with respect to the browser window.
Absolutely positioned elements are never positioned relative to the browser window (i.e. the viewport) except by coincidence. Nor are they, in the absence of a positioned ancestor, as w3schools would have it, positioned relative to the body element. Nor are they positioned relative to the html (i.e. the root) element.
html {
margin:20px;
border :1px red solid;
height:100px;
}
body {
margin:30px;
border :1px blue solid;
height:90px;
}
div {
border :1px green solid;
height:155px;
position:absolute;
top:0;
left:0;
}
<div>text</div>
If an absolutely positioned element has no positioned ancestor, it is positioned relative to the initial containing block, which has its top, left corner at (0, 0) on the canvas. As the scrollbars move the viewport over the canvas, so absolutely positioned elements are moved relative to the viewport by scrolling.
Here is a short explanation of how these positions properties work
Fixed Position
A fixed position element is positioned relative to the viewport, or the browser window itself. The viewport doesn't change when the window is scrolled, so a fixed positioned element will stay right where it is when the page is scrolled
Absolute Position
This property helps to position element with respect to a parent element that has a position of either relative or absolute and if there is no such element it'll position according to the browser window
Absolute Position:
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.
ref: https://www.w3schools.com/css/css_positioning.asp
So Basically Absolute Positioning of an Element, when no nearest parent has position defined, Absolute Positioned element is placed with respect to Element , but not with respect to browser window. that makes it scroll , on scrolling the webpage body or browser window.
Like you said, an element with position absolute "is positioned and sized solely in reference to its absolute positioning containing block".
A good follow up question is: what if there is no relative/absolute/fixed positioned parent (including document or body)? The element "is included in the scrollable overflow area of the box that generates is containing block." Usually this is your window object, but it could be any scrollable div you have. Absolutely positioned elements inside of a scrollable element will scroll up with it, even if your scrollable element is statically positioned.
Fixed elements are "positioned relative to ... the viewport" and do "not move when the document is scrolled."
https://www.w3.org/TR/css-position-3/#absolute-position

Is it possible for a parent element to Inherit height from an absolutely positioned child element?

Is it possible for a parent element to inherit the height from a child element that is positioned absolutely?
Currently, I have <div id="parent"> with position: relative;. Inside this "parent" is another element <div id="child"> with position: absolute;.
The issue I'm facing is that the "child" element is not forcing the "parent" element to inherit it's height, in turn causing page layout problems.
No, it's not possible.
Absolute positioned elements aren't in the normal flow of the document, so they don't increase parent's height.
From MDN:
an element that is positioned absolutely is taken out of the flow and
thus takes up no space when placing other elements.
From W3C:
Absolutely positioned boxes are taken out of the normal flow. This
means they have no impact on the layout of later siblings.

absolute positioning relative to body of a div generated dynamically

can i place a div at absolute position relative to body which is dynamically generated inside a a div which position is relative
here is my scenario i have to keep it this way only
http://jsfiddle.net/bipin000/swqqH/
okay its not possible until you make parent div position:static and then make dynamic div position:absolute
http://jsfiddle.net/swqqH/25/
No, that's not possible. As the parent div is positioned, it becomes the offset parent, so the absolutely positioned div will be placed relative to the outer div, not relative to the body.

Position overlay div

I have a link in a table cell and when the link is clicked I show a hidden div.
Currently I use position: absolute and z-index:10. It works fine, but I would like to move it a bit to the top and left. When I add top: -10px and left: -10px, the div moves to the position of the window.
How do I make it 10px off the table cell?
You need to set the parent element using position relative then use position absolute on the element you want to position. So if you want it to be positioned based on the table you need to add position: relative to the table (which won't do anything because it is already positioned relative) and position: absolute to the overlay. Absolute positioning takes the element out of the document flow and relative positioning leaves it in the document flow which is why stuff is being moved around. The reason for this is based off how CSS works: http://www.w3schools.com/css/pr_class_position.asp
relative The element is positioned relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position
absolute The element is positioned relative to its first positioned (not static) ancestor element
You might also be interested in fixed.
fixed The element is positioned relative to the browser window
Here is an Example: http://pastehtml.com/view/av391nzsv.html
position: relative;
instead of
position: absolute;
Relative says to measure top and left from the parent element, absolute says to measure from the top left corner of the page.
use margin-top:-10px; margin-left:-10px;

Resources