Vertically centering Div's

This is a discussion on "Vertically centering Div's" within the Web Page Design section. This forum, and the thread "Vertically centering Div's are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 17th, 2007, 00:31
Junior Member
Join Date: Mar 2007
Location: Australia
Age: 19
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Vertically centering Div's

I was working on creating a vertically centered div. I did find a way however which sadly only works in IE. Dose anybody know how to vertically center a div which in most browsers?


Heres the code for IE DIV Centering:
Code: Select all
#containerbg {
    position:absolute;
    _top:-50%;
    display:table-cell;
    vertical-align:middle;
    background-color:#FF6633;
    width:100%;
}
Reply With Quote

  #2 (permalink)  
Old Mar 17th, 2007, 10:22
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Vertically centering Div's

The traditional method of handling IE6 and Firefox/others was something like:
body: {text-align: center; margin: 0 auto;}
div.wrap {margins: [whatever] auto; text-align: left;}

setting the parent div/element to left/right margin "auto" centers it in most browsers. For IE < 7, you have to center the text in a parent, then align it back to left in the target div.

Also, at the top of your css, the following code prevents problems:
Code: Select all
* {
margin:0;
padding:0;
}

Last edited by masonbarge; Mar 17th, 2007 at 10:28.
Reply With Quote
  #3 (permalink)  
Old Mar 17th, 2007, 16:49
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Vertically centering Div's

Mason seems to have misunderstood the question. That's for horizontal centering. And he should've clarified. The * { margin: 0; padding: 0; } won't prevent problems, it will simply remove all margin and padding from every element on the page. This is sometimes helpful because different browsers have different default margin and padding settings. It is by no means necessary, however.

verkevinmanering69err455:

The code you posted is rather poor. Something like the following would work and validate.

Code: Select all
#containerbg {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 400px;
    height: 200px;
    margin: -100px 0 0 -200px; /* margin: -half-height 0 0 -half-width; */
    background: #f63;
}
Vertical align is poorly supported as I recall, so you might as well not even use it. I could be mistaken on that point because I never use it. If you don't want horizontal centering like I've posted, remove left: 50%; and set the margin to: margin: -100px 0 0;.
Reply With Quote
  #4 (permalink)  
Old Mar 19th, 2007, 17:22
moojoo's Avatar
Moderator
Join Date: Aug 2005
Location: Texas
Age: 31
Posts: 1,761
Blog Entries: 1
Thanks: 0
Thanked 18 Times in 18 Posts
Send a message via AIM to moojoo Send a message via MSN to moojoo Send a message via Yahoo to moojoo
Re: Vertically centering Div's

http://www.newguyinennis.com/samples...centering.html

http://www.newguyinennis.com/samples...ing_fluid.html
__________________
The internet is just a fad.
http://www.mevans76.com
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Reply With Quote
Reply

Tags
centering, div tag, vertical

Thread Tools

Posting 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Centering Vertically R8515198 Web Page Design 5 Dec 6th, 2007 11:05
[SOLVED] Centering horizontally and vertically Emzi Web Page Design 4 Nov 1st, 2007 16:07
Div's tags in IE = death verkevinmanering69err455 Website Planning 2 Mar 12th, 2007 04:43
Div's overlapping fluerasa Web Page Design 2 Oct 8th, 2006 10:09
centering vertically benbacardi Web Page Design 17 May 27th, 2004 13:51


All times are GMT. The time now is 08:06.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43