Explain it simply
Explain it simply
Picture an element as a small box with a lid. The start tag opens the box and puts a label on it (<p> means "paragraph"), the content is what you put inside, and the end tag closes the lid. You can put small boxes inside a bigger box, but two boxes can never overlap halfway.
Some elements are not boxes at all but single labelled items, such as a line break (<br>) or a picture (<img>). There is nothing to put inside them, so there is no end tag to close.
Problem → Why → Solution
The problem: a button inside a link
Wanting "Order now" to look like a button and act like a link, many beginners put a <button> inside an <a>. It seems to work with a mouse, but a keyboard user's Tab key stops twice on the same spot (once for the link, once for the button). Screen readers may announce a confusing "link, button", and what Enter does depends on which of the two has focus.
<a href="/order">
<button>Order now</button>
</a>Why does this happen?
Every element has a content model: rules for what it may contain. An <a> may not contain interactive elements such as links or buttons. The parser leaves this markup as it is instead of repairing it, so the page renders, but the standard makes no promise about how it behaves. If you want a link, use a single <a> and style it like a button with CSS.
Core concept: element anatomy and nesting
| Piece | In <p>Hello</p> |
|---|---|
| Start tag | <p>: holds the element name and any attributes |
| Content | Hello: text or other elements |
| End tag | </p>: the name with a / in front |
| Element | all three together, one node in the DOM tree |
Putting elements inside one another is called nesting. The rule is simple: the element opened last is closed first. In <p>A <strong>bold</strong> word</p>, the strong sits entirely inside the p. Overlap like <p>A <strong>bold</p></strong> cannot exist in a tree, so the browser has to repair it, and the result may not be what you expected.
Void elements have no content and therefore no end tag: br, img, input, meta, link, hr, source and wbr are among them. HTML accepts a slash, as in <br/>, but simply ignores it. The important catch is that a slash on a non-void element does nothing: <div /> does not close the div, it stays open.
Content model rules restrict nesting too. A <p> may only contain phrasing content, the in-line kind such as <strong>, <em>, <a> and <br>, never block elements such as <div>, <ul> or <h2>. Neither <a> nor <button> may contain another interactive element. Tag names are case-insensitive but are written in lowercase, and end tags that the standard lets you omit, such as </p>, should still always be written: the code is easier to read and less error-prone.
You will also notice that some elements start on a new line and stretch across the page, while others flow inside a line of text. Headings, paragraphs, lists and article are shown as blocks by default; strong, em, a, img and br sit inside the line. This is only the browser's default CSS and can be changed, but it matches the content rules: the in-line elements are the ones allowed inside a paragraph.
- Start tag opens a box
- Content goes inside
- End tag closes the lid
- Void elements: nothing to close
Close the inner box before the outer one; boxes never overlap.
General pattern
<name>content</name>
<outer>text <inner>nested text</inner> more text</outer>
<voidname attribute="value">The first line is a plain element, the second shows nesting with inner closed before outer, and the third is a void element with no end tag.
Example: an article with nesting and a void element
Here all three kinds of element (a container, a nested element and a void element) appear in their proper places. Notice that <br> sits inside a paragraph: it breaks a line within text, so do not use it on loose text outside any element or to add space between blocks.
<article>
<h2>Mohinga</h2>
<p>Mohinga is a <strong>fish noodle soup</strong> often eaten for breakfast.</p>
<p>
Shop hours:<br>
6:00 to 11:00 every day
</p>
</article>Code breakdown
- Line 1
<article>The outermost box. It wraps one self-contained piece of content, here one dish, and every other element is its descendant.
- Line 2
<h2>Mohinga</h2>A plain element with start tag, content and end tag all on one line.
- Line 3
<strong>fish noodle soup</strong>An element nested inside the p. </strong> comes before </p>, so the nesting is correct. strong means "important", and browsers show it bold by default.
- Line 5
Shop hours:<br><br> is a void element: no content and no end tag. It breaks the line inside the paragraph, which suits text like this where the line break is part of the meaning, as in addresses and timetables.
- Line 8
</article>Closes the article last, after every element inside it has been closed.
The heading "Mohinga" appears, followed by a paragraph in which "fish noodle soup" is bold. In the second paragraph, "Shop hours:" is followed by "6:00 to 11:00 every day" on a new line.Reading nesting in DevTools
In the Elements panel, each nesting level is indented one step, and the small triangles fold an element's children away. Hovering over a line highlights that element's box on the page, and the bar at the bottom of the panel lists its ancestors, for example html > body > article > p > strong. When a style refuses to apply, that ancestor bar is the quickest way to check whether the element really is where you think it is.
In real projects
How this is used in real projects
In frameworks such as React and Vue, a component returns a small element tree, and pages are built by nesting components inside each other. The nesting rules therefore still apply: wrap a Card component's <a> in a Button component that renders <button>, and you are back at this lesson's problem. Note that JSX requires the slash on void elements, as in <br />, while in HTML it is optional.
CSS selectors depend on nesting too: article p strong selects "a strong inside a p inside an article". If elements end up in the wrong place, the styles stop applying. In code reviews, "are interactive elements nested?" and "is there a block element inside a p?" are routine items on accessibility checklists.
Common mistakes
Common mistakes
1. Self-closing a non-void element with />
Writing <div class="box" /> out of XML or JSX habit leaves the div open: the HTML parser ignores the slash, and everything after it ends up inside the div. Write <div class="box"></div> with a real end tag.
2. Giving a void element an end tag
End tags such as </br> or </img> do not exist. Oddly, the browser treats </br> as <br>, so you can end up with an extra line break. Write void elements as a start tag only.
3. Putting a block element inside <p>
With <p>Our menu: <ul>...</ul></p>, the browser closes the p before the ul and creates an extra empty paragraph at the end. Give the intro text its own <p> and put the list outside it.
Best practices
Best practices
- Choose an element for what the content means, not for how it looks, and adjust the look with CSS.
- Indent every child element one extra level so nesting mistakes are visible at a glance.
- Write void elements in one consistent style across a project, either <br> or <br />.
Mini challenge
Mini challenge: a dish card
Write an article about tea leaf salad that uses two levels of nesting and a void element in the right places.
- An article containing an h2 heading
- A paragraph with a strong nested inside it
- Another paragraph with an em nested inside a strong (two levels deep)
- An address paragraph that uses <br> for line breaks, with no end tags on it
<article>
<!-- TODO: h2 with the dish name -->
<!-- TODO: paragraph with one strong phrase inside -->
<!-- TODO: paragraph with an em inside a strong -->
<!-- TODO: address paragraph, one line per <br> -->
</article>Expected: You see a heading, a paragraph with a bold phrase, a paragraph with italic text inside bold text, and an address split over three lines.
Show hint
Close the innermost element first: <strong>... <em>...</em> ...</strong>. Then check in the DevTools Elements panel that the tree matches your source.
Show solution
<article>
<h2>Tea leaf salad</h2>
<p>Lahpet thoke mixes <strong>fermented tea leaves</strong> with crunchy beans.</p>
<p>It is served at <strong>almost <em>every</em> celebration</strong> in Myanmar.</p>
<p>
Find it at:<br>
Shwe Pyi Tea House<br>
45 Bogyoke Road, Yangon
</p>
</article>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Element = start tag + content + end tag, one node in the DOM tree.
- Close the last opened element first; tags never overlap.
- Void elements such as br, img and input have no end tag, and /> does not close a non-void element.
- A p holds only phrasing content, and a and button may not contain interactive elements.
HTML Living Standard — HTML Syntax — WHATWG