文档分节
1、添加基本的标题
h1 元素表示标题。HTML 定义了一套标题元素体系,从 h1 一直到 h6,h1 级别最高。
元素 | h1~h6 |
---|---|
元素类型 | 流 |
允许具备的父元素 | hgroup 元素或其他任何可以包含流元素的元素。这些元素不能说 address 元素的后代元素 |
局部属性 | 无 |
内容 | 流内容 |
标签用法 | 开始标签和结束标签 |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
I like apples and oranges.
I also like bananas, mangoes, cherries, apricots, plums, peaches and grapes.
You can see other fruits I like <a href="fruitlist.html">here</a>.
<strong>Warning:</strong> Eating too many oranges can give you heart burn.
My favorite kind of orange is the mandarin, properly known
as <i>citrus reticulata</i>.
Oranges at my local store cost <s>$1 each</s> $2 for 3.
The <abbr title="Florida Department of Citrus">FDOC</abbr> regulates the Florida
citrus industry.
I still remember the best apple I ever tasted.
I bought it at <time datetime="15:00">3 o'clock</time>
on <time datetime="1984-12-7">December 7th</time>.
</body>
</html>
2、hgroup 元素
hgroup 元素可用来将几个标题元素作为一个整体处理,以免搅乱 HTML 文档的大纲。
元素 | hgroup |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含流元素的元素 |
局部属性 | 无 |
内容 | 一个或多个标题元素(h1~h6) |
标签用法 | 开始标签和结束标签 |
习惯样式 | hgroup { display: block; } |
hgroup 主要用来解决子标题的问题。假设文档中有一段内容,其标题为 “Fruits I Like”,还有一个子标题 “How I Learned to Love Citrus”。为此需要用到 h1 到 h2 元素。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<h1>Fruits I Like</h1>
<h2>How I Learned to Love Citrus</h2>
I like apples and oranges.
<h2>Additional fruits</h2>
I also like bananas, mangoes, cherries, apricots, plums, peaches and grapes.
<h3>More information</h3>
You can see other fruits I like <a href="fruitlist.html">here</a>.
<h1>Activities I Like</h1>
<p>I like to swim, cycle and run. I am in training for my first triathlon,
but it is hard work.</p>
<h2>Kinds of Triathlon</h2>
There are different kinds of triathlon - sprint, Olympic and so on.
<h3>The kind of triathlon I am aiming for</h3>
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</body>
</html>
这里遇到的问题是无法区分表示子标题的 h2 元素和表示下一级标题的 h2 元素。类似于如下的大纲:
Fruits I Like
How I Learned to Love Citrus
Additional fruits
More information
Activities I Like
Kinds of Triathlon
The kind of triathlon I am aiming for
使用 hgroup:
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1, h2, h3 { background: grey; color: white; }
hgroup > h1 { margin-bottom: 0px;}
hgroup > h2 { background: grey; color: white; font-size: 1em;
margin-top: 0px; }
</style>
</head>
<body>
<hgroup>
<h1>Fruits I Like</h1>
<h2>How I Learned to Love Citrus</h2>
</hgroup>
I like apples and oranges.
<h2>Additional fruits</h2>
I also like bananas, mangoes, cherries, apricots, plums, peaches and grapes.
<h3>More information</h3>
You can see other fruits I like <a href="fruitlist.html">here</a>.
<h1>Activities I like</h1>
<p>I like to swim, cycle and run. I am in training for my first triathlon,
but it is hard work.</p>
<h2>Kinds of Triathlon</h2>
There are different kinds of triathlon - sprint, Olympic and so on.
<h3>The kind of triathlon I am aiming for</h3>
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</body>
</html>
大纲如下:
Fruits I Like
Additional fruits
More information
Activities I Like
Kinds of Triathlon
The kind of triathlon I am aiming for
3、生成节
section 是 HTML5 中新增的。它表示的是文档中的一节。使用标题元素的时候实际上也生成了隐含的节。用 section 元素则可以明确地生成节并且将其与标题分开。section 元素用来包含的是那种应该列入文档大纲或目录中的内容。section 元素通常包含一个或多个段落及一个标题,不过标题并不是必须的。
元素 | section |
---|---|
元素类型 | 流 |
允许具有的父元素 | 任何可以包含流元素的元素,但 section 元素不能是 address 元素的后代元素 |
局部属性 | 无 |
内容 | style 元素和流元素 |
标签用法 | 开始标签和结束标签 |
习惯样式 | section { display: block; } |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1, h2, h3 { background: grey; color: white; }
hgroup > h1 { margin-bottom: 0px; }
hgroup > h2 { background: grey; color: white; font-size: 1em;
margin-top: 0px;}
</style>
</head>
<body>
<section>
<hgroup>
<h1>Fruits I Like</h1>
<h2>How I Learned to Love Citrus</h2>
</hgroup>
I like apples and oranges.
<section>
<h1>Additional fruits</h1>
I also like bananas, mangoes, cherries, apricots, plums,
peaches and grapes.
<section>
<h1>More information</h1>
You can see other fruits I like <a href="fruitlist.html">here</a>.
</section>
</section>
</section>
<h1>Activities I like</h1>
<p>I like to swim, cycle and run. I am in training for my first triathlon,
but it is hard work.</p>
<h2>Kinds of Triathlon</h2>
There are different kinds of triathlon - sprint, Olympic and so on.
<h3>The kind of triathlon I am aiming for</h3>
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</body>
</html>
4、添加首部和尾部
header 元素表示一节的首部。里边可以包含各种适合出现在首部的东西,包括刊头或徽标。在内嵌的元素方面,header 元素通常包含一个标题元素或一个 hgroup 元素,还可以包含该节的导航元素。
元素 | header |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含流元素的元素。header 元素不能是 address、footer 元素和其他 header 元素的后代元素 |
局部属性 | 无 |
内容 | 流内容 |
标签用法 | 开始标签和结束标签 |
习惯样式 | header { display: block; } |
footer 元素是 header 元素的配套元素,表示一节的尾部。footer 通常包含着该节的总结信息,还可以包含作者介绍、版权信息、到相关内容的连接、徽标及免责声明等。
元素 | footer |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含流元素的元素。footer 元素不能是 address、footer 元素和其他 header 元素的后代元素 |
局部属性 | 无 |
内容 | 流内容 |
标签用法 | 开始标签和结束标签 |
习惯样式 | footer { display: block; } |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1, h2, h3 { background: grey; color: white; }
hgroup > h1 { margin-bottom: 0; margin-top: 0}
hgroup > h2 { background: grey; color: white; font-size: 1em;
margin-top: 0px; margin-bottom: 2px}
body > header *, footer > * { background:transparent; color:black;}
body > section, body > section > section,
body > section > section > section {margin-left: 10px;}
body > header, body > footer {
border: medium solid black; padding-left: 5px; margin: 10px 0 10px 0;
}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Things I like</h1>
<h2>by Adam Freeman</h2>
</hgroup>
</header>
<section>
<header>
<hgroup>
<h1>Fruits I Like</h1>
<h2>How I Learned to Love Citrus</h2>
</hgroup>
</header>
I like apples and oranges.
<section>
<h1>Additional fruits</h1>
I also like bananas, mangoes, cherries, apricots, plums,
peaches and grapes.
<section>
<h1>More information</h1>
You can see other fruits I like <a href="fruitlist.html">here</a>.
</section>
</section>
</section>
<section>
<header>
<h1>Activities I like</h1>
</header>
<section>
<p>I like to swim, cycle and run. I am in training for my first
triathlon, but it is hard work.</p>
<h1>Kinds of Triathlon</h1>
There are different kinds of triathlon - sprint, Olympic and so on.
<section>
<h1>The kind of triathlon I am aiming for</h1>
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</section>
</section>
</section>
<footer id="mainFooter">
©2011, Adam Freeman. <a href="http://apress.com">Visit Apress</a>
</footer>
</body>
</html>
5、添加导航区域
nav 元素表示文档中的一个区域,它包含着到其他页面或同一页面的其他部分的链接。该元素的目的是规划出文档的主要导航区域。
元素 | nav |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含流元素的元素。但是该元素不能是 address 元素的后代元素 |
局部属性 | 无 |
内容 | 流内容 |
标签用法 | 开始标签和结束标签 |
习惯样式 | nav { display: block; } |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1, h2, h3 { background: grey; color: white; }
hgroup > h1 { margin-bottom: 0; margin-top: 0}
hgroup > h2 { background: grey; color: white; font-size: 1em;
margin-top: 0px; margin-bottom: 2px}
body > header *, body > footer * { background:transparent; color:black;}
body > section, body > section > section,
body > section > section > section {margin-left: 10px;}
body > header, body > footer {
border: medium solid black; padding-left: 5px; margin: 10px 0 10px 0;
}
body > nav { text-align: center; padding: 2px; border : dashed thin black;}
body > nav > a {padding: 2px; color: black}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Things I like</h1>
<h2>by Adam Freeman</h2>
</hgroup>
<nav>
<h1>Contents</h1>
<ul>
<li><a href="#fruitsilike">Fruits I Like</a></li>
<ul>
<li><a href="#morefruit">Additional Fruits</a></li>
</ul>
<li><a href="#activitiesilike">Activities I Like</a></li>
<ul>
<li><a href="#tritypes">Kinds of Triathlon</a></li>
<li><a href="#mytri">The kind of triathlon I am
aiming for</a></li>
</ul>
</ul>
</nav>
</header>
<section>
<header>
<hgroup>
<h1 id="fruitsilike">Fruits I Like</h1>
<h2>How I Learned to Love Citrus</h2>
</hgroup>
</header>
I like apples and oranges.
<section>
<h1 id="morefruit">Additional fruits</h1>
I also like bananas, mangoes, cherries, apricots, plums,
peaches and grapes.
<section>
<h1>More information</h1>
You can see other fruits I like <a href="fruitlist.html">here</a>.
</section>
</section>
</section>
<section>
<header>
<h1 id="activitiesilike">Activities I like</h1>
</header>
<section>
<p>I like to swim, cycle and run. I am in training for my first
triathlon, but it is hard work.</p>
<h1 id="tritypes">Kinds of Triathlon</h1>
There are different kinds of triathlon - sprint, Olympic and so on.
<section>
<h1 id="mytri">The kind of triathlon I am aiming for</h1>
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</section>
</section>
</section>
<nav>
More Information:
<a href="http://fruit.org">Learn More About Fruit</a>
<a href="http://triathlon.org">Learn More About Triathlons</a>
</nav>
<footer id="mainFooter">
©2011, Adam Freeman. <a href="http://apress.com">Visit Apress</a>
</footer>
</body>
</html>
6、使用 article
article 元素代表 HTML 文档中一段独立成篇的内容,从理论上讲,可以独立于页面其余内容发布或使用(例如通过 RSS)。这不是说作者必须单独发布它,而是说判断是否使用该元素时要以独立性为依据。一篇新文章和博文条目都是这方面的典型例子。
元素 | article |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含的流元素的元素,但该元素不能是 address 元素的后代元素 |
局部属性 | 无 |
内容 | style 元素和流内容 |
标签用法 | 开始标签和结束标签 |
习惯样式 | article { display: block; } |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1, h2, h3, article > footer { background: grey; color: white; }
hgroup > h1 { margin-bottom: 0; margin-top: 0}
hgroup > h2 { background: grey; color: white; font-size: 1em;
margin-top: 0px; margin-bottom: 2px}
body > header *, body > footer * { background:transparent; color:black;}
article {border: thin black solid; padding: 10px; margin-bottom: 5px}
article > footer {padding:5px; margin: 5px; text-align: center}
article > footer > nav > a {color: white}
body > article > section,
body > article > section > section {margin-left: 10px;}
body > header, body > footer {
border: medium solid black; padding-left: 5px; margin: 10px 0 10px 0;
}
body > nav { text-align: center; padding: 2px; border : dashed thin black;}
body > nav > a {padding: 2px; color: black}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Things I like</h1>
<h2>by Adam Freeman</h2>
</hgroup>
<nav>
<h1>Contents</h1>
<ul>
<li><a href="#fruitsilike">Fruits I Like</a></li>
<li><a href="#activitiesilike">Activities I Like</a></li>
</ul>
</nav>
</header>
<article>
<header>
<hgroup>
<h1 id="fruitsilike">Fruits I Like</h1>
<h2>How I Learned to Love Citrus</h2>
</hgroup>
</header>
I like apples and oranges.
<section>
<h1 id="morefruit">Additional fruits</h1>
I also like bananas, mangoes, cherries, apricots, plums,
peaches and grapes.
<section>
<h1>More information</h1>
You can see other fruits I like <a href="fruitlist.html">here</a>
</section>
</section>
<footer>
<nav>
More Information:
<a href="http://fruit.org">Learn More About Fruit</a>
</nav>
</footer>
</article>
<article>
<header>
<hgroup>
<h1 id="activitiesilike">Activities I like</h1>
<h2>It hurts, but I keep doing it</h2>
</hgroup>
</header>
<section>
<p>I like to swim, cycle and run. I am in training for my first
triathlon, but it is hard work.</p>
<h1 id="tritypes">Kinds of Triathlon</h1>
There are different kinds of triathlon - sprint, Olympic and so on.
<section>
<h1 id="mytri">The kind of triathlon I am aiming for</h1>
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</section>
</section>
<footer>
<nav>
More Information:
<a href="http://triathlon.org">Learn More About Triathlons</a>
</nav>
</footer>
</article>
<footer id="mainFooter">
©2011, Adam Freeman. <a href="http://apress.com">Visit Apress</a>
</footer>
</body>
</html>
此例调整了文档的结构以向博客通常采用的格局看齐,不过这恐怕不能说是最精彩的博客。文档的主体分作三大部分。第一部分是一个 header 元素,它汇总了各条博文信息,为文档其他部分提供了一个锚点。第二部分是末尾那个 footer 元素,它与 header 相照应,提供了适合其余内容的基本信息。新增加的东西在第三部分:article 元素。
7、生成附注栏
aside 元素用来表示跟周边内容稍沾一点边的内容,类似于书籍或杂志中的侧栏。它可以是背景信息、到相关文章的链接。
元素 | aside |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含流元素的元素,但该元素不能是 address 元素的后代元素 |
局部属性 | 无 |
内容 | style 元素和流内容 |
标签用法 | 开始标签和结束标签 |
习惯样式 | aside { display: block; } |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1, h2, h3, article > footer { background: grey; color: white; }
hgroup > h1 { margin-bottom: 0; margin-top: 0}
hgroup > h2 { background: grey; color: white; font-size: 1em;
margin-top: 0px; margin-bottom: 2px}
body > header *, body > footer * { background:transparent; color:black;}
article {border: thin black solid; padding: 10px; margin-bottom: 5px}
article > footer {padding:5px; margin: 5px; text-align: center}
article > footer > nav > a {color: white}
body > article > section,
body > article > section > section {margin-left: 10px;}
body > header, body > footer {
border: medium solid black; padding-left: 5px; margin: 10px 0 10px 0;
}
body > nav { text-align: center; padding: 2px; border : dashed thin black;}
body > nav > a {padding: 2px; color: black}
aside { width:40%; background:white; float:right; border: thick solid black;
margin-left: 5px;}
aside > section { padding: 5px;}
aside > h1 {background: white; color: black; text-align:center}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Things I like</h1>
<h2>by Adam Freeman</h2>
</hgroup>
<nav>
<h1>Contents</h1>
<ul>
<li><a href="#fruitsilike">Fruits I Like</a></li>
<li><a href="#activitiesilike">Activities I Like</a></li>
</ul>
</nav>
</header>
<article>
<header>
<hgroup>
<h1 id="fruitsilike">Fruits I Like</h1>
<h2>How I Learned to Love Citrus</h2>
</hgroup>
</header>
<aside>
<h1>Why Fruit is Healthy</h1>
<section>
Here are three reasons why everyone should eat more fruit:
<ol>
<li>Fruit contains lots of vitamins</li>
<li>Fruit is a source of fibre</li>
<li>Fruit contains few calories</li>
</ol>
</section>
</aside>
I like apples and oranges.
<section>
<h1 id="morefruit">Additional fruits</h1>
I also like bananas, mangoes, cherries, apricots, plums,
peaches and grapes.
<section>
<h1>More information</h1>
You can see other fruits I like <a href="fruitlist.html">here</a>
</section>
</section>
<footer>
<nav>
More Information:
<a href="http://fruit.org">Learn More About Fruit</a>
</nav>
</footer>
</article>
<article>
<header>
<hgroup>
<h1 id="activitiesilike">Activities I like</h1>
<h2>It hurts, but I keep doing it</h2>
</hgroup>
</header>
<section>
<p>I like to swim, cycle and run. I am in training for my first
triathlon, but it is hard work.</p>
<h1 id="tritypes">Kinds of Triathlon</h1>
There are different kinds of triathlon - sprint, Olympic and so on.
<section>
<h1 id="mytri">The kind of triathlon I am aiming for</h1>
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</section>
</section>
<footer>
<nav>
More Information:
<a href="http://triathlon.org">Learn More About Triathlons</a>
</nav>
</footer>
</article>
<footer id="mainFooter">
©2011, Adam Freeman. <a href="http://apress.com">Visit Apress</a>
</footer>
</body>
</html>
8、提供联系信息
address 元素用来表示文档或 article 元素的联系信息。
元素 | address |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含流元素的元素 |
局部属性 | 无 |
内容 | 流内容。但是标题元素 h1~h6、section、footer、nav、article 和 aside 元素不能用做该元素的后代元素 |
标签用法 | 开始标签和结束标签 |
习惯样式 | address { display: block; font-style: italic; } |
9、生成详情区域
details 元素在文档中生成一个区域,用户可以展开它以了解关于某主题的更多信息。
元素 | details |
---|---|
元素类型 | 流 |
允许具备的父元素 | 任何可以包含流元素的元素 |
局部属性 | open |
内容 | 流内容及一个可有可无的 summary 元素 |
标签用法 | 开始标签和结束标签 |
习惯样式 | details { display: block; } |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1, h2, h3, article > footer { background: grey; color: white; }
hgroup > h1 { margin-bottom: 0; margin-top: 0}
hgroup > h2 { background: grey; color: white; font-size: 1em;
margin-top: 0px; margin-bottom: 2px}
body > header *, body > footer * { background:transparent; color:black;}
body > article > section,
body > article > section > section {margin-left: 10px;}
body > header {
border: medium solid black; padding-left: 5px; margin: 10px 0 10px 0;
}
article {border: thin black solid; padding: 10px; margin-bottom: 5px}
details {border: solid thin black; padding: 5px}
details > summary { font-weight: bold}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Things I like</h1>
<h2>by Adam Freeman</h2>
</hgroup>
</header>
<article>
<header>
<hgroup>
<h1 id="activitiesilike">Activities I like</h1>
<h2>It hurts, but I keep doing it</h2>
</hgroup>
</header>
<section>
<p>I like to swim, cycle and run. I am in training for my first
triathlon, but it is hard work.</p>
<details>
<summary>Kinds of Triathlon</summary>
There are different kinds of triathlon - sprint, Olympic and so on.
I am aiming for Olympic, which consists of the following:
<ol>
<li>1.5km swim</li>
<li>40km cycle</li>
<li>10km run</li>
</ol>
</details>
</section>
</article>
</body>
</html>
浏览器会提供一个界面控件,它被激活时展开并显示 details 元素的内容。details 元素折拢时,只有其 summary 元素的内容可见。要让页面一显示 details 元素就呈展开状态,需要使用它的 open 属性。