Results 1 to 8 of 8

Thread: trouble centering page with CSS

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Question trouble centering page with CSS

    I've been working on a page for quite some time and I could never get my page dynamically centered (I'm using XHTML Strict), but then I found this ssite that was centered with CSS*. I noticed that it all seems to be centered with:
    Code:
    #holder {
        background: #fff;
        border: 1px solid #000;
        margin: 10px auto;
        width: 760px;
        /*position: absolute;*/
    }
    When I drop the comments from around the position statement the page fails to center. Now, the way my page is set-up (I can't post links since I was subcontracted at the last minute so while I did the best I could and it's decent... it's still not properly centered**) is that there is a lot of <div>s using position:absolute to line-up the images that are part of the decoration/content. Now all the tags are ultimately nested within a <div> that that encompasses all of the <body> and is id="page".

    I've changed the style for page in the .css file to :
    div#page{margin: 10px auto;}
    and the page doesn't center!! I realize that "absolute" breaks "margin" but "page" is now a top-level tag and it is not absolute. My understanding of using "absolute" is that it applies moving the container with respect to it's container, so that moving the parent container would move the child by the additional of the parent. In fact, I have a triple-nested div that works that way on the page (without and margin setting though). Does anyone have any suggestions?

    I get this problem with both Firefox 1.0 and IE 6.0 and both my xhtml and css are standard compliant.

    *Disclaimer: I'm not advocating the site or supporting it's content, mearly using it as an example.
    **Actually, out of frustration I just statically centered it on the screen assuming browsing at 1024x800 using the fullscreen
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: trouble centering page with CSS

    The margin trick only works on objects that are in the flow. This means it won't work for position: absolute. Why do you need position: absolute?

    Objects with position: absolute are positioned relative to their containing positioning context. A positioning context is started by any element that has a position value other than static.

    If you need some random element in the flow to become a positioning context, give it position: relative.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Re: trouble centering page with CSS

    Well the site contains a various adjacent images theat are supposed to align and the center image (kinda of like a 3x3 grid) gets replaced by various onmouseover events. As these images need to lineup (the are really just one image spread over a few .gif files-- I hate the design but it's what the customer wanted... or so I was told by the boss) I found it troublesome to line up the images up with position:relative having to substract 4px for each image to to compensate for image spacing and whatnot (the images need to be touching since the figure "flows" through them).

    I also figured that position:absolute would be more consistent amongst browsers than position:relative since it leaves less to interpretation.

    This is my first page ever with CSS since I usually do sererside programming but they needed someone on short notice.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: trouble centering page with CSS

    What image spacing?
    Code:
    img { padding: 0; margin: 0; }
    Can you show me a little schema of what you're trying to achieve?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: trouble centering page with CSS

    I had a similar issue.... it ceneterd in FF, but not in IE.... I found that, and this seems most rediculous..... margin: auto auto auto auto; worked.....

    Also, sometimes cleaning out your cache helps. I had that happend to me a couple of times where the CSS got cached and no changes to it took.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: trouble centering page with CSS

    If you have nine equally sized images that you want in a 3x3 pattern I would just create one big <div> to hold them all and then float the images.
    Code:
    img { 
        float: left; 
        margin: 0; 
        padding: 0;
    }
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  7. #7

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Re: trouble centering page with CSS

    Quote Originally Posted by McCain
    If you have nine equally sized images that you want in a 3x3 pattern I would just create one big <div> to hold them all and then float the images.
    Nope! The central image is huge and the remaining 8 are essentially border pieces. I'll try setting the spacing and padding to 0 (I don't know all the properties of CSS that I can set... I've just done the W3Schools CSS Tutorial, some trial and error, and what I've learned trying to validate).

    If it doesn't work I'll brew up an example and post it here... if it does I'll let you guys know. Thanks for all the help so far.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  8. #8
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: trouble centering page with CSS

    Well, I guess they don't have to be exactly the same size... Sorry, didn't think of this.
    It should be enoug if:
    * top-left, top, and top-right have the same height
    * top-left, left, and bottom-left have the same width
    * top-right, right, and bottom-right have the same width
    * bottom-left, bottom, and bottom-right have the same height
    * top and bottom are as wide as center
    * left and right are as high as center

    (All the above points would be met if it's just a center image with a simple frame/border around it)

    If you put sizes in the <img>-tagg to make the images confirm to the above points I think that would work as well
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width