Code Bytes · "Web Fundamentals" · Episode 4 · HTML
Tags and elements
An HTML element is an opening tag, its content and a closing tag together. Think of it as a box. Elements can go inside other elements, which is called nesting, and the inside one is closed first. The strong element marks text as important, and browsers show it in bold.
Tags, then elements
Last time we wrote tags: an opening tag like <p>, and a closing tag with a slash, like </p>.
Put the opening tag, the content and the closing tag together, and you get an element:
``html <p>Hello</p> ``
<p>is the opening tagHellois the content</p>is the closing tag
All three together are one p element (a paragraph). People often say "tag" when they mean the whole element, but strictly the tags are just the two labels at the ends.
Think of it as a box
An element is like a box. The opening tag opens it, the closing tag shuts it, and the content sits inside.
Boxes inside boxes: nesting
Boxes can go inside boxes. Here a strong element sits inside a p element:
``html <p>She is <strong>very</strong> sleepy.</p> ``
The strong element is inside the paragraph. That's called nesting. strong means important, so the browser shows "very" in bold:
She is very sleepy.
Close the inside box first
When elements are nested, close the inside one first, then the outside one. In the line above, </strong> comes before </p>.
```html <!-- right: inner first --> <p>She is <strong>very</strong> sleepy.</p>
<!-- wrong: closed in the wrong order --> <p>She is <strong>very</p> sleepy.</strong> ```
Browsers try to fix mistakes like the second line, but the correct way is always inner first.
Try it yourself
- Open a plain text editor (Notepad on Windows, TextEdit in plain-text mode on a Mac) and type:
``html <p>My room is <strong>tiny</strong> but cosy.</p> ``
- Save it as
room.htmland open it in your browser. "tiny" shows in bold. - Now move the
strongtags so they go aroundcosyinstead, save, and refresh. The bold word moves with the box.
Good to know
- Some elements have no content and no closing tag. They're called void (or empty) elements, for example
<br>for a line break and<img>for an image. - Opening tags can also hold extra settings called attributes, like where a link goes. They come later.
- Bold is the browser's default look for
strong. CSS can change how it looks; the tag still says the text is important. - The box comparison is ours, not an official definition.
Answer to the comment question
How many elements are in this line?
``html <p>I like <strong>cats</strong> and <strong>dogs</strong>.</p> ``
3 elements: one p element (the whole line) and two strong elements (around "cats" and around "dogs"), both nested inside the p. The line has 6 tags, because each element has an opening tag and a closing tag.
Test your knowledge
Quick quiz: Tags and elements
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- MDN Web Docs, "Basic HTML syntax": an element is an opening tag, its content and a closing tag; elements can be nested inside other elements and must be closed in the right order
- MDN Web Docs, "The Strong Importance element": strong marks text as important, and browsers show it in bold by default
- MDN Web Docs, "The Paragraph element": the p element is a paragraph
- MDN Web Docs, "Void element": some elements, such as br and img, have no content and no closing tag
Video transcript
Everything on a web page lives inside tags. So what's an element?
Last time, we wrote tags: an opening tag, and a closing tag with a slash.
Put the opening tag, the content and the closing tag together, and you get an element.
Think of it as a box. The opening tag opens it, the closing tag shuts it, and the content sits inside.
Boxes can go inside boxes. We type a P tag, the words She is, then a strong tag around very, and close both.
The strong element sits inside the paragraph. That's nesting. Strong means important, so very shows in bold.
Close the inside box first, then the outside one.
How many elements are in this line? Comment your answer below!
That's your byte.
More from "Web Fundamentals"
- "Web Fundamentals" EP1What happens when you open a website
- "Web Fundamentals" EP2Front end vs back end