Forum Moderators: not2easy

Message Too Old, No Replies

Relative position when using margin:0 auto for container

Is there a trick?

         

Demaestro

3:31 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a container div set up like this:

#container {
margin:0 auto;
width:790px;
}

I want to position some text like this:

#positioned_text {
position:absolute;
left: 411px;
top: 119px;
}

But of course when the container div is auto centered then my left value is off and not positioned where I want.

Is there a trick to set the left using JS? Something like?

#positioned_text {
position:absolute;
left: (411 + (the_auto_margin_value)) px;
top: 119px;
}

[edited by: Demaestro at 3:34 pm (utc) on Mar. 23, 2009]

simonuk

4:14 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



One thing worth noting is the parent of an absolute element must also have it's position defined. You would need a position like relative on the #container for the child to work correctly.

Demaestro

4:39 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



simon

You were right.. adding position:relative to the container object did the trick.

The below now works and the positioned_text left and top values are relative to the containers position.

#container {
position:relative;
margin:0 auto;
width:790px;
}

#positioned_text {
position:absolute;
left: 411px;
top: 119px;
}

simonuk

11:46 am on Mar 24, 2009 (gmt 0)

10+ Year Member



Glad it worked :-)