Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css absolute position an inner div to the top of the page ignoring parents relative position

Tags:

css

is there a way to absolute position an inner div to the top of the page ignoring parents relative position?

like image 436
Gidon Avatar asked Feb 24 '11 11:02

Gidon


People also ask

Is absolute position relative to parent?

Absolute In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow.

How do you fix absolute position inside a div?

Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.

Can a div be position absolute and relative?

If you position it as absolute , then you're positioning it relative to its closest ancestor which is fixed or relative ... Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.


2 Answers

Nope, unless you re-locate it in the DOM.

(using position:fixed might be an alternative if you want it to be window related instead of document related)

like image 102
Gabriele Petrioli Avatar answered Oct 19 '22 03:10

Gabriele Petrioli


You can use position: absolute; and negative values:

HTML:

<div id="parent">
      <div id="child">
           The child's content
      </div>
</div>

CSS:

#parent
{
    position: relative;
    top: 200px;
    border: 1px dashed #f00;
    height: 50px;
    width: 200px;
}

#child
{
    position: absolute;
    top: -200px;
}

This should do it. Example for you here.

like image 26
Kyle Avatar answered Oct 19 '22 02:10

Kyle