Elements, Attributes and Nesting
How tags form elements, how attributes configure them, which elements are void, and what the parser does with boolean attributes, duplicates and bad nesting.
Elements, Attributes and Nesting
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- An element is a start tag, content and an end tag. `<p>Hi</p>` is one element made of two tags. Void elements such as `<br>`, `<img>` and `<input>` have no content and no end tag.
- Attributes sit in the start tag as name and value pairs. Names are case-insensitive, and in a test `ID=Z` came out as `id`. Quote values, because an unquoted value ends at the first space.
- A boolean attribute is true when present, whatever its value. In a test `<input disabled=false>` was disabled. To turn it off, remove the attribute.
- `<div/>` is not self-closing in HTML. The slash is ignored, so `<div/>after` produced a `div` that swallowed the text after it.
- Elements must nest, not overlap. Some combinations are repaired by the parser: an `<a>` inside an `<a>` becomes two sibling links, and a `<div>` inside a `<p>` closes the paragraph.
Tags build elements
You write tags. The browser builds elements. <p>Hello</p> is a start tag, some content and an end tag, and together they make one paragraph element. The distinction matters because the tree has elements in it, not tags, and CSS and JavaScript talk about elements.
Some elements have no content, so they have no end tag. These are void elements: <br>, <hr>, <img>, <input>, <meta>, <link> and a few others. You write <img src="a.png" alt=""> and you’re done. You can write a trailing slash (<br />) and it’s harmless on those, but it’s a habit from XHTML, not a requirement.
The slash does not work on other elements. In a test with headless Chromium 159, the markup <br/><div/>after produced <br><div>after</div>: the parser ignored the slash, opened a div, and the text became its child. If you want an empty div, write <div></div>.
Attributes configure an element
Attributes live in the start tag as name="value" pairs: href on a link, src and alt on an image, class and id on anything. A few rules the parser applies, each checked in a test:
- Names are case-insensitive.
<input ID=Z TITLE='q'>gave attribute namesidandtitle. Write them lowercase anyway. - Quotes are optional in simple cases, but a value without quotes ends at the first space.
<p class=a b>gaveclass="a"and a second attribute namedb. If a value could contain a space, quote it. - Duplicates: first one wins.
<input class=a class=b>ended with the class lista. - Boolean attributes are about presence.
disabled,checked,required,hidden,open.<input disabled=false>produced an input whosedisabledproperty wastrue. The value is ignored, so the only way to turn it off is to remove the attribute. This is the single most common attribute mistake in generated markup.
There are two kinds of attributes: those specific to an element (href on <a>) and global attributes that work on every element (class, id, lang, title, hidden, tabindex, data-*). The MDN page for global attributes is the list to know. The video spends a good part of its time showing you how to read MDN to find both kinds, which is the most reusable skill in this lesson.
Nesting: elements must not overlap
Elements nest like boxes. A box must close before the box around it does. <b><i>text</i></b> is correct. <b><i>text</b></i> overlaps, and the parser fixes it: in the test the output was <b><i>text</i></b>.
Other combinations are not allowed by the content model and get repaired in ways that surprise people:
- A
<div>inside a<p>closes the paragraph first, leaving a stray empty<p>when the original closing tag arrives (covered in the first lesson). - An
<a>inside an<a>: the markup<a href=1>x<a href=2>y</a></a>produced two sibling links,<a href="1">x</a><a href="2">y</a>, so the nested link did not nest. - A
<li>can omit its end tag, and<ul><li>a<li>b</ul>produced two correct list items. That’s allowed by the spec, but write the end tags anyway.
Indentation doesn’t change the tree at all. It only helps humans see the nesting, which is why the deeper video on nesting spends a chapter on it.
Old elements to avoid
Anything that styles rather than describes is the wrong tool. <center>, <font> and <marquee> are deprecated in MDN’s compat data, and the border attribute on <img> is too. Browsers still render them, but CSS does the job. What replaced them is covered in the CSS roadmap.
What the video covers
The embed is The Common Coder’s “Master HTML Attributes and Elements in 13 Minutes!” (July 2024, about 13.5 minutes). Its chapters are HTML tags and elements (0:46), HTML attributes (3:01), the MDN Web Docs (5:04), documentation on HTML attributes and elements (6:48), documentation on HTML global attributes (9:33) and final thoughts (12:18). It is a small channel, so it is a pick on fit rather than reputation. I could not read a transcript, so I describe it only from the chapters and description. The void-element, boolean-attribute, duplicate-attribute and bad-nesting results above are this lesson’s own, from a browser run. The deeper video is Kevin Powell’s six-minute nesting lesson.
Where this fits
This is the vocabulary for the rest of the roadmap. The previous lesson, anatomy of an HTML document, supplied the skeleton. Next come the first real elements, in headings, paragraphs and text semantics.
Check yourself
What is the difference between a tag and an element?
A tag is the piece of markup, `<p>` or `</p>`. An element is the whole thing: start tag, content and end tag, or just the start tag for a void element. The parser builds elements from tags.
A button has `disabled="false"` in the markup. Is it enabled?
No. Boolean attributes are true when present, so `disabled="false"` is still disabled. In a test the `disabled` property was true. Remove the attribute to enable the button, or write `disabled` with no value to be clear.
You write `<div class="a" class="b">`. Which class does the element have?
Only `a`. The parser keeps the first occurrence of a duplicated attribute and drops the later ones, which is easy to trigger when two code paths each add a `class` to the same tag.
Why is writing `<div />` a bug?
HTML ignores the trailing slash on non-void elements, so it opens a `div` that stays open. Everything after it becomes its content until something closes it. Only void elements like `br` and `img` may omit the end tag.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.