Sub topic 1
Elements and attributes: a quick refresher
Links, images, and lists are all built from HTML elements and attributes. This chapter reviews how both work, so the later chapters can focus on the elements themselves.
Prerequisites
This chapter assumes you can:
- Read the basic structure of an HTML document (
html,head,body). - Edit a file in a text editor and open it in a browser.
- Follow file and folder paths such as
images/photo.jpg.
The working model
An element is a unit of the page. Most elements have a start tag, content, and an end tag. The browser reads these tags and builds a tree of elements, which it then renders.
<p class="intro">Welcome to <em>my</em> site.</p>Here p is the element and class="intro" is an attribute on its start tag. The em element sits inside the p element, so it is a child of it.
An attribute is a name and a value written in the start tag. It changes how the element behaves or gives the browser extra data. In name="value", the name is fixed by the HTML specification and the value is yours, such as a URL or a piece of text.
flowchart LRVoid elements
Some elements have no content, so they have no end tag. These are void elements. The img element is one of them.
<img src="images/red-fox.jpg" alt="A red fox standing in snow">Do not write </img>. The browser ignores the stray end tag or treats it as an error. A trailing slash (<img ... />) is allowed but not required.
Attribute rules to remember
- Put attributes only in the start tag, separated by spaces.
- Quote values with double quotes. Quotes are optional for simple values but required when a value contains spaces.
- Boolean attributes, such as
downloadorreversed, are on when present. You don't need to give them a value. - Attribute names are case-insensitive, but lowercase is the convention.
- Some attributes are required for the element to work. A link needs
hrefand an image needssrcandalt.
<a href="/files/guide.pdf" download>Download the guide (PDF)</a>Common errors
| Mistake | Effect | Fix |
|---|---|---|
| `<a href=guides/my page.html>` | The value stops at the first space. | Quote it, and avoid spaces in file names. |
| `<a href="page.html>Text</a>` | A missing closing quote swallows the following markup. | Close every quote. |
| `<b><i>text</b></i>` | The tags overlap and the browser has to repair the tree. | Close elements in the reverse order you opened them. |
| `<img src="a.jpg"></img>` | The end tag is invalid. | Write the start tag only. |
What comes next
The next chapters apply these rules to three families of elements. Links use a with href. Images use img with src and alt. Lists use ul, ol, and dl with their child elements.
Exercise
This markup has two attribute problems. What are they, and how do you fix them? <img src="images/red fox.jpg" alt=A red fox></img>
Show answer
The unquoted values break at spaces, so the src ends at "images/red" and the alt becomes "A". Quote both values, and rename the file or use a path without a space. The img element is void, so also remove the </img> end tag. Corrected: <img src="images/red-fox.jpg" alt="A red fox">