Sub topic 2
Elements, Tags and Attributes
This chapter assumes you can create and save a text file, open it in a browser, and have seen a web page's text and links. You do not need to know any HTML yet. By the end you will be able to read any piece of markup and name its parts.
The anatomy of an element
An HTML document is built from elements. An element is a unit of content together with the markup that tells the browser what the content is. Most elements have three parts:
- A start tag, such as
<p>. - Content, which is text or other elements.
- An end tag, such as
</p>, which repeats the name with a slash.
Together these make <p>Hello</p>. The tag is only the markup in angle brackets. The element is the whole thing, including its content. People often use "tag" and "element" as if they meant the same thing, but the difference matters when you read documentation.
Tag names are not case sensitive, but lowercase is the convention. Use it.
Attributes
An attribute adds information to an element. Attributes go inside the start tag, after the element name. Each one is a name, an equals sign and a quoted value.
<p>Read the <a href="https://developer.mozilla.org/" title="MDN Web Docs">documentation</a> and make <strong>sure</strong> to <em>practice</em>.</p>
<img src="cat.jpg" alt="A grey cat asleep on a sofa" width="400" height="300">The a element has two attributes. href holds the link target, and title holds extra text that browsers often show as a tooltip. The img element has four: src, alt, width and height.
Follow these rules:
- Separate attributes with spaces.
- Quote values with double quotes. Quotes are optional for simple values, but always using them avoids bugs when a value contains a space.
- Do not repeat an attribute on one element. The browser keeps the first one and ignores the rest.
- Some attributes are boolean. Their presence means "on", as in
requiredon an input.
Some attributes work on every element. Common ones are id, class, lang and title. An id must be unique in the page. Other attributes belong to specific elements. For example, href is meaningful on a but not on p.
Void elements
Some elements cannot have content, so they have no end tag. These are void elements. The img element above is one. Others you will meet are br, hr, meta, link and input.
Do not write </img>. A slash before the closing bracket, as in <img ... />, is allowed but has no effect in HTML.
Nesting
Elements can sit inside other elements. The inner element must close before the outer one does.
<p>This is <strong>very <em>important</em></strong> text.</p>The diagram shows the tree that the browser builds from this line.
Common errors
| Mistake | Example | Result |
|---|---|---|
| Overlapping elements | `<b><i>text</b></i>` | The browser guesses a repair, and the result may differ from what you meant. |
| Missing end tag | `<p>One <p>Two` | A new `p` closes the old one, but other elements may swallow the rest of the page. |
| Unquoted value with a space | `<img alt=a cat>` | The alt text is `a`, and `cat` becomes a separate attribute. |
| Misspelled attribute | `<a hrf="/">` | The browser ignores it silently, so the link goes nowhere. |
Practice
Exercise
In <a href="/about" class="nav">About us</a>, name the start tag, the content, the end tag and each attribute with its value.
Show answer
The start tag is <a href="/about" class="nav">. The content is About us. The end tag is </a>. The attributes are href with the value /about and class with the value nav.
Exercise
Why is <p>Some <em>stressed text</p></em> invalid, and how do you fix it?
Show answer
The elements overlap, because em starts inside p but ends after p closes. Close the inner element first: <p>Some <em>stressed text</em></p>.