我建议h1
整个使用。忘记h2
通过h6
。
回到HTML4,使用6个标题级别隐式定义了各个部分。例如,
<body>
<h1>This is a top-level heading</h1>
<p>some content here</p>
<h2>This is the heading of a subsection</h2>
<p>content in the subsection</p>
<h2>Another subsection begins here</h2>
<p>content</p>
<h1>another top-level heading</h1>
现在有了这个section
元素,您可以显式定义这些部分,而不必依靠浏览器读取不同标题级别创建的隐式部分。配备HTML5的浏览器知道,section
元素内的所有内容都会在文档大纲中被“降级”。因此,例如asection > h1
在语义上被视为an h2
,asection > section > h1
被视为an h3
,等等。
令人困惑的是,浏览器仍然基于h2
–h6
标题级别创建隐式部分,但是h2
–h6
元素不会改变其样式。这意味着h2
无论嵌套多少节,an都会看起来仍然像一个h2
(至少在Webkit中)。如果您的h2
标题是例如4级标题,这将令人困惑。
混合h2
-h6
与section
导致非常意外的结果。只需坚持h1
,并用于section
创建显式部分。
<body>
<header>
<h1>This is a top-level heading</h1>
<p>you may optionally wrap this p and the h1 above it inside a header element.
the header element doesn't affect the doc outline.
the section element does, however.</p>
</header>
<section>
<h1>even though this is an h1, the browser "treats it" like an h2
because it's inside an explicit section.
(it got demoted).</h1>
<p>content in the subsection</p>
</section>
<section>
<h1>Another subsection begins here, also treated like an h2</h1>
<p>content</p>
<h2>This is misleading. it is semantically treated like an h3.</h2>
<p>that is because after an h1, an h2 is demoted one level. the h1 above is
already a "level 2" heading, so this h2 becomes a "level 3" heading.</p>
<section>
<h1>just do this instead.</h1>
<p>it is treated like an h3 because it's in a section within a section.
(It got demoted twice.)</p>
</section>
</section>
<h1>another top-level heading</h1>
此外,你可以使用的<main>
元素。此元素仅包含特定于该页面的信息,并且不应包含在整个网站范围内重复的内容,例如导航链接,网站标头/页脚等。可能仅 <main>
存在一个元素<body>
。因此,您的解决方案可能像这样简单:
<header>
<h1>Site title</h1>
<nav>...</nav>
</header>
<main>
<h1>Page title</h1>
<p>page content</p>
</main>