I just wanna center a div!

Out of context: Reply #4

  • Started
  • Last post
  • 39 Responses
  • abettertomorrow0

    Actually here's what will work (CSS only). The part about the background image is a little confusing the way you explained it, but assuming you want to just center some images in the middle of the page...

    Put all of the logos inside a two containing divs. For the margins of the individual logos, just have them set to auto, or whatever makes them line up correctly inside the containers.

    Then for the first containing div, give it a negative vertical margin equal to half its height (ie, -100px if it is 200px tall). For the outer container, give it a vertical margin of 50% and a horizontal margin of auto. That should center your images both horizontally and vertically within the page.

    • The key takeaway here being that while auto might not work vertically, percentage does.abettertomorrow
    • Won't that align all the logos along their top edge rather than vertically center them?thatboyneave
    • It would vertically align the container, then inside the container the logos are just aligned normally.abettertomorrow
    • It's basically the same as Stugoo's second link above.abettertomorrow
    • I think he wants to center the images vertically toothatboyneave
    • face palmmydo

View thread