示例工程 01-what-is-html
01-what-is-html/demo-02-nesting/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>
<style>
body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.panel { padding: 14px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
.box { padding: 8px; border: 2px dashed #2563eb; border-radius: 6px; }
code { background: #f1f5f9; padding: 1px 5px; border-radius: 4px; }
@media (max-width: 640px) { .grid { grid-template-columns: 1fr; } }
</style>
</head>
<body>
<h1>嵌套与浏览器纠错</h1>
<p>段落只能包含行内内容,不能包含块级元素。</p>
<div class="grid">
<section class="panel">
<h2>错误嵌套</h2>
<p>下面这段源码把 div 放进了 p:</p>
<pre><code><p>文字<div class="box">块</div></p></code></pre>
<p>文字<div class="box">块</div></p>
</section>
<section class="panel">
<h2>正确嵌套</h2>
<p>块级元素放在段落外面:</p>
<pre><code><p>文字</p><div class="box">块</div></code></pre>
<p>文字</p>
<div class="box">块</div>
</section>
</div>
</body>
</html>