31

When I use HTML without DOCTYPE, my HTML page Height=100% is working.

But when I use DOCTYPE, height is not working correctly...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>

<table cellpadding="0" cellspacing="0" width="11" height ="100%">
    <tr>
        <td height="100%" bgcolor="#008000">&nbsp;</td>
    </tr>
</table>

</body>

How to solve this problem?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Alex
  • 1,933
  • 9
  • 36
  • 40

3 Answers3

54

Adding a DOCTYPE switches you from quirks mode to standards mode. In standards mode, the html and body elements do not default to 100% of the size of the viewport (browser window); instead, they are only as large as they need to be to contain their children. The table height of 100% means 100% of the containing element, but that's only as large as it needs to be to contain the contents of the table. Quirks mode is an emulation of the behavior of older browsers, in which the html and body elements filled the viewport.

To fix the problem, you just need to add this to your document:

<style>
  html, body { height: 100% }
</style>
Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • i try like above , but width(100%) is working but height not working – Alex Dec 28 '09 at 09:01
  • Can you edit your post with the exact code that you are trying, and which browser(s) it fails on? – Brian Campbell Dec 28 '09 at 12:56
  • I tested this in Firefox, and result is Okay. thanks @Brian Campbell – mostafa.S May 12 '13 at 09:28
  • I just read this 11 years old answer and it put me on the right track to solve my very frustrating problem. I needed to add "height: 100%" to some
    css classes too. Thank you!
    – Mikkel Mar 01 '21 at 18:15
4

When you state 'height=100%' you have to think "100% of what?". It's the parent of that table element which is the body. But how tall is the body?

To do what you want, add this in the CSS: html,body{height:100%}

Rob
  • 14,746
  • 28
  • 47
  • 65
1

Like everyone said the main part of the solution is to put

html, body{'height=100%'}

But its also really important to set every single ancestor's height to be 100%. for example if your code is as follows

<body>
    <div id="firstdiv">
         <div id="scnddiv">
               lets say this is the div you want to make 100%
         </div>
    </div>
</body>

if #scnddiv is the div you want to make 100%, not only you have to make html and body 100% but also #firstdiv. So it will look something like this

html, body, #firstdiv {height:100%}

then you can make anything in that div to be 100%.

I hope this helps.

user4504661
  • 493
  • 7
  • 14