提到HTML5,不能不提到HTML,这里对HTML简讲一下。
HTML:一种超文本标记语言,是一种标识性的语言,包括一系列的标签,通过这些标签可以将网络上的文档格式统一,使分散的Internet连接为一个逻辑整体。HTML5技术结合了HTML4.01的相关标准并革新,符合现代网络发展要求,于2008年正式发布,并在2012年已经形成了稳定的版本。
HTML5新特性
HTML5中的一些有趣的新特性:
· 用于绘画的canvas元素
· 用于媒介回放的video和audio元素
· 对本地离线存储的更好支持
· 新的特殊内容元素,比如article、footer、header、nav、section
· 新的表单控件,比如calendar、date、time、email、url、search
<!doctype> 声明必须位于 HTML5 文档中的第一行
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
文档内容......
</body>
标签 | 描述 |
<canvas> | |
<canvas> | 标签定义图形,比如图表和其他图像。该标签基于 JavaScript 的绘图 API |
新多媒体元素 | |
<audio> | 定义音频内容 |
<video> | 定义视频(video 或者 movie) |
<source> | 定义多媒体资源 和 |
<embed> | 定义嵌入的内容,比如插件。 |
<track> | 为诸如 和 元素之类的媒介规定外部文本轨道。 |
新表单元素 | |
<datalist> | 定义选项列表。请与 input 元素配合使用该元素,来定义 input 可能的值。 |
<keygen> | 规定用于表单的密钥对生成器字段。 |
<output> | 定义不同类型的输出,比如脚本的输出。 |
新的语义和结构元素 | |
<article> | 定义页面独立的内容区域。 |
<aside> | 定义页面的侧边栏内容。 |
<bdi> | 允许您设置一段文本,使其脱离其父元素的文本方向设置。 |
<command> | 定义命令按钮,比如单选按钮、复选框或按钮 |
<details> | 用于描述文档或文档某个部分的细节 |
<dialog> | 定义对话框,比如提示框 |
<summary> | 标签包含 details 元素的标题 |
<figure> | 规定独立的流内容(图像、图表、照片、代码等等)。 |
<figcaption> | 定义 <figure> 元素的标题 |
<footer> | 定义 section 或 document 的页脚。 |
<header> | 定义了文档的头部区域 |
<mark> | 定义带有记号的文本。 |
<meter> | 定义度量衡。仅用于已知最大和最小值的度量。 |
<nav> | 定义导航链接的部分。 |
<progress> | 定义任何类型的任务的进度。 |
<ruby> | 定义 ruby 注释(中文注音或字符)。 |
<rt> | 定义字符(中文注音或字符)的解释或发音。 |
<rp> | 在 ruby 注释中使用,定义不支持 ruby 元素的浏览器所显示的内容。 |
<section> | 定义文档中的节(section、区段)。 |
<time> | 定义日期或时间。 |
<wbr> | 规定在文本中的何处适合添加换行符。 |
浏览器支持
所有现代浏览器都支持HTML5。
此外,所有浏览器,无论新旧,都会自动把未识别元素当做行内元素来处理。正因为如此,可以帮助老式浏览器处理“未知的”HTML元素。
把HTML5元素定义为块级元素
HTML5定义了八个新的语义化HTML标签,这八个标签都是块级元素。
为确保老式浏览器中正确的行为,可以把CSS display属性设置为block,
实例:
header,section,footer,aside,nav,main,article,figure{
display:block;
}
向HTML添加新元素
通过浏览器trick向HTML添加任何元素:
本例向HTML添加了一个名为的新元素,并为其定义display样式
实例:
<!DOCTYPE html>
<html>
<head>
<title>Creating an HTML Element</title>
<script>document.createElement("myHero")</script>
<style>
myHero {
display: block;
background-color: #ddd;
padding: 50px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<myHero>My First Hero</myHero>
</body>
</html>
Internet Explorer 的问题
上述方案可以用于所有新的HTML5元素,但是:
注意: Internet Explorer 8 以及更早的版本,不允许对未知元素添加样式。
幸运的是,Sjoerd Visscher 创造了 “HTML5 Enabling JavaScript”, “the shiv”:
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
以上代码是一段注释,但是 IE9 的早期版本会读取它(并理解它)。
完整的 Shiv 解决方案
实例
<!DOCTYPE html>
<html>
<head>
<title>Styling HTML5</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<h1>My First Article</h1>
<article>
London is the capital city of England.
It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</article>
</body>
</html>
HTML5 Skeleton
实例
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Skeleton</title>
<meta charset="utf-8">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<style>
body {font-family: Verdana, sans-serif; font-size:0.8em;}
header,nav, section,article,footer
{border:1px solid grey; margin:5px; padding:8px;}
nav ul {margin:0; padding:0;}
nav ul li {display:inline; margin:5px;}
</style>
</head>
<body>
<header>
<h1>HTML5 SKeleton</h1>
</header>
<nav>
<ul>
<li><a href="html5_semantic_elements.asp">HTML5 Semantic</a></li>
<li><a href="html5_geolocation.asp">HTML5 Geolocation</a></li>
<li><a href="html5_canvas.asp">HTML5 Graphics</a></li>
</ul>
</nav>
<section>
<h1>Famous Cities</h1>
<article>
<h2>London</h2>
<p>London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
</article>
<article>
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</article>
<article>
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</article>
</section>
<footer>
<p>© 2014 W3Schools. All rights reserved.</p>
</footer>
</body>
</html>
HTML5其他特性各浏览器支持统计
HTML5 Web应用程序
HTML5 图形和内嵌内容
HTML5 音频编码
HTML5 视频编码
HTML5 表单对象
HTML5 表单属性