tags:

views:

165

answers:

2

Hi,

I am trying to middle align an element, But I have bumped into this problem. I am using this tag:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
  <body>
      <table style="width:100%; height: 100%" cellspacing="0" cellpadding="0">
                <tr>
                    <td vertical-align="middle">
                        <div id="progressContainer" style="text-align:center;">
                            Some string here                                                    
                        </div>
                    </td>
                </tr>       
            </table>  
  </body>
</html>

If I remove this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;, its middle aligned properly.

This behavior is reproducible in FF and chrome.

Question:

Am I doing anything wrong here?

A: 

When in Quirks mode, browsers make deliberate errors.

Since the height of the body element is auto (the default), the height: 100% on the table element becomes auto.

(Percentage heights are converted to auto if the parent element has height: auto)

David Dorward
I gave <body height="100%">, did not have an effect on the output.
Manohar
… the HTML element still has `height: auto` so the body get that too!
David Dorward
But I dont want to specify height in harcoded pixels which could introduce UI bugs. Is there any other way?
Manohar
`html { height: 100%; }`
David Dorward
+2  A: 

The attribute is valign="middle" or style="vertical-align: middle". There is no attribute vertical-align.

See 11.3.2 Horizontal and vertical alignment

nikc
I tried with <td style="vertical-align: middle">, but it does not seem to have any effect.
Manohar
Try adding borders to the `table` (and perhaps the `td`) to see that the table actually has a height. Are you using any devtools, e.g. Firebug?
nikc
See demo: http://nikc.kapsi.fi/dev/html/misc/table-valign-demo.html
nikc
Yes I am using firebug, let me check that.
Manohar
Firebug let's you see the implicit and explicit styles associated with any DOM object. Very useful for debugging strange or unexpected behaviour.
nikc
It seems default height is being applied in quirks mode. height: 100% is not taken into account, however width: 100% is applied.
Manohar
As David says, you have to apply `height: 100%` to `html`, `body` and `table` for it to work. See the source of my example.
nikc
Thanks for this, I got it to work.
Manohar