Welcome to Dream.In.Code
Become an Expert!

Join 149,527 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,478 people online right now. Registration is fast and FREE... Join Now!




Vertical Centering

 
Reply to this topicStart new topic

Vertical Centering

im@geek
29 May, 2007 - 10:03 PM
Post #1

D.I.C Head
Group Icon

Joined: 18 Feb, 2005
Posts: 87



Thanked: 1 times
Dream Kudos: 125
My Contributions
Hi all,

I know you can dynamicly center DIVs horizontaly using:

CODE
margin:0 auto;


but does anyone know if there is any way to dynamicaly center a DIV vertically?
User is offlineProfile CardPM
+Quote Post

Arbitrator
RE: Vertical Centering
31 May, 2007 - 12:29 AM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Jan, 2005
Posts: 492



Thanked: 1 times
My Contributions
Depends on what you want to center. If the content has dynamic height (like a block of text), then you would use CSS tables (display: table and display: table-cell) in conjunction with the vertical-align: middle declaration. Due to Internet Explorer’s lack of support, you would have to misuse a table element to vertically center such content in that browser.

If the content to be centered has a fixed or percentage‐based height, you can use absolute positioning. Internet Explorer doesn’t support the pure absolute positioning method; this may mean that you can’t center content with a percentage‐based height. I’ve never tested it. On the other hand, absolute positioning can be combined with negative margins and Internet Explorer does support this:

CODE
/* two “div” elements, one nested inside the other */
/* you may be able to apply the negative margin to the outer “div” element and scrap the inner; I can’t recall */
/* negative top margin is exactly half the height */
div { position: absolute; top: 50%; width: 500px; height: 500px; }
div div { height: 100%; margin: -250px 0 0; }


Finally, to center a single line of text, you can use line-height:

CODE
/* div contains one line of text */
div { height: 500px; line-height: 500px; }

User is offlineProfile CardPM
+Quote Post

im@geek
RE: Vertical Centering
31 May, 2007 - 09:42 PM
Post #3

D.I.C Head
Group Icon

Joined: 18 Feb, 2005
Posts: 87



Thanked: 1 times
Dream Kudos: 125
My Contributions
No dice, but thanks

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month