Anatomy of an Element
<p>This is a paragraph.</p>
<!--
^ ^ ^
opening content closing
tag tag
-->Output
Void (Self-Closing) Elements
Some elements have no content and therefore no closing tag. These are called void elements — common examples include <img>, <br>, <hr>, and <input>.
Void Elements
<img src="photo.jpg" alt="A photo" />
<br />
<hr />
<input type="text" />Output
Common Void Elements
| Tag | Purpose |
|---|---|
| <img> | Embeds an image |
| <br> | Inserts a line break |
| <hr> | Inserts a horizontal rule |
| <input> | Creates a form input field |
| <meta> | Provides document metadata |
Nesting Elements
Elements can contain other elements. When nesting, closing tags must close in the reverse order they were opened, like closing brackets.
Correct vs Incorrect Nesting
<!-- Correct -->
<p>This is <strong>very</strong> important.</p>
<!-- Incorrect: tags cross each other -->
<p>This is <strong>very</p></strong>Output
Best Practice
Always close every non-void tag, even if the browser would tolerate leaving it open. It keeps your markup predictable and easy to debug.