How to properly use h1 in HTML5
以下哪一项是构建页面的正确方法:
1)
1 2 3 4 5 6 7 | <header> Site title <nav>...</nav> </header> <section> Page title </section> |
如果我只在
2)
1 2 3 4 5 6 7 | <header> Site title <nav>...</nav> </header> <section> Page title </section> |
在
3)
1 2 3 4 5 6 7 8 9 | <header> <p> Site title </p> <nav>...</nav> </header> <section> Page title </section> |
最后,w3似乎建议在主
Sections may contain headings of any rank, but authors are strongly
encouraged to either use only h1 elements, or to use elements of the
appropriate rank for the section's nesting level.
正如我在我的评论中所说,您在W3C中引用的那样,h1是一个标题,而不是标题。每个分段元素可以有自己的标题元素。你不能认为h1只是一个页面的标题,而是页面特定部分的标题。就像报纸的头版一样,每一篇文章都有自己的标题。
这是一篇很好的文章。
我建议在整个过程中使用
回到HTML4中,6个标题级别被用于隐式定义节。例如,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <body> This is a top-level heading <p> some content here </p> This is the heading of a subsection <p> content in the subsection </p> Another subsection begins here <p> content </p> another top-level heading |
现在,使用
令人困惑的是,浏览器仍然基于
将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <body> <!-- optional --><header> This is a top-level heading <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> <!-- optional --></header> <section> even though this is an h1, the browser"treats it" like an h2 because it's inside an explicit section. (it got demoted). <p> content in the subsection </p> </section> <section> Another subsection begins here, also treated like an h2 <p> content </p> This is misleading. it is semantically treated like an h3. <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> just do this instead. <p> it is treated like an h3 because it's in a section within a section. (It got demoted twice.) </p> </section> </section> another top-level heading |
此外,您可以使用
1 2 3 4 5 6 7 8 9 10 | <header> Site title <nav>...</nav> </header> <main> Page title <p> page content </p> </main> |
但是,不要忘记可访问性问题。根据MDN的说法,"目前图形浏览器或辅助技术用户代理中没有已知的轮廓算法实现",这意味着屏幕阅读器可能无法找出仅使用