HTML BasicsLesson 4 of 36

HTML Elements

An HTML element is one building block of a page, and most elements have three parts: an opening tag, the content inside it, and a closing tag. The opening tag starts the element, the closing tag ends it, and everything between them is the content those tags control. A paragraph element holds a paragraph, and a heading element holds a heading.

An HTML element is one building block of a page, and most elements have three parts: an opening tag, the content inside it, and a closing tag. The opening tag starts the element, the closing tag ends it, and everything between them is the content those tags control. A paragraph element holds a paragraph, and a heading element holds a heading.

Elements come in sizes. One can wrap a single word, another can wrap a whole page section. When one element sits inside another, that is called nesting. The browser keeps track of how deep the nesting goes and uses that information to lay the page out. Nesting is everywhere in real HTML, so it is worth learning early.

A few elements break the pattern. Void elements like the image tag have no content and no closing tag. They describe themselves with attributes instead. That is not a mistake; it is how those tags are designed, and the browser expects them that way.

Nesting rules

When elements nest, order matters. The innermost tag must close before the tag around it. If you open a paragraph inside a div, you close the paragraph first and the div second. Browsers try to fix broken nesting, but they can guess wrong, and a wrong guess usually means text or layout ends up in the wrong place.

HTML
<div>
  <p>I close first.</p>
</div>

Block and inline elements

Elements fall into two broad groups. Block elements like headings and paragraphs start on a new line and stretch across the full width. Inline elements like links and bold text flow within a line and only take up as much room as their content needs. Knowing which group a tag belongs to explains most of how a page stacks up.

HTML
<p>This line has <strong>bold</strong> and <a href="#">a link</a> inside it.</p>

Syntax

HTML
<tagname>Content goes here</tagname>

Void Elements Self-Close Trap: <div /> Vanishes Silently

Only void elements (img, br, input, meta, etc.) can be self-closed. Writing <div /> or <span /> does NOT self-close - the browser treats '/' as ignored and leaves the tag open, swallowing the next sibling. React devs hit this porting JSX <div /> to plain HTML. GitHub whatwg/html #1142 discusses the parser difference.

HTML
<!-- BAD --><div /><p>this p is INSIDE div!</p>
<!-- GOOD --><div></div>

Good to know

  • An element is an opening tag, content, and a closing tag.
  • Void elements like img have no closing tag.
  • Nested elements close from the inside out.
  • Block elements stack; inline elements flow within a line.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground