2 / 10 · Intro
Tags and elements
An element is usually three things in a row: an opening tag, some content and a closing tag with a slash.
Jump to the 10 practice questionsYou will learn
- Name the parts of an element: tags, attributes, content
- Nest elements and close them in reverse order
- Mark meaning with strong and em, and show special characters
Before this: What a web page is
1Explain
The parts of an element
Look at <p>Bread is ready.</p>. The opening tag <p> says a paragraph starts here. The closing tag </p>, with a slash after the first angle bracket, says it ends. Everything between them is the content. The whole thing, tags plus content, is the element.
Edit the text between the tags, then the tags themselves, and watch the preview.
<p>Bread is <strong>ready</strong>.</p>2Explain
Attributes add details
An opening tag can carry attributes, extra settings written as name="value". In <a href="menu.html">Menu</a> the attribute href says where the link goes. In <html lang="en"> the attribute lang names the language. Put the value in double quotes; the standard allows some unquoted values, but quotes never cause trouble.
<p class="note" id="opening">We open at <strong>7 am</strong>.</p>3Explain
Example: reading one element
Read this line part by part, from left to right.
<abbr title="kilogram">kg</abbr><abbrstarts an element: abbr, an abbreviation.title="kilogram"is an attribute: its name is title and its value, in quotes, is the full word.>ends the opening tag. The content follows: kg.</abbr>is the closing tag. Opening tag, content and closing tag together are one element.- On screen only kg is drawn. Most browsers show the title value when the pointer rests on it.
4Problem
What href is
In <a href="menu.html">Menu</a>, what is href?
Hint
It is written inside the opening tag as name="value".
Solution
Answer: an attribute
It is a name="value" setting inside the opening tag.
5Explore
Close in reverse order
Elements go inside other elements. In the example above, <strong> sits inside <p>. The rule is the same as with brackets in arithmetic: whatever opened last must close first. <p><strong>7 am</p></strong> is wrong because the paragraph closes while the strong element is still open. Browsers will guess what you meant, but different mistakes lead to different guesses.
Pick a content: elements sit inside each other, and character references draw signs that would otherwise start a tag.
Tap a choice: the code and the preview change together.
<p>Bread is <strong>ready</strong>.</p>Nothing is drawn: there is no visible content.
6Problem
Correctly nested?
Is <p><em>Hi</p></em> correctly nested?
Hint
Which tag was opened last? It must close first.
Solution
Answer: No, em must close before p
The last element opened must be the first one closed: <p><em>Hi</em></p>.
7Problem
Closing tags in order
<p><strong><em>HotThe line opens p, then strong, then em. Put the closing tags in the order they must be written.
</strong></em></p>
Hint
The element opened last closes first.
Solution
Answer:
</em></strong></p>
em was opened last, so it closes first; p was opened first, so it closes last.
8Problem
Elements in one line
<p>
<em>Rye bread</em><br>
<strong>Ready at 7 am</strong>
</p>How many elements does this line contain? Count the outer p as well.
Hint
A void element counts as an element too.
Solution
Answer: 4
p, em, strong and br: 4. br has no content and no closing tag, but it is still an element.
9Explain
Void elements
Some elements cannot have content, so they have no closing tag. These are called void elements. The common ones are <br> (a line break), <hr> (a thematic break), <img>, <input>, <meta> and <link>. You may see them written with a slash, such as <br />; in HTML the slash is allowed and ignored, so both forms work.
<p>12 Mill Street<br>Springfield</p>
<hr>
<p>Open from 7 am.</p>10Problem
An element with no content
Which of these is a void element?
Hint
It has no closing tag.
Solution
Answer: br
br has no content and no closing tag.
11Problem
A closing tag that should not be there
Which of these is not valid HTML?
Hint
A void element has no closing tag.
Solution
Answer: <br></br>
br is a void element. The slash in <br /> is allowed and ignored, but </br> is an error; browsers even read it as a second line break.
12Problem
Show a less-than sign
Because < starts a tag, you cannot type it as ordinary text. Write a character reference instead: < for less than, > for greater than, and & for the ampersand itself. A comment, <!-- like this -->, is ignored by the browser and is handy for notes to yourself. Finally, HTML collapses white space: ten spaces or three line breaks in your file show up as a single space.
Hint
A character reference starts with & and ends with ;.
Solution
Answer: <
< stands for less than, > for greater than and & for the ampersand.
13Practice
strong and em
Wrap 24 hours in <strong> and a day ahead in <em>. Keep both inside the paragraph.
- strong inside p
- em inside p
<p>Our sourdough takes 24 hours to make, so order it a day ahead.</p>Hint
Put the opening tag right before the words and the closing tag right after them.
A solution
<p>Our sourdough takes <strong>24 hours</strong> to make, so order it <em>a day ahead</em>.</p>14Practice
Show a tag as text
Make the paragraph show the tag itself: change the text so it reads To start a paragraph, type <p>. on screen. Use character references.
- shows
as text
<p id="demo">To start a paragraph, type p.</p>Hint
Write < for < and > for >.
A solution
<p id="demo">To start a paragraph, type <p>.</p>15Explain
Common mistakes
<p>Open <strong>daily</p></strong>
<p>Closed on Monday.</p><p>Open <strong>daily</strong></p>
<p>Closed on Monday.</p>strong was opened inside p, so it must close before p does.
16
Summary
- Element = opening tag + content + closing tag.
- Attributes are name="value" pairs inside the opening tag.
- Close elements in reverse order of opening; void elements have no closing tag.
- Use
<>&for special characters; white space collapses.