HTML Elements

Beginner
8 min read
Updated: Nov 2026
6 examples
<tag>…</tag>

Definition and Usage

An HTML element is defined by a start tag, some content, and an end tag. The element is everything from the opening tag through the closing tag.

Start tag, content, end tag

Write <tagname>, put content in the middle, then close with </tagname>. Example: <p>A paragraph.</p> is one paragraph element.

What are nested elements?

Elements can contain other elements. Every HTML page is a nested tree: the <html> root wraps <body>, which wraps headings, paragraphs, and more.

When are elements empty?

Some elements have no content and no end tag—for example <br> (line break) and <img> (image). These are called empty or void elements.

Tag vs element.

A tag is the bracketed name (<p>). An element is the full unit: tags plus content (or a single empty tag).

Never skip the end tag.

Browsers may still display a page if you omit </p>, but results can be wrong. Always close normal elements.

Lowercase is recommended.

HTML is not case-sensitive (<P> works), but write lowercase tags—it matches modern style and XHTML rules.

HTML Element Syntax

The basic form of a normal element:

element.html
<tagname> Content goes here… </tagname>
HTML
<h1>A Heading</h1>
<p>A paragraph.</p>

<!-- Empty element: no content, no end tag -->
<br>

<!-- Attribute in the start tag -->
<a href="https://example.com">Visit Example</a>
Start tagElement contentEnd tag
<h1>A Heading</h1>
<p>A paragraph.</p>
<br>nonenone

Attributes live only in the start tag: name="value". Empty elements never get an end tag.

Core HTML Concepts

Ideas that keep markup valid and readable:

ConceptMeaning
Start + content + endNormal element shape: <p>…</p>
Empty / voidNo content, no end tag (<br>, <img>)
NestingElements inside elements; close inner first
AttributesExtra info in the start tag (href, src, id)
Root document<html> wraps the whole page
Body contentVisible page content lives inside <body>
LowercaseRecommended style for tag names

Tag vs element vs attribute

TagElementAttribute
What it isMarkup markers like <p>Full unit including contentName/value in the start tag
Example<a> / </a><a href="…">Go</a>href="…"
Visible?Not as textContent usually isNot shown as page text

How to Write HTML Elements

Practical habits for clean markup.

  1. Pick the right tag for meaning (<h1> for a main heading, <p> for a paragraph).
  2. Always write matching start and end tags for normal elements.
  3. Nest cleanly: open outer, open inner, close inner, close outer.
  4. Use empty tags only where content is not allowed (<br>, <img>, <hr>).
  5. Put attributes in the start tag with quoted values.
  6. Prefer lowercase tag and attribute names.
GoalPattern
Heading<h1>Title</h1>
Paragraph<p>Text</p>
Line break<br>
Nesting<p>Hi <strong>you</strong></p>
Attribute<a href="url">Link</a>

Live Preview

Switch between a basic element, nested markup, an empty line break, and an attribute example.

Shows rendered output next to the source shape of each element pattern.

Rendered page

A paragraph.

<p>A paragraph.</p>

HTML Examples

Six patterns: basic syntax, nesting explained, empty elements, closing tags, attributes, and a full page.

Example 1 — Basic Element Syntax

HTML
<h1>A Heading</h1>
<p>A paragraph.</p>
Try It Yourself

How It Works

  1. Start tag opens the element (<h1>, <p>).
  2. Content sits between the tags.
  3. End tag closes the element (</h1>, </p>).
  4. Whole unit from start to end is the HTML element.

Example 2 — Nested HTML Elements

HTML
<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>
Try It Yourself

How It Works

  1. html is the root element for the whole document.
  2. body nests inside html and holds visible content.
  3. h1 and p nest inside body.
  4. Close inner first — end tags reverse the open order.

Example 3 — Empty Elements

HTML
<p>This is a<br>paragraph with a line break.</p>
<hr>
<img src="/images/codetofun-logo.png" alt="CodeToFun logo" width="120">
Try It Yourself

How It Works

  1. br inserts a line break with no content and no end tag.
  2. hr draws a thematic break—also empty.
  3. img embeds an image; src and alt are attributes.
  4. Never write </br> or </img>.

Example 4 — Always Close Normal Elements

HTML
<!-- Good: end tags present -->
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>

<!-- Risky: missing end tags (do not rely on this) -->
<!--
<p>This is a paragraph
<p>This is a paragraph
-->
Try It Yourself

How It Works

  1. Closed paragraphs are clear to browsers and tools.
  2. Missing end tags may still “work” in some cases.
  3. Unexpected layout or nesting errors often follow.
  4. Habit: always write the matching end tag.

Example 5 — Attributes Intro

HTML
<a href="https://www.codetofun.com">Visit CodeToFun</a>
<p id="intro" class="lead">Welcome to HTML elements.</p>
<img src="/images/codetofun-logo.png" alt="CodeToFun" width="100">
Try It Yourself

How It Works

  1. Attributes sit inside the start tag only.
  2. href tells the link where to go.
  3. id / class identify or style the paragraph.
  4. src / alt configure the image element.

Example 6 — Complete HTML Page

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>HTML Elements Example</title>
</head>
<body>
  <h1>Welcome to My Website</h1>
  <p>This page is built from nested HTML elements.</p>
  <p>Here is a line break:<br>second line.</p>
  <p>Visit <a href="https://www.codetofun.com">CodeToFun</a>.</p>
</body>
</html>
Try It Yourself

How It Works

  1. Document skeleton nests html → head/body.
  2. Normal elements use start tag, content, end tag.
  3. br shows an empty element inside a paragraph.
  4. a with href shows attributes on a nested element.

Edge Cases & Pitfalls

Most element bugs come from missing end tags, crossed nesting, or treating void tags like normal ones.

close

Missing end tags

Browsers may auto-close, but nested content can attach to the wrong parent.

nest

Crossed nesting

Do not write <p><strong>x</p></strong>. Close in reverse order.

void

End tags on empty elements

</br> or </img> is invalid. Use only the start tag.

attr

Attributes on the end tag

Attributes belong only in the start tag, never in </p>.

case

Mixed-case tag soup

<P> works in HTML, but stick to lowercase for consistency and tooling.

p

Block inside paragraph

A <p> cannot contain another <p> or headings—the browser will split it.

Browser Support

HTML elements (start tags, content, end tags, and void elements) are core HTML and work in all browsers.

✓ Baseline · Widely available

HTML elements

Every browser that parses HTML understands elements, nesting, and empty tags.

100% element syntax
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML elements Universal

Bottom line: Write well-formed elements with matching tags—browsers everywhere will render them.

Key Takeaways

  • Start + content + end defines a normal HTML element.
  • Empty elements like <br> and <img> have no end tag.
  • Nesting builds the whole document tree—close inner tags first.
  • Attributes add name/value data inside the start tag only.

One line: An HTML element is <tag>content</tag> (or a single empty tag), and pages are trees of nested elements with optional attributes on start tags.

Frequently Asked Questions

An HTML element is everything from a start tag through its content to the matching end tag—for example <p>Hello</p>. The element is the whole unit, not just the tags.
A tag is the markup piece such as <p> or </p>. An element is the complete unit: start tag, content, and end tag—or a single empty tag like <br> or <img>.
Empty elements have no content and no end tag. Common examples are <br>, <hr>, <img>, <meta>, and <input>. You write only the start tag (a trailing slash is optional in HTML).
Yes. Elements can contain other elements. All HTML documents are nested trees: html wraps body, body wraps headings and paragraphs, and so on. Close inner tags before outer ones.
For normal elements, always write the end tag. Browsers may guess when it is missing, but that leads to bugs. Empty elements never take an end tag.
Attributes add extra information in the start tag, usually as name="value" pairs—for example <a href="https://example.com">. They customize how the element behaves or is identified.

Practice in the editor

Open a Try It demo and build elements with tags, nesting, and attributes.

Open Try It editor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful