<h1>–<h6>:HTML 区域标题元素 - HTML(超文本标记语言) 编辑
HTML <h1>
–<h6>
标题(Heading)元素呈现了六个不同的级别的标题,<h1>
级别最高,而 <h6>
级别最低。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
内容类别 | 流式内容, 标题内容,可触知的内容。 |
---|---|
允许内容 | 短语内容 |
标签省略 | 不允许,开始标签和结束标签都不能省略。 |
允许的父元素 | 任何接受流内容的元素;不要把它作为 <hgroup> 元素的子元素, 这种做法已经被废弃了。 |
允许的 ARIA roles | tab , presentation |
DOM 接口 | HTMLHeadingElement |
属性
该元素包含所有全局特性。
align属性已废弃;不要继续使用它。
使用要点
- 用户代理可以使用标题信息,例如自动构建文档的目录。
- 不要为了减小标题的字体而使用低级别的标题, 而是使用 CSS
font-size
属性。 - 避免跳过某级标题:始终要从
<h1>
开始,接下来依次使用<h2>
等等。 - 使用
<section>
元素时,为了方便起见,你应该考虑避免在同一个页面上重复使用 <h1>,<h1> 应被用于表示页面的标题,其他的标题当从 <h2> 开始。在使用 section 时,应当为每个 section 都使用一个<h2>
。详情请参考 Defining sections in [Page not yet written]。
示例
所有标题
下面的代码展示了所有可用的标题级别。
<h1>一级标题</h1>
<h2>二级标题</h2>
<h3>三级标题</h3>
<h4>四级标题</h4>
<h5>五级标题</h5>
<h6>六级标题</h6>
下面是这些代码的结果:
示例页面
下面的代码展示了几个具有部分内容的标题。
<h1>Heading elements</h1>
<h2>Summary</h2>
<p>Some text here...</p>
<h2>Examples</h2>
<h3>Example 1</h3>
<p>Some text here...</p>
<h3>Example 2</h3>
<p>Some text here...</p>
<h2>See also</h2>
<p>Some text here...</p>
下面是代码的运行结果:
可访问性问题
导航
A common navigation technique for users of screen reading software is jumping from heading to heading to quickly determine the content of the page. Because of this, it is important to not skip one or more heading levels. Doing so may create confusion, as the person navigating this way may be left wondering where the missing heading is.
错误用法
<h1>Heading level 1</h1>
<h3>Heading level 3</h3>
<h4>Heading level 4</h4>
正确用法
<h1>Heading level 1</h1>
<h2>Heading level 2</h2>
<h3>Heading level 3</h3>
嵌套
Headings may be nested as subsections to reflect the organization of the content of the page. Most screen readers can also generate an ordered list of all the headings on a page, which can help a person quickly determine the hierarchy of the content:
h1
Beetlesh2
Etymologyh2
Distribution and Diversityh2
Evolutionh3
Late Paleozoich3
Jurassich3
Cretaceoush3
Cenozoic
h2
External Morphologyh3
Headh4
Mouthparts
h3
Thoraxh4
Prothoraxh4
Pterothorax
h3
Legsh3
Wingsh3
Abdomen
When headings are nested, heading levels may be "skipped" when closing a subsection.
- Headings • Page Structure • WAI Web Accessibility Tutorials
- MDN Understanding WCAG, Guideline 1.3 explanations
- Understanding Success Criterion 1.3.1 | W3C Understanding WCAG 2.0
- MDN Understanding WCAG, Guideline 2.4 explanations
- Understanding Success Criterion 2.4.1 | W3C Understanding WCAG 2.0
- Understanding Success Criterion 2.4.6 | W3C Understanding WCAG 2.0
- Understanding Success Criterion 2.4.10 | W3C Understanding WCAG 2.0
Labeling section content
Another common navigation technique for users of screen reading software is to generate a list of sectioning content and use it to determine the page's layout.
Sectioning content can be labeled using a combination of the aria-labelledby
and id
attributes, with the label concisely describing the purpose of the section. This technique is useful for situations where there is more than one sectioning element on the same page.
示例
<header>
<nav aria-labelledby="primary-navigation">
<h2 id="primary-navigation">Primary navigation</h2>
<!-- navigation items -->
</nav>
</header>
<!-- page content -->
<footer>
<nav aria-labelledby="footer-navigation">
<h2 id="footer-navigation">Footer navigation</h2>
<!-- navigation items -->
</nav>
</footer>
In this example, screen reading technology would announce that there are two <nav>
sections, one called "Primary navigation" and one called "Footer navigation". If labels were not provided, the person using screen reading software may have to investigate each nav
element's contents to determine their purpose.
- Using the aria-labelledby attribute
- Labeling Regions • Page Structure • W3C WAI Web Accessibility Tutorials
规范
规范 | 状态 | 注释 |
---|---|---|
HTML Living Standard <h1>, <h2>, <h3>, <h4>, <h5>, and <h6> | Living Standard | |
HTML5 <h1>, <h2>, <h3>, <h4>, <h5>, and <h6> | Recommendation | |
HTML 4.01 Specification <h1>, <h2>, <h3>, <h4>, <h5>, and <h6> | Recommendation |
浏览器兼容性
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.参见
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论