[SOLVED] Centering Vertically

This is a discussion on "[SOLVED] Centering Vertically" within the Web Page Design section. This forum, and the thread "[SOLVED] Centering Vertically 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 Dec 6th, 2007, 08:32
Reputable Member
Join Date: May 2007
Location: Margate
Age: 24
Posts: 156
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Centering Vertically

Good Morning,
I am trying to center the body html vertically and horizantally. Thought it would have been very simple.

Code: Select all
body {
 background-color:#313e5b;
 margin:auto;
 padding:0;
 text-align:center;
 width: 780px;
 height: 500px;
}
Where am I going wrong?

Cheers

Dan
Reply With Quote

  #2 (permalink)  
Old Dec 6th, 2007, 08:38
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,649
Thanks: 0
Thanked 8 Times in 8 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: Centering Vertically

Centering your site vertically is a pain in the butt.

I have never tried to center a website vertically but i have found an example here: http://www.wpdfd.com/editorial/thebox/deadcentre4.html.
Reply With Quote
  #3 (permalink)  
Old Dec 6th, 2007, 08:53
welshstew's Avatar
Lead Administrator

SuperMember
Join Date: May 2007
Location: inside the outside
Posts: 1,388
Blog Entries: 13
Thanks: 1
Thanked 16 Times in 14 Posts
Re: Centering Vertically

never tiried it before but look here, it seems to have the solution you are looking for, you can also look at W3C for an example
__________________
WelshStew
Lead Administrator

tierney rides tboard - uk site | xtreme wales - extreme clothing
If you think I've helped, click the "Thanks"
webforumz - facebook | LinkedIn
Last Blog Entry: Web Standards Curriculum Launched (Jul 8th, 2008)
Reply With Quote
  #4 (permalink)  
Old Dec 6th, 2007, 10:48
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering Vertically

Hi Dan,

the problem is, that you cannot change the size of the body element. The body element always represents the complete visible page. Instead you have to place a separate div inside the body. To get that div centered, try something like

Code: Select all
.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -250px;
  margin-left: -390px;
  width: 780px;
  height: 500px;
  background-color:#313e5b;
}
And the HTML should then look like

Code: Select all
<body>
<div class="centered">
    Content here
</div>
</body>
Hope this helps
Reply With Quote
  #5 (permalink)  
Old Dec 6th, 2007, 10:57
Reputable Member
Join Date: May 2007
Location: Margate
Age: 24
Posts: 156
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering Vertically

hschmitz,
That has worked so much less code. If i was to change the height how would I know what measurements the others should change to,

For example height to 600px.

Cheers for this help everyone. Greatly apprieciated

Daniel
Reply With Quote
  #6 (permalink)  
Old Dec 6th, 2007, 11:05
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,649
Thanks: 0
Thanked 8 Times in 8 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: Centering Vertically

Code: Select all
.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -300px;
  margin-left: -390px;
  width: 780px;
  height: 600px;
  background-color:#313e5b;
}
Daniel, that code above should do the trick.

Basically for the margins (left & top), you need to half the widths and heights so:

margin-top = -height / 2
margin-left = -width / 2

Hope that helps in future!


Thread marked as solved!
Reply With Quote
Reply

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 horizontally and vertically Emzi Web Page Design 4 Nov 1st, 2007 16:07
[SOLVED] Problems with centering in CSS. mcdanielnc89 Web Page Design 26 Nov 1st, 2007 06:07
[SOLVED] Centering! mcdanielnc89 Web Page Design 20 Oct 21st, 2007 22:02
Vertically centering Div's verkevinmanering69err455 Web Page Design 3 Mar 19th, 2007 17:22
centering vertically benbacardi Web Page Design 17 May 27th, 2004 13:51


All times are GMT. The time now is 19:37.


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