默認情況下,HTML文檔的格式與文檔內(nèi)容在瀏覽器窗口中顯示的格式是不相關(guān)的,例如:瀏覽器會將連在一起的幾個空白字符折算為一個空格,并且會忽略換行符。HTML提供了組織內(nèi)容的方式,將顯示的內(nèi)容分段,預先編排內(nèi)容的格式等。
建立段落
HTML會忽略你在文本中輸入的回車符和其他額外的空格,網(wǎng)頁中的新的段落使用p元素標識,一個段落包含一個或多個相關(guān)句子,通常圍繞的是一個觀點或論點,或者多個論點間有一些共同的主題。
<body>
<h1>Antoni Gaudí</h1>
<p>Many tourists are drawn to Barcelona
to see Antoni Gaudí's incredible
architecture.</p>
<p>Barcelona celebrated the 150th
anniversary of Gaudí's birth in
2002.</p>
</body>
可以為段落添加樣式,包括字體、字號、顏色等。
div元素
div元素沒有具體的含義,如果沒有恰當?shù)脑乜捎脮r可以使用這個元素為內(nèi)容建立結(jié)構(gòu)并賦予其含義,它的含義通常通過class或id屬性指定。
但是注意不在萬不得已的情況下最好不要使用div元素,應該優(yōu)先考慮那些具有語義重要性的元素。
預先編排內(nèi)容格式
瀏覽器會將所有額外的回車和空格壓縮,并根據(jù)窗口的大小自動換行。pre元素可以改變?yōu)g覽器處理內(nèi)容的方式,阻止合并空白字符,讓源文檔中的格式得以保留。但注意除非有必要保留文檔原始格式,否則最好不要使用該元素,因為它削弱了通過使用元素和樣式來控制呈現(xiàn)結(jié)果這一機制的靈活性。
pre元素通常和code元素搭配使用,用于展示代碼示例,因為編程語言中的格式通常都很重要。
<p>Add this to your style sheet if you want
to display a dotted border underneath the
<code>abbr</code> element whenever it has
a <code>title</code> attribute.</p>
<pre>
<code>
abbr[title] {
border-bottom: 1px dotted #000;
}
</code>
</pre>
引用他處內(nèi)容
blockquote元素表示引自他處的一片內(nèi)容,與q元素類似(用于短的引述,不能跨行),但通常用在要引用的內(nèi)容較多的場景。該元素的cite屬性可以用來指定所引用的內(nèi)容的來源。
<blockquote cite="<a >The</a> apple forms a tree that is small and deciduous, ......</blockquote>
注意瀏覽器會忽略blockquote元素中的內(nèi)容的格式,默認對blockquote文本進行縮進。要在引用中建立結(jié)構(gòu),可以使用一些組織元素,例如p或hr。
瀏覽器應對q元素中的文本會自動加上特定語言的引號,但不同的瀏覽器的效果會有差異。下面是使用q元素的一個例子。
<p>She tried again, this time in French:
<q lang="fr">Avez-vous lu le livre
<cite lang="en">High Tide in Tucson</cite>
de Kingsolver? C'est inspirational.</q></p>
添加主題分隔
hr元素代表段落級別的主題分隔。在HTML5中,hr元素代表著向另一個相關(guān)主題的轉(zhuǎn)換,習慣樣式是一條橫貫頁面的直線。
<blockquote cite="http://en.wikipedia.org/wiki/Apple">
主題1
<hr>
主題2
<hr>
......
</blockquote>
上例在blockquote元素中加入了一些hr元素,形成一定的結(jié)構(gòu)。
將內(nèi)容組織為列表
HTML中列表的類型有有序列表、無序列表和描述列表。
1)有序列表,ol為父元素,li為列表項;
2)無序列表,ul為父元素,li為列表項;
3)描述列表,dl為父元素,dt和dd分別代表dl中的術(shù)語和描述。
在此之外,用戶還可以定義自己的列表。
有序列表
ol元素表示有序列表,列表項用li元素表示。
<body>
I like apples and oranges.
I also like:
<ol>
<li>bananas</li>
<li>mangoes</li>
<li>cherries</li>
<li>plums</li>
<li>peaches</li>
<li>grapes</li>
</ol>
You can see other fruits I like <a href="fruitlisthtml">here</a>
</body>
ol元素支持屬性如下:
1)start:設置列表首項的編號值,默認首項的編號為1;
2)type:設置顯示在各列表項旁的編號的類型,包括:
l:十進制數(shù)(默認),1,2,3,4
a:小寫拉丁字母,a,b,c,d
A:大寫拉丁字母,A,B,C,D
i:小寫羅馬數(shù)字,i,ii,iii,iv
I:大寫羅馬數(shù)字,I,II,III,IV
3)reversed:列表編號采用降序,部分瀏覽器支持
無序列表
ul元素表示無序列表,用li元素表示列表項。
<body>
I like apples and oranges.
I also like:
<ul>
<li>bananas</li>
<li>mangoes</li>
<li>cherries</li>
<li>plums</li>
<li>peaches</li>
<li>grapes</li>
</ul>
You can see other fruits I like <a href="fruitlisthtml">here</a>
</body>
無序列表的每個項目前都會顯示一個項目符號,符號的樣式可以用CSS屬性list-style-type控制。
li元素的屬性
li元素表示列表中的項目,它可以與ul、ol搭配使用,可以包含value屬性,表示列表項的序號。
<body>
I like apples and oranges.
I also like:
<ol>
<li>bananas</li>
<li value="4">mangoes</li>
<li>cherries</li>
<li value="7">plums</li>
<li>peaches</li>
<li>grapes</li>
</ol>
You can see other fruits I like <a href="fruitlisthtml">here</a>
</body>
描述列表
定義說明列表需要用到三個元素:dl、dt和dd元素,這些元素沒有局部屬性:
1)dl:表示說明列表;
2)dt:表示說明列表中的術(shù)語;
3)dd:表示說明列表中的定義。
<body>
I like apples and oranges.
I also like:
<dl>
<dt>Apple</dt>
<dd>The apple is the pomaceous fruit of the apple tree</dd>
<dd><i>Malus domestica</i></dd>
<dt>Banana</dt>
<dd>The banana is the parthenocarpic fruit of the banana tree</dd>
<dd><i>Musa acuminata</i></dd>
<dt>Cherry</dt>
<dd>The cherry is the stone fruit of the genus <i>Prunus</i></dd>
</dl>
You can see other fruits I like <a href="fruitlist.html">here</a>.
</body>
自定義列表
HTML中的ul元素結(jié)合CSS中的counter特性和:before選擇器,可以生成復雜的列表。下面是一個例子:
<head>
......
<style>
body{
counter-reset: OuterItemCount 5 InnerItemCount;
}
#outerlist > li:before {
content: counter(OuterItemCount)". ";
counter-increment: OuterItemCount 2;
}
ulinnerlist > li:before {
content: counter(InnerItemCount, lower-alpha) ". ";
counter-increment: InnerItemCount;
}
</style>
</head>
<body>
I like apples and oranges.
I also like:
<ul id="outerlist" style="list-style-type: none">
<li>bananas</li>
<li>mangoes, including:</li>
<ul class="innerlist">
<li>Haden mangoes</li>
<li>Keitt mangoes</li>
<li>Kent mangoes</li>
</ul>
<li>cherries</li>
<li>plums, including:
<ul class="innerlist">
<li>Elephant Heart plums</li>
<li>Stanley plums</li>
<li>Seneca plums</li>
</ul>
</li>
<li>peaches</li>
<li>grapes</li>
</ul>
You can see other fruits I like <a href="fruitlist.html">here</a>.
</body>
使用插圖
HTML5對插圖的定義為:一個獨立的內(nèi)容單元,可帶標題,通常作為一個整體被文檔的主體引用,把它從文檔主體中刪除也不會影響文檔的含義。
HTML使用figure元素插入圖表、照片、圖形、插圖、代碼片段等,figcaption是figure的標題,可選,出現(xiàn)在figure內(nèi)容的開頭或結(jié)尾處。
<body>
I like apples and oranges.
<figure>
<figcaption>Listing 23. Using the code element</figcaption>
<code>var fruits = ["apples", "oranges", "mangoes", "cherries"];<br>document.writen("I like " + fruits.length + " fruits");
</code>
</figure>
You can see other fruits I like <a href="fruitlist.html">here</a>.
</body>
figure元素生成了一個將code元素裹在其中的插圖,并用figcaption元素為其添加了一個標題。注意figcaption元素必須是figure元素的第一個或最后一個子元素。
figure元素可以包含多個內(nèi)容塊,但只能包含一個figcaption。