示例工程 14-figure-picture
14-figure-picture/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>figure 与 picture · 图注与响应式图片</title>
<meta name="description" content="用 figure 组织图注,用 picture 做艺术方向与格式回退。" />
<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; }
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: 16px 0; padding: 12px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
figcaption { margin-top: 8px; color: #64748b; font-size: 13px; }
blockquote { margin: 0; padding: 8px 14px; border-left: 3px solid #2563eb; color: #1e3a8a; }
blockquote p { margin: 0; color: inherit; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
pre { margin: 10px 0 0; padding: 14px; overflow-x: auto; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-size: 12px; line-height: 1.7; }
pre code { padding: 0; background: none; color: inherit; }
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">MEDIA NOTES</span>
<nav aria-label="页内导航">
<a href="#figure">figure</a>
<a href="#picture">picture</a>
<a href="#rules">规则</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 14</p>
<h1>figure 与 picture</h1>
<p class="lead">
<code>figure</code> 把内容和说明绑在一起;<code>picture</code> 让同一位置按屏幕或格式换图。
</p>
<section id="figure">
<h2>1. figure 与图注</h2>
<p>图片、图表、代码、引用都可以是 figure。</p>
<figure>
<img
src="assets/chart.svg"
alt="四个季度访问量柱状图,逐季上升"
width="640"
height="360"
loading="lazy"
/>
<figcaption>图 14-1:2026 年季度访问量,Q4 最高。</figcaption>
</figure>
<p>引用也可以带图注:</p>
<figure>
<blockquote cite="source.html">
<p>结构正确,内容清楚,可访问,才算完成。</p>
</blockquote>
<figcaption>—— <cite>林小满</cite>,访谈原文</figcaption>
</figure>
</section>
<section id="picture">
<h2>2. picture 与艺术方向</h2>
<p>窄屏换一张更适合竖向阅读的图:</p>
<picture>
<source media="(max-width: 600px)" srcset="assets/narrow.svg" />
<img
src="assets/wide.svg"
alt="响应式布局示意:宽屏横向排列,窄屏纵向排列"
width="800"
height="400"
loading="lazy"
/>
</picture>
<p>按像素密度选择不同尺寸,用 <code>srcset</code> 与 <code>sizes</code>:</p>
<pre><code><img
src="chart-640.svg"
srcset="chart-320.svg 320w, chart-640.svg 640w, chart-960.svg 960w"
sizes="(max-width: 700px) 100vw, 640px"
alt="季度访问量柱状图"
width="640" height="360"></code></pre>
</section>
<section id="rules">
<h2>3. 规则</h2>
<ul>
<li><code>figure</code> 里只有一个 <code>figcaption</code></li>
<li>图注可放开头或结尾,通常放结尾</li>
<li><code>picture</code> 里必须有 <code>img</code> 作为回退</li>
<li><code>alt</code> 写在 <code>img</code> 上,不写在 <code>source</code> 上</li>
<li>艺术方向用 <code>media</code>,分辨率切换用 <code>srcset</code></li>
</ul>
</section>
</main>
<footer>
<p>示例图片为本地 SVG,仅用于教学。</p>
</footer>
</div>
</body>
</html>