Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can tell absolute position ignore is parent relative position?

Tags:

css

This is the code: http://jsfiddle.net/noamway/r8vMp/8/

    <div id="website" style="width:100%;">
    <div id="layoutAll" style="width:200px;margin:0 auto">
        <div id="layout" style="width:100%;position:relative;">
            <div id="full_strip" style="width:100%;background-color:black;color:white;height:100px;position:absolute;left:0;">
            Hello world
            </div>
        </div>
    </div>
</div>

And I like that "full_strip" will be 100% from all the page width. Because of the relative of is parent I can't do that. I can't remove any setting from the is parents so I need a commend or something else on him that will tell him to ignore is relative parent.

Thanks, Noam

like image 281
Noamway Avatar asked Sep 02 '13 03:09

Noamway


People also ask

Is position absolute relative to parent?

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. It is positioned automatically to the starting point (top-left corner) of its parent element.

What is the difference between absolute and relative position?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.

What determines the position of an element that has position absolute set?

The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static ).

When you place position absolute on something what is it positioned relative to?

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.


2 Answers

Remove width:200px for div with id #layout. Important thing is dont use same id for two elements. Duplicate id's are dangerous.

CODE:

<div id="website" style="width:100%;">
    <div id="layout" style="margin:0 auto">
        <div id="layout" style="width:100%;position:relative;">
            <div id="full_strip" style="width:100%;background-color:black;color:white;height:100px;position:absolute;left:0;">
            Hello world
            </div>
        </div>
    </div>
</div>

DEMO FIDDLE

like image 174
Kiran Avatar answered Sep 17 '22 01:09

Kiran


You basically ask for relating an absolute position Div which is dynamically generated inside a relative position Div to the body instead of to its relative parent Div.

Position relative and absolute are always related to the first root parent element that has a absolute or relative position. This why it is impossible to do what you ask for.

The only solution for you its to place the “full_strip” Div outside of its position relative parent element and into body tag.

like image 20
Roy Shoa Avatar answered Sep 17 '22 01:09

Roy Shoa