基本 HTML 文字
HTML 的其中一件核心工作，就是給出文件的結構和含義（又稱semantics (en-US)），以便瀏覽器正確顯示。本文章旨在說明 HTML 可透過增加標題、章節、強調、建立清單等，建立結構化的頁面。
|需求：||熟悉基本 HTML、在 Getting started with HTML 有講解。|
|目標：||學習如何標記一個具有文字的基礎頁面賦予它結構及含義— 包含段落, 標題, 列表, 強調文字, 以及引用句|
在 HTML 裡，每個段落都被包在
<p> (en-US) 元素中，就像：
<p>I am a paragraph, oh yes I am.</p>
<h1>I am the title of the story.</h1>
在 HTML 裡有六種標題元素：
<h6> (en-US). 每個元素分別代表著在文件中的不同層級;
<h3> 代表更次級的副標題, 依此類推。
<h1>The Crushing Bore</h1> <p>By Chris Mills</p> <h2>Chapter 1: The dark night</h2> <p>It was a dark night. Somewhere, an owl hooted. The rain lashed down on the ...</p> <h2>Chapter 2: The eternal silence</h2> <p>Our protagonist could not so much as a whisper out of the shadowy figure ...</p> <h3>The specter speaks</h3> <p>Several more hours had passed, when all of a sudden the specter sat bolt upright and exclaimed, "Please have mercy on my soul!"</p>
為了回答這個問題，我們先看看 text-start.html 這個文章（鷹嘴豆泥食譜）的最前端。請先下載這個文件的副本到你的電腦，等一下練習時會用到。這個文件目前包含很多不同的內容，並沒有被標記出架構，唯一的排版只有換行而已。
- 使用者造訪一個網站時，時常只是快速的瀏覽內容以找尋他們正在找的相關內容，更常時候，使用者只有看一個頁面內的標題而已（來源：spend a very short time on a web page）。如果他們沒在短時間內找到實用的資訊，他們就會放棄這個頁面而去其他網頁尋找。
- 搜尋引擎會把標題作為重要關鍵字來排序搜尋結果。所以如果一個頁面沒有標題，就很難被搜尋引擎排在上方。換句話說，就是沒有達到搜尋引擎最佳化（Search Engine Optimization (SEO)）。
- 視力嚴重受損的使用者通常無法閱覽網頁，而透過螢幕閱讀器（screen reader）聽頁面。螢幕閱讀器軟體提供使用者能夠更快速的取得頁面資訊。其中一個方式是透過朗誦出文件的架構，能讓使用者更快地獲得他們在尋找的資訊。所以假如一個頁面沒被恰當地標記架構，就會迫使使用者必須聆聽完整個頁面以取得尋找的資訊。
Active learning: Giving our content structure
Let's jump straight in with a live example. In the example below, add elements to the raw text in the Input field so that it appears as a heading and two paragraphs in the Output field.
If you make a mistake, you can always reset it using the Reset button. 如果你中途卡關，點擊按鈕來查看答案。
Why do we need semantics?
Semantics are relied on everywhere around us — we rely on previous experience to tell us what the function of an everyday object is; when we see something, we know what its function will be. So, for example, we expect a red traffic light to mean "stop", and a green traffic light to mean "go". Things can get tricky very quickly if the wrong semantics are applied (Do any countries use red to mean "go"? I hope not.)
In a similar vein, we need to make sure we are using the correct elements, giving our content the correct meaning, function, or appearance. In this context the
<h1> (en-US) element is also a semantic element, which gives the text it wraps around the role (or meaning) of "a top level heading on your page."
<h1>This is a top level heading</h1>
By default, the browser will give it a large font size to make it look like a heading (although you could style it to look like anything you wanted using CSS). More importantly, its semantic value will be used in multiple ways, for example by search engines and screen readers (as mentioned above).
On the other hand, you could make any element look like a top level heading. Consider the following:
<span style="font-size: 32px; margin: 21px 0; display: block;">Is this a top level heading?</span>
This is a
Now let's turn our attention to lists. Lists are everywhere in life — from your shopping list to the list of directions you subconsciously follow to get to your house every day, to the lists of instructions you are following in these tutorials! Lists are everywhere on the Web too, and we've got three different types to worry about.
Unordered lists are used to mark up lists of items for which the order of the items doesn't matter — let's take a shopping list as an example.
milk eggs bread hummus
Every unordered list starts off with a
<ul> (en-US) element — this wraps around all the list items:
<ul> milk eggs bread hummus </ul>
The last step is to wrap each list item in a
<li> (en-US) (list item) element:
<ul> <li>milk</li> <li>eggs</li> <li>bread</li> <li>hummus</li> </ul>
Active learning: Marking up an unordered list
Try editing the live sample below to create your very own HTML unordered list.
Ordered lists are lists in which the order of the items does matter — let's take a set of directions as an example:
Drive to the end of the road Turn right Go straight across the first two roundabouts Turn left at the third roundabout The school is on your right, 300 meters up the road
The markup structure is the same as for unordered lists, except that you have to wrap the list items in an
<ol> (en-US) element, rather than
<ol> <li>Drive to the end of the road</li> <li>Turn right</li> <li>Go straight across the first two roundabouts</li> <li>Turn left at the third roundabout</li> <li>The school is on your right, 300 meters up the road</li> </ol>
Active learning: Marking up an ordered list
Try editing the live sample below to create your very own HTML ordered list.
Active learning: Marking up our recipe page
So at this point in the article, you have all the information you need to mark up our recipe page example. You can choose to either save a local copy of our text-start.html starting file and do the work there, or do it in the editable example below. Doing it locally will probably be better, as then you'll get to save the work you are doing, whereas if you fill it in to the editable example, it will be lost the next time you open the page. Both have pros and cons.
If you get stuck, you can always press the Show solution button, or check out our text-complete.html example on our github repo.
It is perfectly ok to nest one list inside another one. You might want to have some sub-bullets sitting below a top level bullet. Let's take the second list from our recipe example:
<ol> <li>Remove the skin from the garlic, and chop coarsely.</li> <li>Remove all the seeds and stalk from the pepper, and chop coarsely.</li> <li>Add all the ingredients into a food processor.</li> <li>Process all the ingredients into a paste.</li> <li>If you want a coarse "chunky" hummus, process it for a short time.</li> <li>If you want a smooth hummus, process it for a longer time.</li> </ol>
Since the last two bullets are very closely related to the one before them (they read like sub-instructions or choices that fit below that bullet), it might make sense to nest them inside their own unordered list, and put that list inside the current fourth bullet. This would look like so:
<ol> <li>Remove the skin from the garlic, and chop coarsely.</li> <li>Remove all the seeds and stalk from the pepper, and chop coarsely.</li> <li>Add all the ingredients into a food processor.</li> <li>Process all the ingredients into a paste. <ul> <li>If you want a coarse "chunky" hummus, process it for a short time.</li> <li>If you want a smooth hummus, process it for a longer time.</li> </ul> </li> </ol>
Try going back to the previous active learning example and updating the second list like this.
Emphasis and importance
In human language, we often emphasise certain words to alter the meaning of a sentence, and we often want to mark certain words as important or different in some way. HTML provides various semantic elements to allow us to mark up textual content with such effects, and in this section, we'll look at a few of the most common ones.
When we want to add emphasis in spoken language, we stress certain words, subtly altering the meaning of what we are saying. Similarly, in written language we tend to stress words by putting them in italics. For example, the following two sentences have different meanings.
I am glad you weren't late.
I am glad you weren't late.
The first sentence sounds genuinely relieved that the person wasn't late. In contrast, the second one sounds sarcastic or passive-aggressive, expressing annoyance that the person arrived a bit late.
In HTML we use the
<em> (en-US) (emphasis) element to mark up such instances. As well as making the document more interesting to read, these are recognised by screen readers and spoken out in a different tone of voice. Browsers style this as italic by default, but you shouldn't use this tag purely to get italic styling. To do that, you'd use a
<span> (en-US) element and some CSS, or perhaps an
<i> (en-US) element (see below).
<p>I am <em>glad</em> you weren't <em>late</em>.</p>
To emphasize important words, we tend to stress them in spoken language and bold them in written language. For example:
In HTML we use the
<strong> (en-US) (strong importance) element to mark up such instances. As well as making the document more useful, again these are recognized by screen readers and spoken in a different tone of voice. Browsers style this as bold text by default, but you shouldn't use this tag purely to get bold styling. To do that, you'd use a
<span> (en-US) element and some CSS, or perhaps a
<b> (en-US) element (see below).
<p>This liquid is <strong>highly toxic</strong>.</p> <p>I am counting on you. <strong>Do not</strong> be late!</p>
You can nest strong and emphasis inside one another if desired:
<p>This liquid is <strong>highly toxic</strong> — if you drink it, <strong>you may <em>die</em></strong>.</p>
Active learning: Let's be important
In this active learning section, we have provided an editable example. Inside it, we'd like you to try adding emphasis and strong importance to the words you think need them, just to have some practice.
Italic, bold, underline...
The elements we've discussed so far have clearcut associated semantics. The situation with
<i> (en-US), and
<u> (en-US) is somewhat more complicated. They came about so people could write bold, italics, or underlined text in an era when CSS was still supported poorly or not at all. Elements like this, which only affect presentation and not semantics, are known as presentational elements and should no longer be used, because as we've seen before, semantics is so important to accessibility, SEO, etc.
<u> with new, somewhat confusing, semantic roles.
Here's the best rule of thumb: it's likely appropriate to use
<u> to convey a meaning traditionally conveyed with bold, italics, or underline, provided there is no more suitable element. However, it always remains critical to keep an accessibility mindset. The concept of italics isn't very helpful to people using screen readers, or to people using a writing system other than the Latin alphabet.
<i>(en-US) is used to convey a meaning traditionally conveyed by italic: Foreign words, taxonomic designation, technical terms, a thought...
<b>(en-US) is used to convey a meaning traditionally conveyed by bold: Key words, product names, lead sentence...
<u>(en-US) is used to convey a meaning traditionally conveyed by underline: Proper name, misspelling...
備註： A kind warning about underline: People strongly associate underlining with hyperlinks. Therefore, on the Web, it's best to underline only links. Use the
<u> element when it's semantically appropriate, but consider using CSS to change the default underline to something more appropriate on the Web. The example below illustrates how it can be done.
<!-- scientific names --> <p> The Ruby-throated Hummingbird (<i>Archilochus colubris</i>) is the most common hummingbird in Eastern North America. </p> <!-- foreign words --> <p> The menu was a sea of exotic words like <i lang="uk-latn">vatrushka</i>, <i lang="id">nasi goreng</i> and <i lang="fr">soupe à l'oignon</i>. </p> <!-- a known misspelling --> <p> Someday I'll learn how to <u style="text-decoration-line: underline; text-decoration-style: wavy;">spel</u> better. </p> <!-- Highlight keywords in a set of instructions --> <ol> <li> <b>Slice</b> two pieces of bread off the loaf. </li> <li> <b>Insert</b> a tomato slice and a leaf of lettuce between the slices of bread. </li> </ol>
That's it for now! This article should have given you a good idea of how to start marking up text in HTML, and introduced you to some of the most important elements in this area. There are a lot more semantic elements to cover in this area, and we'll look at a lot more in our 'More Semantic Elements' article, later on in the course. In the next article, we'll be looking in detail at how to create hyperlinks, possibly the most important element on the Web.