【转】HTML5标签使用的常见误区
最近组内进行HTML5标签的学习,方法呢就是大家每人挑选几个标签,自己先去学习,然后给大家作讲解.这个过程大家还是挺有收获的.但是现在HTML5还处在草案阶段,有些新的标签元素的解释也是经常有变化,甚至标签加入移出也很频繁(比如 hgroup),同时现有的大的门户网站在使用HTML5方面也没有很好的范例可以参考,让大家的学习过程更摸索.下面是我在 html5doctor 上面看到的一篇文章,在目前大家懵懂的阶段,可能看看大师的讲解会更容易理解。由于才疏学浅,很多不明白的地方可能只是做了字面上的翻译,不对的地方还请大家多多指教。
下面附上原文地址:Avoiding common HTML5 mistakes 作者 :Richard Clark,有疑问的地方大家可以核对英文。
在这篇文章中,我将给大家分享html5构建页面的小错误和不好的实践方法,让我们在以后的工作中避免这些错误。
不要把
当成简单的容器来定义样式
我们经常看到的一个错误,就是武断的将
- <div id="wrapper">
- <div id="header">
- <h1>My super duper page h1>
- div>
- <div id="main">
- div>
- <div id="secondary">
- div>
- <div id="footer">
- div>
- div>
现在我看到了下面的代码样子:
- <section id="wrapper">
- <header>
- <h1>My super duper page h1>
- header>
- <section id="main">
- section>
- <section id="secondary">
- section>
- <footer>
- footer>
- section>
直观的看,上面的例子是错误的:
- <body>
- <header>
- <h1>My super duper page h1>
- header>
- <div role="main">
- div>
- <aside role="complementary">
- aside>
- <footer>
- footer>
- body>
如果你还是无法确定哪一个元素更适合使用,我建议你去查看HTML5 sectioning content element flowchart来让你继续前行。
-
元素通常是通常作为一组解释或者导航辅助工具,通常包含section的标题. -
元素会将当有副标题子标题,各类标识文字时,对
到
标题进行群组,将其作为section的标题.
- <article>
- <header>
- <h1>My best blog post h1>
- header>
- article>
如果你的
- <article>
- <h1>My best blog post h1>
- article>
- 这里只有一个标题,
- 或者 本身就够了(比如:不需要 )
- <header>
- <hgroup>
- <h1>My best blog post h1>
- hgroup>
- <p>by Rich Clark p>
- header>
这种情况下,将
移除,只保留标题就好.
- <header>
- <h1>My best blog post h1>
- <p>by Rich Clark p>
- header>
- <header>
- <hgroup>
- <h1>My company h1>
- <h2>Established 1893 h2>
- hgroup>
- header>
- <hgroup>
- <h1>My company h1>
- <h2>Established 1893 h2>
- hgroup>
The nav element represents a section of a page that links to other pages or to parts within the page: a section with navigation links.Note: Not all groups of links on a page need to be in a nav element — the element is primarily intended for sections that consist of major navigation blocks. In particular, it is common for footers to have a short list of links to various pages of a site, such as the terms of service, the home page, and a copyright page. The footer element alone is sufficient for such cases; while a nav element can be used in such cases, it is usually unnecessary.
WHATWG HTML spec
- 主要导航
- 网站搜索
- 二级导航(这个能是有争议的)
- 页面内链接(比如一篇很长的文章)
- 翻页
- 社交类的链接(虽然有些社交类的链接也是主要的链接,比如关于我About me和品味Flavours )
- 博客文章的标签
- 博客文章的分类列表
- 第三级导航
- 大页脚
- 如果用
和标题标签能够解决你的问题,那就不要去使用 - 你是不是为了增加可访问性而增加的一个快捷跳转链接呢?
在规范中关于
如果仅仅是一张表现类的图片而且和文档中其他的内容没有关系的话,那就不需要使用
- <header>
- <h1>
- <figure>
- <img src="/img/mylogo.png" alt="My company" class="hide" />
- figure>
- My company name
- h1>
- header>
- <header>
- <figure>
- <img src="/img/mylogo.png" alt="My company" />
- figure>
- header>
这里就不需要说啥了,这是很明显的错误,可能你认为我们说的是不是将logo放在H1标签里面,但是我们在这里并不讨论这个问题。让我们迷惑的是
- <header>
- <h1>My company name h1>
- header>
在HTML5中,我们并不需要给