2010-12-14 114 views
8

我只想將表格放在頁面中間。任何幫助都會很棒。對齊頁面中心的表格

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
<head> 
<title>Randy's first html web page !</title> 
<style type="text/css"> 
body 
{background-image:url('Koala.gif');} 
h1 
{ 
text-align:center; 
} 
h2 
{ 
text-align:center; 
} 
p 
{ 
text-align:center; 
font-size:200%; 
color:#00fff0; 
} 
div 
{ 
background-color:#efffff; 
} 
</style> 
</head> 
<h1> Hello Professor</h1> 
<h2> By: Randy White</h2> 
<P> I haven't done anything like this before.</P> 
<P>Seems to be ok</P> 
<P><img src="Hydrangeas.jpg" width="150" height="100" alt="Hydrangeas.jpg"></P> 
<table border="1"> 
<tr> 
<th>Month</th> 
<th>Day</th> 
<th>Year</th> 
</tr> 
<tr> 
<td>December</td> 
<td>1</td> 
<td>2010</td> 
</tr> 
</table> 
<a href="http://www.google.com">Visit Google!</a> 
</body> 
</html> 

回答

24

嘗試:

table { 
    width: 50%; 
    margin-left: auto; 
    margin-right: auto; 
} 

或:

table { 
    width: 200px; 
    margin-left: auto; 
    margin-right: auto; 
} 

順便說一句,這臺本作中的所有表。您可能希望在更具體的選擇器上具有屬性(如table.someclass)。

+0

這似乎沒有工作。也許我正在把它放在不正確的地方。你能告訴我這會去嗎? – user770022 2010-12-14 00:45:01

+0

我犯的一個小錯誤,我想它太短;)現在嘗試,工作。 – Kos 2010-12-14 00:48:46

+0

得到它感謝 – user770022 2010-12-14 01:03:28

2

舊風格是ALIGN =「中心」:

<table width="400" border="0" cellspacing="0" cellpadding="0" align="center"> 
2

水平對齊表格在頁面的中間,也使表格的寬度自動適應本身,請嘗試以下:

table { 
    width: auto; 
    margin-left: auto; 
    margin-right: auto; 
}