示例工程 03-elements-attributes

03-elements-attributes/app/index.html

html · 5.8 KB

运行:元素、属性与嵌套 · HTML 笔记

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>元素、属性与嵌套 · HTML 笔记</title>
    <meta name="description" content="标签、元素、属性、布尔属性与嵌套规则。" />
    <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%, 760px); 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 { color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
      h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 46px); line-height: 1.1; }
      .lead { margin: 0 0 20px; color: #334155; font-size: 17px; }
      section { margin-top: 24px; padding: 20px; border: 1px solid #e2e8f0; border-radius: 14px; background: #fff; }
      h2 { margin: 0 0 10px; font-size: 20px; }
      h3 { margin: 14px 0 6px; font-size: 15px; }
      p { margin: 0 0 10px; color: #334155; }
      ul, ol { margin: 8px 0 0; padding-left: 22px; color: #334155; }
      li + li { margin-top: 4px; }
      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; }
      table { width: 100%; margin-top: 10px; border-collapse: collapse; font-size: 13px; }
      th, td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; vertical-align: top; }
      th { background: #f1f5f9; font-weight: 600; }
      .note { padding: 12px 14px; border-left: 4px solid #f59e0b; border-radius: 6px; background: #fffbeb; color: #92400e; }
      footer { padding-top: 18px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        .site-header { flex-direction: column; }
        table { display: block; overflow-x: auto; }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header class="site-header">
        <span class="brand">HTML NOTES</span>
        <nav aria-label="页面导航">
          <a href="#anatomy">解剖</a>
          <a href="#attributes">属性</a>
          <a href="#nesting">嵌套</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 03</p>
        <h1>元素、属性与嵌套</h1>
        <p class="lead">
          标签只是语法符号;元素是标签 + 内容 + 属性;嵌套决定父子关系。浏览器与读屏都按这棵树工作。
        </p>

        <section id="anatomy">
          <h2>1. 一个元素的解剖</h2>
          <pre><code>&lt;a href="/about" title="关于我们"&gt;关于&lt;/a&gt;</code></pre>
          <ul>
            <li><code>a</code>:标签名,决定元素类型。</li>
            <li><code>href="/about"</code>:属性名与属性值。</li>
            <li><code>关于</code>:内容,用户看到并可点击。</li>
            <li><code>&lt;/a&gt;</code>:结束标签。</li>
          </ul>
        </section>

        <section id="attributes">
          <h2>2. 常见属性</h2>
          <table>
            <thead>
              <tr><th>属性</th><th>作用</th><th>例子</th></tr>
            </thead>
            <tbody>
              <tr><td><code>id</code></td><td>页面内唯一标识</td><td><code>id="main"</code></td></tr>
              <tr><td><code>class</code></td><td>可复用的分类</td><td><code>class="card"</code></td></tr>
              <tr><td><code>href</code></td><td>链接目标</td><td><code>href="/about"</code></td></tr>
              <tr><td><code>src</code></td><td>资源地址</td><td><code>src="a.png"</code></td></tr>
              <tr><td><code>alt</code></td><td>图片替代文本</td><td><code>alt="团队合影"</code></td></tr>
              <tr><td><code>data-*</code></td><td>自定义数据</td><td><code>data-id="42"</code></td></tr>
              <tr><td><code>hidden</code></td><td>隐藏元素</td><td><code>hidden</code></td></tr>
            </tbody>
          </table>
          <h3>布尔属性</h3>
          <p><code>disabled</code>、<code>required</code>、<code>checked</code> 这类属性,只写名字就表示真。</p>
          <pre><code>&lt;button type="button" disabled&gt;不可点&lt;/button&gt;</code></pre>
        </section>

        <section id="nesting">
          <h2>3. 嵌套规则</h2>
          <ul>
            <li><code>p</code> 里不能放块级元素,例如 <code>div</code>、<code>section</code>。</li>
            <li><code>a</code> 里不能再放 <code>a</code> 或按钮。</li>
            <li><code>ul</code> / <code>ol</code> 的直接子元素只能是 <code>li</code>。</li>
            <li><code>img</code>、<code>br</code>、<code>input</code> 是空元素,没有结束标签。</li>
          </ul>
          <p class="note">规则不是审美问题:错误的嵌套会被浏览器修正,读屏顺序也会跟着变。</p>
        </section>
      </main>

      <footer>
        <p>示例数据,仅用于教程演示。</p>
      </footer>
    </div>
  </body>
</html>