HTML 表格的預設樣式實在很難看,但只要幾行 CSS,就能做出乾淨俐落的表單式表格。下面這份範例示範如何處理 caption、表頭、儲存格與邊框的樣式。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>經營類項目驗收費用決算審批表</title>
<meta name="Generator" content="EditPlus">
<style>
table,th,td,caption{ margin:0px; padding:0px; line-height:18px; color:#000; font-size:12px; font-family:Microsoft YaHei;}
.mytable th{ background:#BCE774; text-align:left; font-weight:normal; width:150px; padding:6px;}
.mytable caption{ background:#328AA4; color:#FFF; line-height:30px; border:1px solid #000;}
.mytable td{ background:#ECFBD4; padding:3px;}
.mytable th,.mytable td{border-top:1px solid #000;border-left:1px solid #000;}
.mytable{border-bottom:1px solid #000;border-right:1px solid #000;}
</style>
</head>
<body>
<table class="mytable" cellpadding="0" cellspacing="0">
<caption>經營類項目驗收費用決算審批表</caption>
<tr>
<th>項目名稱</th>
<td><input type="text" /></td>
<th>項目負責人</th>
<td><input type="text" value="" /></td>
</tr>
<tr>
<th>項目目標完成情況<br />完成時間</th>
<td colspan="3"><textarea cols="55" rows="4"></textarea></td>
</tr>
<tr>
<th>費用預算總額<br />單位:元(現金)</th>
<td><input type="text" /></td>
<th>費用實際列支總額<br />(現金)</th>
<td><input type="text" /></td>
</tr>
<tr>
<th>費用預算總額<br />(物品)</th>
<td><input type="text" /></td>
<th>費用實際列支費用總額<br />(物品)</th>
<td><input type="text" /></td>
</tr>
<tr>
<th>內部資源預算總額</th>
<td><input type="text" /></td>
<th>內部資源實際列支總額</th>
<td><input type="text" /></td>
</tr>
<tr>
<th>項目費用,列支明細<br />(人力及開發費用)</th>
<td colspan="3"><textarea cols="55" rows="4"></textarea></td>
</tr>
<tr>
<th>項目費用,列支明細<br />(非人力成本報銷明細及內部其它結算明細)</th>
<td colspan="3"><textarea cols="55" rows="4"></textarea></td>
</tr>
<tr>
<th>實際稅收成本</th>
<td colspan="3"><textarea cols="55" rows="4"></textarea></td>
</tr>
<tr>
<th>實際業務提成</th>
<td colspan="3"><textarea cols="55" rows="4"></textarea></td>
</tr>
</table>
</body>
</html>
| 項目名稱 | 項目負責人 | ||
|---|---|---|---|
| 項目目標完成情況 完成時間 | |||
| 費用預算總額 單位:元(現金) | 費用實際列支總額 (現金) | ||
| 內部資源預算總額 | 內部資源實際列支總額 | ||
用 <th> 而非 <td> 當欄位標題:這不只是視覺問題。th 本身就帶有「表頭」語義,螢幕閱讀器能據此建立欄位的關聯,是無障礙上的必要做法。
用 <caption> 而非外掛標題:caption 是表格專屬的標題元素,語義準確,樣式也完全可控(這裡用深藍底白字與 30px 行高做出標題列的效果)。
邊框只畫上與左:這是畫表格框線的經典技巧——th 與 td 只設 border-top 和 border-left,再給 table 本身補 border-bottom 與 border-right,這樣相鄰儲存格之間就不會出現雙倍粗細的線,也不需要 border-collapse。
用 colspan 合併儲存格:需要大面積輸入空間的欄位(如明細說明)用 colspan="3" 橫向合併,比塞進一個小 textarea 美觀得多。
cellpadding="0" 搭配 CSS padding:先把 HTML 屬性的內距歸零,改用 CSS 的 padding 統一控制,避免兩套機制互相干擾。
參考:
https:///a/bjac/u1i5j3g9.htm
https:///a/bjad/1rp5vfq4.htm
https:///a/bjad/y20tu6yi.htm