示例工程 10-sizing-overflow
10-sizing-overflow/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>尺寸与溢出实验室</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header class="site-header">
<span class="brand">SIZE LAB</span>
<nav aria-label="页面导航">
<a href="#sizing">尺寸</a>
<a href="#ratio">比例</a>
<a href="#overflow">溢出</a>
</nav>
</header>
<main class="page">
<p class="eyebrow">LESSON / 10</p>
<h1>尺寸与溢出实验室</h1>
<p class="lead">
宽度不是越精确越好,而是要有上下界。内容也不是总能塞进盒子;
溢出的处理方式,决定了页面是可用还是失控。
</p>
<section class="panel" id="sizing">
<h2>1. min、max 与 clamp</h2>
<p>下面这个盒子宽度是 <code>min(100%, 560px)</code>,不会超过 560px,也不会溢出父级。</p>
<div class="sized-box">width: min(100%, 560px)</div>
<p class="hint">把窗口拉窄,盒子跟着变窄;拉宽,最多 560px。</p>
</section>
<section class="panel" id="ratio">
<h2>2. aspect-ratio</h2>
<p>用 <code>aspect-ratio</code> 保持 16:9,宽度变化时高度自动跟随。</p>
<div class="ratio-card">
<span>16 : 9</span>
</div>
</section>
<section class="panel" id="overflow">
<h2>3. overflow</h2>
<p>同一个长内容容器,四种 overflow 值。</p>
<div class="overflow-grid">
<div class="overflow-demo visible"><strong>visible</strong><p>这是一段很长很长很长的内容,会直接溢出容器边界。</p></div>
<div class="overflow-demo hidden"><strong>hidden</strong><p>这是一段很长很长很长的内容,超出部分被裁掉。</p></div>
<div class="overflow-demo auto"><strong>auto</strong><p>这是一段很长很长很长的内容,需要时出现滚动条。</p></div>
<div class="overflow-demo scroll"><strong>scroll</strong><p>这是一段很长很长很长的内容,总是显示滚动条。</p></div>
</div>
</section>
<section class="panel">
<h2>4. 文本截断</h2>
<p class="truncate">这是一段很长的单行文本,超出后显示省略号,不会换行。</p>
<p class="clamp-text">这是一段较长的多行文本,用 line-clamp 限制为三行,超出部分显示省略号。它适合卡片摘要,但不要隐藏关键信息。</p>
</section>
</main>
<footer class="site-footer">示例数据,仅用于教程演示。</footer>
</body>
</html>