示例工程 25-responsive-images

25-responsive-images/app/index.html

html · 4.9 KB

运行:响应式图片与媒体

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>响应式图片与媒体</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <header class="site-header">
      <span class="brand">IMAGE LAB</span>
      <nav aria-label="页面导航">
        <a href="#srcset">srcset</a>
        <a href="#picture">picture</a>
        <a href="#fit">object-fit</a>
        <a href="#ratio">比例</a>
      </nav>
    </header>

    <main class="page">
      <section class="hero">
        <div class="hero-copy">
          <p class="eyebrow">LESSON / 25</p>
          <h1>让浏览器挑最合适的图片</h1>
          <p class="lead">
            srcset 提供候选,sizes 描述布局宽度;浏览器按真实需要下载,不浪费流量。
          </p>
        </div>
        <figure class="hero-figure">
          <img
            class="hero-img"
            src="assets/product-800.svg"
            srcset="
              assets/product-400.svg 400w,
              assets/product-800.svg 800w,
              assets/product-1200.svg 1200w
            "
            sizes="(min-width: 960px) 480px, 100vw"
            width="1200"
            height="900"
            alt="占位产品图:渐变背景上的白色卡片与尺寸标签"
          />
          <figcaption>2x 屏幕会选更大的文件:这里桌面加载 1200w,手机加载 800w;标签显示实际尺寸。</figcaption>
        </figure>
      </section>

      <section class="panel" id="srcset">
        <h2>1. srcset 与 sizes</h2>
        <p>
          <code>srcset</code> 列出候选与宽度,<code>sizes</code> 告诉浏览器图片在布局中占多宽。浏览器结合设备像素比选择。
        </p>
        <pre><code>&lt;img
  src="assets/product-800.svg"
  srcset="assets/product-400.svg 400w,
          assets/product-800.svg 800w,
          assets/product-1200.svg 1200w"
  sizes="(min-width: 960px) 480px, 100vw"
  width="1200" height="900"
  alt="占位产品图"&gt;</code></pre>
        <p class="hint">提示:width/height 提供宽高比,避免加载前后布局跳动。</p>
      </section>

      <section class="panel" id="picture">
        <h2>2. picture:按条件换图</h2>
        <p>宽屏用横幅,窄屏用方形;这是「美术指导」,不是单纯换分辨率。</p>
        <picture class="art">
          <source media="(min-width: 700px)" srcset="assets/wide.svg" />
          <img
            src="assets/square.svg"
            width="600"
            height="600"
            alt="占位横幅:紫色渐变背景与尺寸标签"
          />
        </picture>
        <p class="hint">窄屏加载 600×600 方形,宽屏加载 1200×500 横幅。</p>
      </section>

      <section class="panel" id="fit">
        <h2>3. object-fit</h2>
        <p>同一张 800×600 图片放进 240×160 的框:cover 裁切、contain 留白、fill 变形。</p>
        <div class="fit-grid">
          <figure class="fit-box">
            <img class="fit-cover" src="assets/product-800.svg" width="800" height="600" alt="cover 裁切示例" />
            <figcaption>cover</figcaption>
          </figure>
          <figure class="fit-box">
            <img class="fit-contain" src="assets/product-800.svg" width="800" height="600" alt="contain 留白示例" />
            <figcaption>contain</figcaption>
          </figure>
          <figure class="fit-box">
            <img class="fit-fill" src="assets/product-800.svg" width="800" height="600" alt="fill 变形示例" />
            <figcaption>fill</figcaption>
          </figure>
        </div>
      </section>

      <section class="panel" id="ratio">
        <h2>4. aspect-ratio 与画廊</h2>
        <p>不同比例的框,图片用 cover 填满;比例由容器决定,不会因图片尺寸跳动。</p>
        <div class="gallery">
          <figure class="shot ratio-43">
            <img src="assets/product-800.svg" width="800" height="600" loading="lazy" alt="4:3 裁切示例" />
          </figure>
          <figure class="shot ratio-11">
            <img src="assets/product-800.svg" width="800" height="600" loading="lazy" alt="1:1 裁切示例" />
          </figure>
          <figure class="shot ratio-169">
            <img src="assets/product-800.svg" width="800" height="600" loading="lazy" alt="16:9 裁切示例" />
          </figure>
        </div>
      </section>

      <section class="panel">
        <h2>5. 加载策略</h2>
        <ul class="tips">
          <li>首屏图片不要 lazy;首屏以下用 <code>loading="lazy"</code>。</li>
          <li>始终写 width/height 或 aspect-ratio,避免 CLS。</li>
          <li>用现代格式(AVIF/WebP)并提供回退。</li>
          <li>装饰图 <code>alt=""</code>;内容图写清用途。</li>
        </ul>
      </section>
    </main>

    <footer class="site-footer">示例数据,仅用于教程演示。</footer>
  </body>
</html>