Go Back   Dynamic Drive Forums > General Coding > HTML
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-18-2007, 03:09 PM
dog dog is offline
Regular Coders
 
Join Date: Apr 2006
Posts: 178
Thanks: 6
Thanked 0 Times in 0 Posts
Default DOCType and Vertical Positioning

Hello,

I want to position my page content inside a box that sits in the centre of the browser window. Example: http://ll.dc5b.com.

In the example the box holding the page content is placed inside a td and given a 'verticle-position: middle;'. See the example below:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>

<title>Example</title>

<style type="text/css">
body { text-align: center; }

table#centreHolder { height: 100%; width: 100%;  }

table#centreHolder td { vertical-align: middle; }

div#containerBox {width: 600px; margin: auto; border: 1px solid black; } 
</style>
</head>

<body>
<table id="centreHolder"><tr><td>
  <div id="containerBox">SITE CONTENT</div>
</td></tr></table>
</body>
However when I use a 4.01 DOCTYPE it changes everything.
Example:
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Example</title>

<style type="text/css">
body { text-align: center; }

table#centreHolder { height: 100%; width: 100%;  }

table#centreHolder td { vertical-align: middle; }

div#container {width: 600px; margin: auto; border: 1px solid black; } 
</style>
</head>

<body>
<table id="centreHolder"><tr><td>
  <div id="container">SITE CONTENT</div>
</td></tr></table>
</body>
Can anyone offer me an alternative way of acheiving the same effect using a 4.01 doctype?

Thanks,
dog
Reply With Quote
  #2  
Old 06-18-2007, 03:52 PM
alexjewell alexjewell is offline
Senior Coders
 
Join Date: Mar 2006
Location: Cleveland, Ohio
Posts: 522
Thanks: 2
Thanked 5 Times in 5 Posts
Default

What changes when using a 4.01 doctype?
__________________
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
Reply With Quote
  #3  
Old 06-18-2007, 04:09 PM
dog dog is offline
Regular Coders
 
Join Date: Apr 2006
Posts: 178
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by alexjewell View Post
What changes when using a 4.01 doctype?
Two things actually. The 'vertical-align: middle;', seems to have no effect and also the 'text-align: center' from the body gets inherited by all the child elements.
Reply With Quote
  #4  
Old 06-18-2007, 05:01 PM
alexjewell alexjewell is offline
Senior Coders
 
Join Date: Mar 2006
Location: Cleveland, Ohio
Posts: 522
Thanks: 2
Thanked 5 Times in 5 Posts
Default

ok, the vertical-align: middle is something I'm not entirely sure about.

To fix the child elements inheriting the text-align: center; do the following in your css stylesheet:

Code:
body *{
text-align: left;}
__________________
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
Reply With Quote
  #5  
Old 06-18-2007, 05:48 PM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 18,999
Thanks: 19
Thanked 1,134 Times in 1,120 Posts
Blog Entries: 3
Default

As far as I know, there is no height:100% in HTML 4.01 unless the height of the container (in the case of your demo, the container is the body) is specified.
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
  #6  
Old 06-18-2007, 06:37 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

Hahahahaha, HTML 3.2 Final? Seriously? I didn't know anybody had actually used that within the past... eight years? The latest well-supported DOCTYPE, and the one you should have been using for the past half a decade at least, is HTML 4.01 Strict.

It's very bad style to use tables for layout. Don't do this. Avoid pixel-sizing things too, as this results in fragile, inflexible designs. Try this page for an explanation of your vertical-align problem.
Quote:
Originally Posted by jscheuer1
As far as I know, there is no height:100% in HTML 4.01 unless the height of the container (in the case of your demo, the container is the body) is specified.
The <body>'s height is considered to be specified at the height of the viewport.
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #7  
Old 06-18-2007, 06:57 PM
mwinter mwinter is offline
Elite Coders
 
Join Date: Dec 2004
Location: UK
Posts: 2,361
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Twey View Post
The <body>'s height is considered to be specified at the height of the viewport.
100% of the height of the root element is the height of the viewport. For HTML, that's the html element.
__________________
Mike
Reply With Quote
  #8  
Old 06-18-2007, 10:15 PM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 18,999
Thanks: 19
Thanked 1,134 Times in 1,120 Posts
Blog Entries: 3
Default

That makes it sound as though you could set the body's height to 100% and it would then be the height of its container, the HTML element and therefore the height of the viewport, but it isn't.

There was an oversight in developing the standard for HTML 4.01 in that you cannot vertically center unless you know the height of the content. Even then, you have to jump through hoops to do it and it has a nasty side effect when the viewport is shorter than the content.

Javascript can come to rescue, and in IE, using the expression technique in style, this can work out quite well, even though it is somewhat involved. In all others, you are left with content that must 'snap into place' onload.

Either way, it flops with javascript disabled.

If you must have content of unknown and/or of changing height vertically centered, I'd suggest sticking with quirks mode all the way:

Code:
<html>
<head>

<title>Example</title>

<style type="text/css">
#container {width: 600px;border: 1px solid black; } 
</style>
</head>

<body>
<table height="100%" align="center"><tr><td valign="middle">
  <div id="container">SITE CONTENT</div>
</td></tr></table>
</body>
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Last edited by jscheuer1; 06-18-2007 at 10:20 PM.
Reply With Quote
  #9  
Old 06-18-2007, 10:30 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

http://www.wpdfd.com/editorial/thebox/deadcentre4.html
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Reply With Quote
  #10  
Old 06-18-2007, 11:02 PM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 18,999
Thanks: 19
Thanked 1,134 Times in 1,120 Posts
Blog Entries: 3
Default

This is another example of needing to know the height of the centered content. A bit overly complex of one at that.

Quote:
To get it to
centre vertically, it has a negative top position that is exactly half
of its height
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Forum Jump


All times are GMT. The time now is 04:36 PM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.