2016-07-13 65 views
1

我有兩個表,就像這樣的垂直對齊:兩個表

#reference 
 
{ 
 
\t font-family: "Roboto", sans-serif; 
 
\t font-size: 14px; 
 
\t margin: 0px 10px 0px 15px; 
 
\t text-align: left; 
 
\t border-collapse: collapse; 
 
\t color: navy; 
 
} 
 

 
#reference th 
 
{ 
 
\t padding: 8px; 
 
\t font-size: 16px; 
 
\t font-weight: normal; 
 
\t background: yellow; 
 
\t border-top: 2px solid #000; 
 
\t border-bottom: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: navy; 
 
\t text-align: center; 
 
} 
 

 
#reference td 
 
{ 
 
\t padding: 8px; 
 
\t border-bottom: 1px solid #000; 
 
\t border-top: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: #669; 
 
} 
 

 
#reference tbody tr:hover td 
 
{ 
 
\t background: pink; 
 
\t color: #339; 
 
} 
 

 
#copy 
 
{ 
 
\t font-family: "Roboto", sans-serif; 
 
\t font-size: 14px; 
 
\t margin: 0px 10px 0px 15px; 
 
\t text-align: left; 
 
\t border-collapse: collapse; 
 
\t color: navy; 
 
} 
 

 
#copy th 
 
{ 
 
\t padding: 8px; 
 
\t font-size: 16px; 
 
\t font-weight: normal; 
 
\t background: yellow; 
 
\t border-top: 2px solid #000; 
 
\t border-bottom: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: navy; 
 
\t text-align: center; 
 
} 
 

 
#copy td 
 
{ 
 
\t padding: 8px; 
 
\t border-bottom: 1px solid #000; 
 
\t border-top: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: #669; 
 
} 
 

 
#copy tbody tr:hover td 
 
{ 
 
\t background: pink; 
 
\t color: #339; 
 
}
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <title>Tables test</title> 
 
\t \t <link href="style.css" rel="stylesheet" type="text/css"> 
 
\t \t <meta charset="utf-8"> 
 
\t </head> 
 
\t <body> 
 
\t \t <table id="reference"> 
 
\t \t \t <thead> 
 
\t \t \t \t <th style="width: 100px;">t1 - header 1</th> 
 
\t \t \t \t <th style="width: 200px;">t1 - header 2</th> 
 
\t \t \t \t <th style="width: 250px;">t1 - header 3</th> 
 
\t \t \t </thead> 
 
\t \t <tbody> 
 
\t \t \t <tr> 
 
\t \t \t \t <td>t1 - value 1</td> 
 
\t \t \t \t <td>t1 - value 2</td> 
 
\t \t \t \t <td>t1 - value 3</td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td>t1 - value 4</td> 
 
\t \t \t \t <td>t1 - value 5</td> 
 
\t \t \t \t <td>t1 - value 6</td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td>t1 - value 7</td> 
 
\t \t \t \t <td>t1 - value 8</td> 
 
\t \t \t \t <td>t1 - value 9</td> 
 
\t \t \t </tr> 
 
\t \t </tbody> 
 
\t \t </table> 
 

 
\t \t <table id="copy"> 
 
\t \t \t <thead> 
 
\t \t \t \t <th style="width: 300px; height: 0;">t2 - header 1</th> 
 
\t \t \t \t <th style="width: 250px; height: 0;">t2 - header 2</th> 
 
\t \t \t </thead> 
 
\t \t \t <tbody> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td style="width: 300px;">t2 - value 1</td> 
 
\t \t \t \t \t <td style="width: 250px;">t2 - value 2</td> 
 
\t \t \t \t </tr> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td style="width: 300px;">t2 - value 3</td> 
 
\t \t \t \t \t <td style="width: 250px;">t2 - value 4</td> 
 
\t \t \t \t </tr> 
 
\t \t \t </tbody> 
 
\t \t </table> 
 
\t </body> 
 
</html>

我需要 「T2 - 頭1」 爲具有確切相同的寬度, 「T1 - 頭1」 + 「t1 - 標題2」。我在互聯網上運行了幾次搜索,發現了相同的解決方案,但所有的作品都有相同的寬度,而我需要預定義的列寬。有任何想法嗎?

+1

你需要有兩個不同的表中,或者一個高級的外觀他們合併?另外,這不是有效的HTML5。 – j08691

+0

兩個不同的表。 –

回答

0

爲什麼不能用百分比?

如果你堅持550px,只需在代碼中設置它,並使用550px的百分比或將來使用的任何表格來保持它不變。

你可以考慮引導表,以獲得在表操作(濃縮表,響應表等)

table { 
 
    width:550px; 
 
} 
 

 
#reference 
 
{ 
 
\t font-family: "Roboto", sans-serif; 
 
\t font-size: 14px; 
 
\t margin: 0px 10px 0px 15px; 
 
\t text-align: left; 
 
\t border-collapse: collapse; 
 
\t color: navy; 
 
} 
 

 
#reference th 
 
{ 
 
\t padding: 8px; 
 
\t font-size: 16px; 
 
\t font-weight: normal; 
 
\t background: yellow; 
 
\t border-top: 2px solid #000; 
 
\t border-bottom: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: navy; 
 
\t text-align: center; 
 
} 
 

 
#reference td 
 
{ 
 
\t padding: 8px; 
 
\t border-bottom: 1px solid #000; 
 
\t border-top: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: #669; 
 
} 
 

 
#reference tbody tr:hover td 
 
{ 
 
\t background: pink; 
 
\t color: #339; 
 
} 
 

 
#copy 
 
{ 
 
\t font-family: "Roboto", sans-serif; 
 
\t font-size: 14px; 
 
\t margin: 0px 10px 0px 15px; 
 
\t text-align: left; 
 
\t border-collapse: collapse; 
 
\t color: navy; 
 
} 
 

 
#copy th 
 
{ 
 
\t padding: 8px; 
 
\t font-size: 16px; 
 
\t font-weight: normal; 
 
\t background: yellow; 
 
\t border-top: 2px solid #000; 
 
\t border-bottom: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: navy; 
 
\t text-align: center; 
 
} 
 

 
#copy td 
 
{ 
 
\t padding: 8px; 
 
\t border-bottom: 1px solid #000; 
 
\t border-top: 1px solid #000; 
 
\t border-left: 1px solid #000; 
 
\t border-right: 1px solid #000; 
 
\t color: #669; 
 
} 
 

 
#copy tbody tr:hover td 
 
{ 
 
\t background: pink; 
 
\t color: #339; 
 
}
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <title>Tables test</title> 
 
\t \t <link href="style.css" rel="stylesheet" type="text/css"> 
 
\t \t <meta charset="utf-8"> 
 
\t </head> 
 
\t <body> 
 
\t \t <table id="reference"> 
 
\t \t \t <thead> 
 
\t \t \t \t <th style="width: 20%;">t1 - header 1</th> 
 
\t \t \t \t <th style="width: 35%;">t1 - header 2</th> 
 
\t \t \t \t <th style="width: 45%;">t1 - header 3</th> 
 
\t \t \t </thead> 
 
\t \t <tbody> 
 
\t \t \t <tr> 
 
\t \t \t \t <td>t1 - value 1</td> 
 
\t \t \t \t <td>t1 - value 2</td> 
 
\t \t \t \t <td>t1 - value 3</td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td>t1 - value 4</td> 
 
\t \t \t \t <td>t1 - value 5</td> 
 
\t \t \t \t <td>t1 - value 6</td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td>t1 - value 7</td> 
 
\t \t \t \t <td>t1 - value 8</td> 
 
\t \t \t \t <td>t1 - value 9</td> 
 
\t \t \t </tr> 
 
\t \t </tbody> 
 
\t \t </table> 
 

 
\t \t <table id="copy"> 
 
\t \t \t <thead> 
 
\t \t \t \t <th style="width: 55%;">t2 - header 1</th> 
 
\t \t \t \t <th style="width: 45%;">t2 - header 2</th> 
 
\t \t \t </thead> 
 
\t \t \t <tbody> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td>t2 - value 1</td> 
 
\t \t \t \t \t <td>t2 - value 2</td> 
 
\t \t \t \t </tr> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td>t2 - value 3</td> 
 
\t \t \t \t \t <td>t2 - value 4</td> 
 
\t \t \t \t </tr> 
 
\t \t \t </tbody> 
 
\t \t </table> 
 
\t </body> 
 
</html>

+1

工作正常!謝謝! –

0

所有你需要做的是改變的寬度「T2 - 標題1」

<th style="width: 317px; height: 0;">t2 - header 1</th> 
+0

從哪裏得到317?這背後的邏輯是什麼?如果我需要另一個佈局,如何開始嘗試不同的寬度,從像素到像素,直到表格適合?我正在尋求一個通用的解決方案。 –