萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 表格嵌入圖片完整教學:背景圖、前景圖、響應式圖片、懶加載與無障礙 alt 寫作

前景圖片(內容圖)

<table>
  <tr>
    <th>產品</th><th>圖片</th><th>價格</th>
  </tr>
  <tr>
    <td>筆記型電腦</td>
    <td>
      <img src="laptop.webp" alt="銀色 14 吋筆電,鍵盤背光" width="120" height="90" loading="lazy" decoding="async">
    </td>
    <td>NT$ 28,900</td>
  </tr>
</table>

背景圖片(裝飾性)

<style>
.hero-row { background: url('banner.jpg') center/cover no-repeat; min-height: 200px; }
.hero-row td { background: rgba(0,0,0,0.4); color: white; } /* 半透明疊加層 */
</style>
<tr class="hero-row">
  <td colspan="3">季末大促銷</td>
</tr>

響應式圖片:srcset + sizes

<img
  src="product-400.jpg"
  srcset="product-400.jpg 400w, product-800.jpg 800w, product-1200.jpg 1200w"
  sizes="(max-width: 480px) 100vw, (max-width: 960px) 50vw, 33vw"
  alt="產品照片"
  width="400" height="300"
  loading="lazy">

Picture 元素:美術導向(Art Direction)

<picture>
  <source media="(max-width: 600px)" srcset="product-mobile.webp" type="image/webp">
  <source media="(min-width: 601px)" srcset="product-desktop.webp" type="image/webp">
  <img src="product-fallback.jpg" alt="產品照片" width="400" height="300">
</picture>

無障礙 alt 文字寫作原則

圖片類型alt 內容範例
資訊性簡潔傳達資訊alt="紅色跑車,時速 0-100 3.2 秒"
裝飾性空字串 alt=""(隱藏於螢幕閱讀器)背景花紋、分隔線
功能性(連結/按鈕)描述功能而非外觀alt="下載 PDF 型錄"
複雜圖表簡短 alt + <figcaption> 或 longdescalt="Q1 銷售長條圖"

效能優化清單

  • 優先使用 WebP/AVIF 格式,配合 <picture> 降級。
  • 指定 width/height 預留空間,避免 CLS(累積版面位移)。
  • 啟用 loading="lazy"(視窗外圖片延遲載入)。
  • CDN 自動轉檔/壓縮(如 Cloudinary, Imgix, Cloudflare Images)。
copyright © 萬盛學電腦網 all rights reserved