Elements - HTML Tutorial
You've now used a handful of elements. Let's look at how elements work in a bit more detail, because they are the building blocks of every web page.
The Anatomy of an Element
Most elements have three parts:
<p>This is a paragraph.</p>
- The opening tag,
<p>, contains the element's name inside angle brackets. - The content, "This is a paragraph.", is what the element contains. Content can be text, other elements, or both.
- The closing tag,
</p>, is the same as the opening tag but with a forward slash before the name.
The whole thing, from the opening tag to the closing tag, is the element.
Element names are not case-sensitive, so <P> and <p> mean the same thing. However, lowercase is the standard convention, and it's what you'll see in this tutorial and in almost all modern code.
Tags vs. Elements
People often say "tags" when they mean "elements", and you'll hear both words used. Strictly speaking, a tag is just the marker at the start or end, such as <p> or </p>. The element is the tags plus everything between them. Knowing the difference makes documentation and error messages easier to understand.
Nesting Elements
Elements can contain other elements. When they do, they must be properly nested: the element you opened last must be closed first.
<p>This is <em>very</em> important.</p>
<p>This is <em>very important.</p></em>
In the incorrect example, the em element starts inside the paragraph but tries to end outside it. A good way to picture nesting is as boxes inside boxes: a box inside another box has to be closed before the outer box can be.
Empty Elements
A few elements have no content, so they don't need a closing tag. These are called void elements (or empty elements). You've already seen one, <meta charset="utf-8">. Other common ones include:
<br>for a line break<img>for an image<hr>for a thematic break between sections<input>for a form field<link>for linking to a style sheet
You may sometimes see these written with a slash at the end, such as <br />. That style comes from an older language called XHTML. Both forms work in HTML, and this tutorial uses the shorter <br>.
Comments
You can leave notes in your code using comments. Anything between <!-- and --> is ignored by the browser and doesn't appear on the page:
<!-- This is a comment. Visitors won't see it on the page. -->
<p>Visitors will see this paragraph.</p>
Comments are useful for explaining your code or temporarily hiding something while you experiment. Remember that anyone can read them using View Source, so never put private information in a comment.
Mistakes and Unknown Elements
Browsers are very forgiving. If you misspell an element name or use one that doesn't exist, the browser won't show an error. It simply ignores the tag it doesn't understand and displays the content inside as plain text.
Warning: <blink>Hot coffee!</blink> Handle with care.
The blink element once made text flash on and off in some old browsers. It was never part of the HTML standard, and no modern browser supports it, so today the text just appears normally.
This forgiveness is handy, but it also means mistakes can go unnoticed. If something doesn't look right, check your spelling and make sure every element is closed and nested correctly. In Web Standards, you'll learn how to use a validator to find these problems automatically.