Sub topic 1
Elements and Attributes: A Quick Refresher
Every link, image, and list in this tutorial is built from two ideas: elements and attributes. This chapter reviews both, with attention to the details that cause bugs later. It also introduces the vocabulary the later chapters use.
Prerequisites
This chapter assumes you can already do the following:
- Describe the basic structure of an HTML document:
html,head, andbody. - Create a
.htmlfile in a text editor and open it in a browser. - Read file and folder paths such as
images/photo.jpgand../index.html.
If any of these is shaky, fix it first. Link and image paths depend on path reading in particular.
Elements: the working model
An element is a unit of a page with a defined meaning, such as a paragraph, a link, or an image. Most elements have three parts:
- A start tag, such as
<p>. - Content, which is text, other elements, or both.
- An end tag, such as
</p>.
Together, these three parts are one element. The tags are only the notation you write. The browser reads your source and builds a tree of elements in memory, called the DOM (Document Object Model). CSS, JavaScript, and screen readers all work with that tree, not with your raw text.
Two kinds of element matter here:
- Container elements wrap content and need an end tag:
a,ul,ol,li,figure,figcaption,nav,picture. - Void elements cannot have content and have no end tag:
img,source,br,hr,meta,link,input. You write<img ...>and stop. A trailing slash (<img ... />) is allowed but does nothing. An end tag such as</img>is an error.
Elements also fall into broad behavior groups. Block-level elements such as p, ul, ol, figure, and nav start on a new line. Inline elements such as a and img sit inside a line of text. HTML5 describes this more precisely with content categories, but the block/inline picture is enough for now. The a element is also unusual because it can wrap almost anything, including images and whole blocks.
Attributes: extra information in the start tag
An attribute is a name and value pair written inside the start tag. It configures the element or gives the browser more information about it.
<a href="https://example.com/" title="Example site">Example</a>Here href and title are attributes of the a element. The rules:
- Attributes go only in the start tag, never in the end tag.
- Separate them with whitespace. Order does not matter.
- Quote the value with double or single quotes. Unquoted values are legal only when they contain no spaces, quotes,
=,<,>, or backticks. Always quote them. - A boolean attribute is true by being present. You write
downloadorreversed, with no value needed.
Attributes you will meet in this tutorial
| Attribute | Used on | Purpose |
|---|---|---|
| `href` | `a` | Destination of the link |
| `target`, `rel` | `a` | Where the link opens and how it relates to the current page |
| `download` | `a` | Suggest saving the file |
| `src`, `alt` | `img` | Image file and text alternative |
| `width`, `height`, `loading` | `img` | Reserved space and load timing |
| `srcset`, `sizes` | `img`, `source` | Choose among image files |
| `start`, `reversed` | `ol` | Numbering of the list |
| `aria-label`, `aria-current` | many | Accessibility information |
Global attributes
Some attributes work on any element. The ones you will use most are id, class, lang, and title. The id attribute is especially important for this tutorial. It gives an element a unique name in the page, and fragment links such as #install point to it. Two elements with the same id are invalid, and a link to that id goes to the first one only.
A complete worked example
This file uses one element of each kind discussed so far. Save it as refresher.html and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Elements and attributes</title>
</head>
<body>
<h1 id="top">Trail notes</h1>
<p>
Read the <a href="https://example.com/" title="Opens the example site">example site</a>
for details.
</p>
<img src="fox.jpg" alt="A red fox standing in snow" width="320" height="213">
<ol start="3" reversed>
<li>Bronze</li>
<li>Silver</li>
<li>Gold</li>
</ol>
</body>
</html>Note what each part shows:
html,head,body,p,a,ol, andliare containers with end tags.metaandimgare void elements with none.lang,charset,href,title,src,alt,width,height, andstartare attributes with quoted values.reversedis a boolean attribute with no value.- The
fox.jpgfile does not exist unless you add one. The browser will show the alt text instead, which is a useful early look at what alt text is for.
Common errors
The browser's parser is forgiving. It repairs bad markup without telling you, which makes mistakes hard to spot. The most common ones:
- Overlapping elements. Elements must nest completely inside each other. The first block below overlaps. The second is correct.
<p>Read the <a href="/docs/">docs</p></a><p>Read the <a href="/docs/">docs</a></p>- Forgotten end tags. A missing
</li>or</a>can make everything after it become part of the element. If the rest of your page is suddenly a link, look for an uncloseda. - Unquoted values with spaces. In
<img alt=A red fox>, the alt text is justA, andredandfoxbecome meaningless attributes. - Duplicate attributes. If you write
classtwice on one element, the browser keeps the first and ignores the second. - Treating boolean attributes as true/false strings.
reversed="false"is still reversed, because presence alone means true. To turn it off, remove the attribute. - Unescaped ampersands. A URL like
?a=1&b=2inhrefis best written?a=1&b=2. Use<for a literal<in text. - Wrong child elements. A
ulmay contain onlylielements directly. Putting text or anotherulstraight inside it is invalid. Later chapters cover the correct pattern. - Case problems in values. Element and attribute names are not case-sensitive, so
HREFworks. File names are another matter.Fox.jpgandfox.jpgare different files on most web servers, so a link that works on your laptop can break after deployment.
What to carry forward
Keep three habits for the rest of the tutorial. Close every container element, nest fully, and quote every attribute value. Read each new element as "what does it mean, and which attributes does it need?" The a element is next. It needs href, and the choice of URL form is the main decision.
Exercise
This markup has three mistakes. Name them and write the corrected version: <p>See <a href=/docs/ title=User guide>the guide</p></a> <img src="map.png" alt="Campus map"></img>
Show answer
The mistakes are (1) the title value contains spaces and is unquoted, so only User is the value, (2) </p> and </a> overlap, and (3) img is a void element and must not have an end tag. Corrected: <p>See <a href="/docs/" title="User guide">the guide</a></p> <img src="map.png" alt="Campus map">. Quoting href is not required here, but it is the safer habit.
Exercise
A page contains <ol reversed="false">. Does the list count down? How do you make it count up?
Show answer
Yes, it counts down. A boolean attribute is true whenever it is present, whatever its value. Remove the reversed attribute entirely to get normal counting up.
Exercise
You add <h2 id="setup"> in two places on one page and link to #setup. Which heading does the link go to, and what should you do?
Show answer
The browser goes to the first element with that id. Duplicate ids are invalid, so give each heading a unique id, for example setup-linux and setup-windows, and link to the one you mean.