示例工程 13-images

13-images/app/index.html

html · 5.7 KB

运行:图片与替代文本 · img / alt / 尺寸

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>图片与替代文本 · img / alt / 尺寸</title>
    <meta name="description" content="图片的 alt、宽高、懒加载与性能取舍。" />
    <style>
      * { box-sizing: border-box; }
      body {
        margin: 0;
        min-height: 100vh;
        padding: 32px 20px;
        background: #f8fafc;
        color: #0f172a;
        font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
        line-height: 1.75;
      }
      .page { width: min(100%, 780px); margin: 0 auto; }
      .site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
      .brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
      nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
      nav a + a { margin-left: 14px; }
      main { padding: 32px 0; }
      .eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
      h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); line-height: 1.15; }
      .lead { margin: 0 0 24px; color: #334155; font-size: 17px; }
      section { margin-top: 28px; padding-top: 20px; border-top: 1px solid #e2e8f0; }
      h2 { margin: 0 0 10px; font-size: 23px; }
      h3 { margin: 16px 0 6px; font-size: 16px; }
      p { margin: 0 0 12px; color: #334155; }
      ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
      li + li { margin-top: 4px; }
      img { max-width: 100%; height: auto; border-radius: 10px; }
      figure { margin: 14px 0; padding: 12px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      figcaption { margin-top: 8px; color: #64748b; font-size: 13px; }
      .icon-link { display: inline-flex; align-items: center; gap: 8px; color: #2563eb; }
      .icon-link img { width: 28px; height: 28px; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
      .note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
      footer { margin-top: 32px; padding-top: 16px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        .site-header { flex-direction: column; }
        nav a + a { margin-left: 12px; }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header class="site-header">
        <span class="brand">IMAGE NOTES</span>
        <nav aria-label="页内导航">
          <a href="#alt">替代文本</a>
          <a href="#size">尺寸</a>
          <a href="#loading">加载</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 13</p>
        <h1>图片</h1>
        <p class="lead">
          图片让页面有信息,也带来性能与可访问性问题。三个属性最关键:<code>src</code>、<code>alt</code>、<code>width</code>/<code>height</code>。
        </p>

        <section id="alt">
          <h2>1. 替代文本</h2>
          <p>信息型图片要写清它传达的内容,而不是「一张图片」。</p>
          <figure>
            <img
              src="assets/chart.svg"
              alt="四个季度访问量柱状图:Q1 到 Q4 逐季上升,Q4 最高"
              width="640"
              height="360"
              loading="lazy"
            />
            <figcaption>图 13-1:季度访问量,Q4 达到峰值。</figcaption>
          </figure>

          <h3>1.1 装饰图</h3>
          <p>只用于视觉分隔的图片,写空 <code>alt</code>,让读屏跳过。</p>
          <img src="assets/divider.svg" alt="" width="400" height="40" loading="lazy" />

          <h3>1.2 功能图</h3>
          <p>图片在链接里承担按钮作用时,<code>alt</code> 要描述动作。</p>
          <p>
            <a class="icon-link" href="assets/manual.txt" download="manual.txt">
              <img src="assets/download.svg" alt="下载用户手册" width="48" height="48" loading="lazy" />
              用户手册(TXT)
            </a>
          </p>
        </section>

        <section id="size">
          <h2>2. 宽高与布局</h2>
          <p>写上 <code>width</code> 与 <code>height</code>,浏览器可以提前预留空间,避免内容跳动。</p>
          <p>宽高是图片的固有比例,不是显示尺寸;显示尺寸交给 CSS。</p>
          <p class="note">漏写宽高,图片加载后页面会「跳一下」,这是最常见的布局偏移来源。</p>
        </section>

        <section id="loading">
          <h2>3. 加载策略</h2>
          <ul>
            <li>首屏主图:正常加载,可加 <code>fetchpriority="high"</code>。</li>
            <li>首屏以下的图:<code>loading="lazy"</code>。</li>
            <li>不要给首屏主图加懒加载,会推迟显示。</li>
            <li>图片格式优先 WebP / AVIF,保留回退。</li>
          </ul>
          <p>懒加载是性能优化,不是可访问性功能;读屏仍能读到未加载图片的 alt。</p>
        </section>

        <section id="rules">
          <h2>4. 检查表</h2>
          <ul>
            <li>每张 <code>img</code> 都有 <code>alt</code></li>
            <li>信息图 alt 具体,装饰图 alt 为空</li>
            <li>有 <code>width</code> 与 <code>height</code></li>
            <li>首屏图不懒加载,下方图懒加载</li>
            <li>图片有授权,压缩到合适体积</li>
          </ul>
        </section>
      </main>

      <footer>
        <p>示例图片为本地 SVG,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>