SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can not get div to center using margin 0 auto.

    Hi,
    I am working with a Wordpress theme in a child theme style sheet
    If you resize the page you will see that when there is a lot of spare space depending on how many images fit in a row they sit to the left with the space on the right. I want the rows of images to be centered no matter how many images there are in a row.
    I think the div I want to center horizontally is #main, but I could be wrong
    I have tried all sorts of things, width auto, margin 0 auto etc. For now I have removed any of my custom CSS related to that div and its parent so that it is as original as possible.
    The page is here http://stephanieh57.staging.wpengine.com/shop/

    full path of div
    html.parent body.archive div#wrapper.parent div#content.col-full div#main.col-left

  2. #2
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,821
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Am unclear as to what you want , or if it's actually possible :/. So lets address the tech issues first:
    in order to center a BLOCK element ASIDE from margin: 0 auto; you will need to give that element some sort of width, be it in px, %, em etc. I dont see that anywhere in your code, so that may be PART of the issue.

    I am not user if the element you want to center is the one you listed in your OP, but this one may work just as well ( again that's depending on your overall goals) :
    Code:
    ul.products {
        margin: 0 auto !important; width:650px;
    }
    Note I added a width so you can see how that works.

    You could also center your products this way:
    Code:
    ul.products {text-align:center;}
    ul.products li  {display:inline-block; text-align:left;}

    hope that helps

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,
    apologies first if anyone has been trying to look at the page. I had a problem with a theme update for a while. All back to normal now.
    I see what you mean about a width.
    To clarify what I want...
    If I want the rows of images in the ul to be able to flow to any number depending on screen size I can not fix the width.
    On my screen I get a nice 4 images per row. But size my browser window down and it flows to rows of 3. Eventually 2 then 1. That is what I want. BUT I want the rows to always be centered.
    Is that then not possible to achieve with CSS?
    If not could Javascript do it?

  4. #4
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,821
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    IF the width of your image blocks, is always the same , you could try :
    Code:
    ul.products {text-align:center;}
    ul.products li  {display:inline-block; text-align:left;}
    but normally centering doesn't work that way.

    When you center a WRAPPER element , you NEED to restrict it's size. ( the browser calculates: margins= (100%-width)/2.. and how would anyone be able to figure out margin w/o knowing width? )
    MEANWHILE the element inside are are merely flowing in line... one after the next, this may give the appearance of columns...but is just slight of hand. The elements are actually lined up one after the other, when the browser cant fit an INTEGER # of elements into a row it leaves the remaining space open. In other words if the width of the container is 3.7 images wide, there will appear to be a gap of .7 of an image. There is really no way around that other than the method I suggested above. But again in order for it mo maintain that grid appearance then your image containers (the LIs) must be the same width


Tags for this Thread

Bookmarks

Posting Permissions

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