Forum Moderators: not2easy

Message Too Old, No Replies

Centering a layer "set"

How to center a set of layers as a single unit?

         

pliny

7:09 am on Jan 11, 2005 (gmt 0)

10+ Year Member



Here is a comp of what I am working on:

[offhollywooddigital.net...]

This contains about a half-dozen layers, including header, main content area, and nav bar. I need to center the whole thing vertically and horizontally, without changing the distances between individual layers (e.g.: main content area is 68px below header; nav bar starts 450px below header).

If I place the layers with absolute positioning, the absolute point of reference for each layer is simply the top left of the porthole. If I place them with relative positioning, they the layers display in normal flow, sequentially.

One possible solution with Javascript, computing x and y offsets based on browser window size, and then dynamically adding the offsets to each layer's "top" and "left" attributes. That works, kinda sorta, but it's messy--querying style attributes is not as simple as it sounds. Plus I have to support Mac browsers, which is always good for a Javascript headache.

Basically, I need to group together a set of layers and move that set, wholesale, to a horiz-vert-centered position while not shifting them in relation to each other. And do it cleanly, if possible.

Any suggestions?

ahmedtheking

10:32 am on Jan 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



change your DTD to XHTML 1.1, go to www.firestartermedia.com (my site) and steal the DTD from there!

Change the main layer (that contains all the layers) to...

position: relative;
margin: auto; /*this will center it, but may not make it go in the middle (cos some browsers r stupid, like IE!) */
top:10%; /*or whatever you want it to be as a kind of safe-proof if the margin doesnt work! */

killbornj

12:00 pm on Jan 12, 2005 (gmt 0)



I was having these problems too.

if your using Div tags this is how i solved it.

<style type="text/css">
#centerLayer {
margin: 0 auto; /* this will work for most browsers however it WILL NOT WORK FOR IE 6.0 */ }
body { text-align: center; /* as messed up as it looks, this will fix your centering problem in IE 6.0 */ }
#contentLayer { text-align: left; /* adjust text back to normal alignment */ }
</style>

<body>
<div class="centerLayer">
<div class="contentLayer">....</div>
</div>
</body>

If I haven't explained it good enough for you, go to
[quirksmode.org ] for more information about this.

ahmedtheking

3:03 pm on Jan 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



make sure the layer is relative and it will work in IE!

pliny

7:26 pm on Jan 12, 2005 (gmt 0)

10+ Year Member



Thanks for all of the advice.

I wound up creating a table (I know, weak) in order to center the main container div. That's the most fool-proof method, especially when you have to start worry about Mac browsers.

[offhollywooddigital.net...]

What I didn't know when I asked the question is that absolute positioning references the container layer and not the browser porthole for co-ordinates (0,0).

Thanks again --

Tutorialized

10:11 pm on Jan 12, 2005 (gmt 0)



Thanks ahmedtheking!

I was having that problem in IE (firefox was working fine without the DTD).

Added the DTD and now it center aligns in IE as well. Muchos gracias. :)