2011-07-16 45 views
4

我想讓我的桌子佔用屏幕的80%,但現在它只是表格中內容的大小。css高度80%不工作

#ecom-mainarea .center 
{ 
margin-left: 10%; 
position: relative; 
width: 80%; 
height: 80%; /* when this is 500px it works fine, but % doesn't work */ 
border: 1px solid; 
border-bottom-color: teal; 
border-top-color: gray; 
border-left-color: gray; 
border-right-color: teal; 
background-color: white; 
voice-family: "\"}\""; 
voice-family: inherit; 
vertical-align: text-top; 
} 

回答

7

您需要確保htmlbody元素的高度爲100%。他們需要從頂部延伸到底部。如果不是htmlbody元素將會和你的表一樣高。

在這裏,你有一個工作的樣本:

<!doctype html> 
<html> 
<head> 
    <title>Table height</title> 
    <style> 
     html, body 
     { 
      padding: 0; 
      margin: 0; 
      height: 100%; 
     } 
    </style> 
</head> 
<body> 
    <table style="background: cyan; height: 80%;"> 
     <tr> 
      <td> 
       Table has 80% of the height of the body 
      </td> 
     </tr> 
    </table> 
</body> 
</html> 
0

如果我記得,百分比不支持CSS高度。要訴諸其他方法。爲您指定的父元素的高度(絕對單位)

+0

奇怪。寬度似乎正在工作。任何其他方法的建議? – 0x4f3759df

+2

支持。 http://www.w3.org/TR/CSS21/visudet.html#the-height-property –

+1

我應該澄清 - 我不認爲它適用於瀏覽器高度,但它應該相對於其容器工作。 –

1

您可以使用一個黑客位的

+0

你不需要圖像。我以前見過這個,不知道在哪裏。 http://jsfiddle.net/2vRrZ/4/ – thirtydot

+0

@thirtydot:我想避免'inline-block'。儘管我認爲它不會在IE6上運行。 – Ryan

0

作爲設計建議,除非你真的想擁有它作爲我建議爲表的固定大小的百分比,所以無論他們的分辨率是什麼,每臺計算機上的佈局看起來都一樣。

+1

已修復。我不知道他們是如此爭議,謝謝你的分享。 – rajh2504