HTML 表格儲存格(<td> 或 <th>)支援內嵌任何流式內容,圖片(<img>)自然也不例外。正確嵌入圖片能豐富資料呈現,如商品縮圖、使用者頭像、狀態圖示等。
<table>
<thead>
<tr><th>商品</th><th>圖片</th><th>價格</th></tr>
</thead>
<tbody>
<tr>
<td>藍牙耳機</td>
<td><img src="images/headphone.jpg" alt="藍牙耳機產品圖" width="80"></td>
<td>$1,200</td>
</tr>
</tbody>
</table>
預設圖片為行內元素,垂直對齊為 baseline,易產生底部間隙。建議用 CSS 修正:
td img {
display: block;
margin: 0 auto; /* 水平置中 */
vertical-align: middle; /* 垂直置中 */
}
為適應不同螢幕尺寸,搭配 max-width: 100% 且 height: auto,確保圖片不超出儲存格寬度:
td img {
max-width: 100%;
height: auto;
}
<img src="photo.jpg" alt="團隊照片"
onerror="this.src='images/placeholder.png'; this.onerror=null;">
alt 屬性alt="" 或 CSS 背景圖<figcaption>掌握表格嵌入圖片技巧,能讓資料表更豐富直觀。記住:語意化 HTML 搭配 CSS 控制外觀,才是現代前端的最佳實踐。