Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Centering absolutely-positioned elements?

Centering absolutely-positioned elements?
Thread Tools
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jul 10, 2002, 12:03 PM
 
I have a page header that I'm trying to position absolutely. It's at the bottom of my HTML code, but I want to use my stylesheet to position it at the top.

That part isn't hard. But I also need it centered horizontally. The normal trick of setting margin-left and margin-right to auto doesn't seem to work for absolutely-positioned elements. Anyone else know how to do this? I think I've figured out a way to do it with JavaScript, but I'd rather not resort to that if I don't have to.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
Professional Poster
Join Date: Jan 2000
Location: Detroit
Status: Offline
Reply With Quote
Jul 10, 2002, 02:15 PM
 
hmmm

i place a header (and footer) inside a centered table. that makes it always centered to whatever size your browser window is...
     
Fresh-Faced Recruit
Join Date: May 2001
Location: Fort Worth, TX, USA
Status: Offline
Reply With Quote
Jul 10, 2002, 04:43 PM
 
I think you can do

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> margin-left: auto; margin-right: auto; </pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">to get what you want.

See <a href="http://www.allmyfaqs.com/cgi-bin/wiki.pl?Center_with_CSS" target="_blank">here</a> for more.
     
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jul 10, 2002, 04:56 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by glhaynes:
<strong>I think you can do

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> margin-left: auto; margin-right: auto; </pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">to get what you want.

See <a href="http://www.allmyfaqs.com/cgi-bin/wiki.pl?Center_with_CSS" target="_blank">here</a> for more.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Unfortunately, that trick doesn't seem to work for absolutely-positioned elements. I assume it probably doesn't work with fixed-positioned elements either. It does work for static and relative positioning, though.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
ism
Grizzled Veteran
Join Date: Sep 2001
Status: Offline
Reply With Quote
Jul 10, 2002, 07:04 PM
 
This technique should work with absolutely positioned elements (negative margin technique).

It requires you to know the width of the header (say 400px), position the element left by this amount and then set it back right half of it's width.

Style using:

div#header {
position: absolute;
top: 0px;
width: 400px;
margin-left: -200px;
left: 400px;
}

Can't think of a better way just yet. Thinking perhaps floats to get around absolute positioning, but then you've got the same problem with centering. Hmmm, ain't css fun?
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 10, 2002, 09:47 PM
 
This is not the cleanest way, but what about this:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">&lt;div style=&quot;position: absolute; left:0px; top:0px; width: 100%&quot;&gt;
&lt;div style=&quot;text-align: center&quot;&gt;
Your header goes here
&lt;/div&gt;
&lt;/div&gt;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">The only problem I can see immediately is with IE5.1 for Mac - it resizes a window fine outwards, but make the window more narrow and it doesn't adjust the width of the DIV. Mozilla seems to work well. Haven't tested any others.

Do you need to put this at the end of your code? It would degrade better if it were at the top.

<small>[ 07-10-2002, 10:59 PM: Message edited by: Simon Mundy ]</small>
Computer thez nohhh...
     
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status: Offline
Reply With Quote
Jul 12, 2002, 05:37 PM
 
Don't have time to actually try this, but it seems straight forward.
Let's say your banner image is W 400px by H 60px.
Set your div tag style to:
top: 0px
left: 0px
width: 100%
height: 60px
align: center
* put your banner image tag here *
close div tag.

Give it whirl. My curiosity is piqued. I'll check back later.
C.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:35 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2