Sub topic 2
Elements, Tags and Attributes
This chapter assumes you can create and save a text file, open a local HTML file in a browser, and recognize a web page when you see one. You do not need to know any HTML yet beyond the idea that a browser reads markup and displays a page. By the end you will be able to read any piece of HTML and name its parts, and you will know the errors that most often trip up learners.
The working model
HTML is made of elements. An element is a unit of the document with a defined meaning, such as a paragraph, a link or an image. You write an element in the source file using tags, which are the pieces of text in angle brackets that mark where the element begins and ends.
When a browser loads your file, it parses the text and builds a tree of elements in memory, called the DOM (Document Object Model). The tags are only the written form. The element is the thing in the tree. This distinction explains several behaviors later in the chapter, such as why a missing end tag does not always break a page.
Anatomy of an element
A typical element has four parts.
<p class="intro">Hello, reader.</p>| Part | In the example | Role |
|---|---|---|
| Start tag | `<p class="intro">` | Opens the element and carries its attributes |
| Attribute | `class="intro"` | Adds extra information to the element |
| Content | `Hello, reader.` | The text or child elements inside |
| End tag | `</p>` | Closes the element |
The element is the whole thing together, from the opening < of the start tag to the closing > of the end tag. The end tag repeats the element name and adds a forward slash. It never carries attributes.
Names are case-insensitive, but use lowercase
HTML element names are not case-sensitive, so <P> and <p> are the same element. The convention is to write all element and attribute names in lowercase. It is easier to read and matches almost every style guide.
Attributes
An attribute gives an element extra information. Attributes live only in the start tag. Most take the form name="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">In this snippet, the a element has two attributes: href (where the link goes) and title (a short advisory text, often shown as a tooltip). The img element has four: src, alt, width and height.
Rules for writing attributes
- Separate each attribute from the tag name and from other attributes with whitespace.
- Put the value in quotes. Double quotes are the convention.
- Use one pair of quotes style per attribute. If the value contains a double quote, wrap it in single quotes, or use the character reference
". - An attribute name may appear only once on an element. If you repeat it, the browser keeps the first one and ignores the rest.
Boolean attributes
Some attributes are boolean: their presence means "true" and their absence means "false". The required attribute on a form control is one.
<input id="email" name="email" type="email" required>You do not write required="false" to turn it off. Writing required="false" still turns it on, because the attribute is present. To turn it off, remove the attribute.
Global attributes
Some attributes can appear on any element. These are the global attributes. The ones you will meet most often are:
| Attribute | Purpose |
|---|---|
| `id` | A unique identifier for the element in the document |
| `class` | One or more space-separated names used to group elements, mostly for CSS and JavaScript |
| `lang` | The human language of the element's content |
| `title` | Advisory text about the element |
| `hidden` | Marks the element as not currently relevant, so it is not rendered |
The id value must be unique within the page. Two elements with the same id is a validation error and causes confusing behavior when something refers to that id, such as a label that uses for or a link that jumps to a fragment.
Other attributes belong to particular elements. href makes sense on a, and src makes sense on img. Putting href on a p is not an error the browser will report loudly, but it does nothing. A validator will flag it.
Void elements
Some elements cannot have content, so they have no end tag. These are void elements. You have already seen one: img. Others you will meet include br (a line break), hr (a thematic break), meta and link (both in the head), and input.
<p>First line<br>Second line</p>
<hr>
<img src="logo.png" alt="Company logo">You may see a trailing slash, as in <br />. HTML allows this on void elements and ignores it, but it is not required. Do not put it on non-void elements. Writing <div /> does not create an empty div. The browser ignores the slash and treats it as an opening <div>, so everything after it becomes a child until a matching end tag appears.
Nesting and the tree
Elements can contain other elements. The inner element is a child and the outer one is its parent. Elements must nest cleanly: the element you open last must be the first one you close.
<p>This is <strong>very <em>important</em></strong> text.</p>The em element sits inside strong, which sits inside p. Each closes before its parent does.
This version is wrong because the tags overlap:
<p>This is <strong>very <em>important</strong></em> text.</p>Browsers try to repair overlap errors, and the result may not be what you intended. You can predict this repair only by learning the parsing rules, so it is better to avoid the problem.
Optional end tags and what the parser does
For a few elements, HTML lets you leave out the end tag. The most common cases are p, li, td, th and tr. The parser closes the element for you when it sees something that cannot be inside it, such as the next li or a block element after a p.
<ul>
<li>HTML
<li>CSS
<li>JavaScript
</ul>This is valid HTML and produces three list items. Still, write the end tags. Explicit end tags make the structure obvious to readers, prevent surprises when you later add content, and keep your habits correct for the many elements where omission is not allowed.
Common errors
These are the mistakes you are most likely to make and how to spot them.
- Missing end tag on an element that needs one. If you forget
</div>or</a>, everything after it becomes content of that element. The symptom is usually a layout or link style that spreads further than you expected. - Missing closing quote. An attribute like
href="page.html>swallows the>and following text into the value. The symptom is a link that goes to a strange address and text that disappears. - Overlapping tags. As shown above,
<b><i>text</b></i>is invalid. Close in reverse order. - Duplicate
idvalues. The page may look fine, but links and labels may point to the wrong element. - Misspelled names.
<pargraph>is not an error the parser rejects. It creates an unknown element that has no meaning and no default styling, so it fails silently. - Invalid boolean values.
disabled="false"still disables the control.
Try it yourself
Save this file as anatomy.html and open it in a browser. Then open the browser's developer tools and inspect the elements. You will see the tree the browser built from your tags.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Anatomy test</title>
</head>
<body>
<p id="note" class="intro">Hello, <em>reader</em>.</p>
<img src="missing.jpg" alt="A placeholder image" width="200" height="100">
<br>
<input type="text" name="q" required>
</body>
</html>Try breaking it. Delete the </em> end tag, or add a second id="note" elsewhere, and watch how the tree in the developer tools changes.
Exercise
In <a href="/about" class="nav-link">About us</a>, name the element, its two attributes, its content, and say which part of the markup could not have a href attribute added to it.
Show answer
The element is a. Its attributes are href="/about" and class="nav-link". Its content is the text "About us". Attributes can only be added to the start tag, so href cannot go on the end tag </a>.
Exercise
A learner writes <input type="checkbox" checked="false"> and is surprised that the box is checked. Explain why and give the fix.
Show answer
checked is a boolean attribute. Its presence means true, whatever value it has. To leave the box unchecked, remove the attribute: <input type="checkbox">.
Exercise
What is wrong with <p>Save <strong>your <em>work</strong></em> often.</p>, and how do you fix it?
Show answer
The strong and em elements overlap. em was opened last, so it must close first. The fix is <p>Save <strong>your <em>work</em></strong> often.</p>.