HighDots Forums  

Center-align absolute positioned elements

Cascading Style Sheets Layout/presentation on the WWW (comp.infosystems.www.authoring.stylesheets)


Discuss Center-align absolute positioned elements in the Cascading Style Sheets forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Philipp Lenssen
 
Posts: n/a

Default Center-align absolute positioned elements - 11-02-2004 , 11:41 AM






Is it possible in IE6+NS7 to center-align a div of fixed width which
itself contains elements which use "position: absolute"?
Using "margin-left: auto" and "margin-right: auto" works well to center
something, but the absolute-positioned contents of the outside layer
will be stuck to the left of the window...!

Thanks a lot!
--
Google Blogoscoped
http://blog.outer-court.com

Reply With Quote
  #2  
Old   
Neal
 
Posts: n/a

Default Re: Center-align absolute positioned elements - 11-02-2004 , 12:10 PM






On 2 Nov 2004 17:41:13 GMT, Philipp Lenssen <info (AT) outer-court (DOT) com> wrote:

Quote:
Is it possible in IE6+NS7
.... or in CSS in general...

Quote:
to center-align a div of fixed width which
itself contains elements which use "position: absolute"?
margin: auto; will center the block.

Quote:
Using "margin-left: auto" and "margin-right: auto" works well to center
something, but the absolute-positioned contents of the outside layer
will be stuck to the left of the window...!
Yes, the margins are taken into account.

A workaround is to nest divs. The CSS for the following should be obvious.

<div class="relativemarginauto">
<div class="wraprelativenomargin">
<p>Content will be positioned to the inner div, which has no margin.</p>
</div>
</div>




Reply With Quote
  #3  
Old   
Philipp Lenssen
 
Posts: n/a

Default Re: Center-align absolute positioned elements - 11-03-2004 , 04:48 AM



Neal wrote:

Quote:
On 2 Nov 2004 17:41:13 GMT, Philipp Lenssen <info (AT) outer-court (DOT) com
wrote:


margin: auto; will center the block.

Using "margin-left: auto" and "margin-right: auto" works well to
center something, but the absolute-positioned contents of the
outside layer will be stuck to the left of the window...!

Yes, the margins are taken into account.

Thanks, but I tried, and it didn't work -- can you help?
Here is a sample. As the outer div is not absolutely positioned, it
seems both Firefox and IExplorer have the same behavior of ignoring the
centering for the absolute positioned contents.
http://outer-court.com/temp/sample/


Reply With Quote
  #4  
Old   
Phil Evans
 
Posts: n/a

Default Re: Center-align absolute positioned elements - 11-03-2004 , 05:19 AM



Philipp Lenssen wrote:
Quote:
Neal wrote:

On 2 Nov 2004 17:41:13 GMT, Philipp Lenssen <info (AT) outer-court (DOT) com
wrote:
margin: auto; will center the block.

Using "margin-left: auto" and "margin-right: auto" works well to
center something, but the absolute-positioned contents of the
outside layer will be stuck to the left of the window...!

Yes, the margins are taken into account.

Thanks, but I tried, and it didn't work -- can you help?
Here is a sample. As the outer div is not absolutely positioned, it
seems both Firefox and IExplorer have the same behavior of ignoring the
centering for the absolute positioned contents.
http://outer-court.com/temp/sample/
Absolutely positioned elements are positioned relative to the next
*positioned* element up in the tree - since your containing block
doesn't have any position, this defaults to the body. Applying position:
relative to #content will give it position, without moving it.

There are still problems with the fact that you've given the #navi and
#maintext elements heights greater than #content, but this should put
you on the right track.

P


Reply With Quote
  #5  
Old   
Philipp Lenssen
 
Posts: n/a

Default Re: Center-align absolute positioned elements - 11-03-2004 , 07:55 AM



Phil Evans wrote:

Quote:
Philipp Lenssen wrote:


Thanks, but I tried, and it didn't work -- can you help?
Here is a sample. As the outer div is not absolutely positioned, it
seems both Firefox and IExplorer have the same behavior of ignoring
the centering for the absolute positioned contents.
http://outer-court.com/temp/sample/

Absolutely positioned elements are positioned relative to the next
positioned element up in the tree - since your containing block
doesn't have any position, this defaults to the body. Applying
position: relative to #content will give it position, without moving
it.

This works great and solved my problem. Thanks a lot!

--
Google Blogoscoped
http://blog.outer-court.com


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.