HTML Elements
An HTML element is a building block of a web page, usually made of an opening tag, some content and a closing tag, such as <p>Hello</p>. Elements tell the browser what each piece of content is: a heading, a paragraph, a link, a list, an image. The browser turns them into a tree called the DOM, which CSS styles and JavaScript can change.
Anatomy of an element
<a href="/pricing" class="nav-link">Pricing</a>
<!--
<a ...> opening tag (element name "a")
href, class attributes
Pricing content
</a> closing tag
-->
The tag is the markup in angle brackets; the element is the whole thing, tags plus content. Element names are not case-sensitive, but lowercase is standard.
Void elements have no content
Some elements cannot contain anything, so they have no closing tag. These are called void elements. The common ones are <img>, <br>, <hr>, <input>, <meta>, <link> and <source>. You may see them written with a trailing slash, like <br />; in HTML that slash is allowed but ignored. In JSX it is required.
Nesting builds the page structure
Elements can contain other elements. A list holds list items, a list item can hold a link, and a link can hold text. Nested elements must close in the reverse order they opened, like stacking and unstacking boxes.
<!-- Correct -->
<p>Read the <strong><em>whole</em></strong> guide.</p>
<!-- Wrong: tags overlap -->
<p>Read the <strong><em>whole</strong></em> guide.</p>
<ul>
<li><a href="/html">HTML</a></li>
<li><a href="/css">CSS</a></li>
</ul>
Browsers try to repair overlapping tags, but each browser may guess differently, which leads to odd styling and scripts that cannot find the element they expect.
Block-level and inline behaviour
By default, some elements start on a new line and stretch across the available width, while others sit inside a line of text.
| Usually block | Usually inline |
|---|---|
div, p, h1 to h6, ul, ol, li, section, form, table | span, a, strong, em, code, img, label, button |
This is only the default display; CSS can change it. What matters more is the content model, the rules about what may go inside what. A <p>, for instance, cannot contain block elements like <div> or another <p>.
A trap with paragraphs and lists
If you place a <div> or a <ul> inside a <p>, the browser closes the paragraph automatically just before it. Your markup looks fine in the editor, but in the DOM the list ends up outside the paragraph, and CSS selectors like p ul match nothing. In React this shows up as a hydration warning. When a block needs other blocks inside, use a <div> or a semantic container instead of <p>.
A similar trap: putting anything other than <li> directly inside <ul> or <ol>. Wrap extra content inside a list item.
Choosing the right element
Pick elements for what the content is, not how it looks. Use <button> for actions and <a> for navigation, headings for headings, and lists for groups of items. The right element gives you keyboard support, screen reader meaning and search engine understanding for free, and you can always restyle it with CSS. The next lesson on semantic HTML5 elements builds on this idea.
Remember
- An element is opening tag, content and closing tag.
- Void elements like
imgandinputhave no closing tag. - Close nested tags in reverse order.
- Respect the content model; paragraphs cannot hold blocks.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.