示例工程 03-elements-attributes
03-elements-attributes/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.75; }
h1 { margin: 0 0 16px; font-size: 28px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; max-width: 820px; }
.box { padding: 16px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
h2 { margin: 0 0 8px; font-size: 15px; }
code { background: #f1f5f9; padding: 1px 5px; border-radius: 4px; font-family: ui-monospace, monospace; font-size: 12px; }
p { margin: 0 0 8px; font-size: 14px; }
@media (max-width: 640px) { .grid { grid-template-columns: 1fr; } }
</style>
</head>
<body>
<h1>嵌套规则</h1>
<div class="grid">
<div class="box">
<h2>合法</h2>
<p>段落里只放行内元素,例如 <strong>强调</strong> 与 <a href="#target">链接</a>。</p>
<ul>
<li>列表项一</li>
<li>列表项二</li>
</ul>
</div>
<div class="box">
<h2>非法(浏览器会修正)</h2>
<p>段落里放块级元素:<div>这个 div 会被浏览器移出段落</div></p>
<ul>
<div>这个 div 不是 li</div>
<li>列表项</li>
</ul>
</div>
</div>
</body>
</html>