What are HTML elements?

An HTML element is the combination of an opening tag, content, and a closing tag. Elements are the fundamental building blocks of every web page.

Think of HTML elements like containers. You open a container, put content inside, and close it. The browser reads these containers and renders them as text, headings, images, links, buttons, and more.

Key point:
Element = opening tag + content + closing tag. For example: <p>Hello, World!</p> is a paragraph element.

Anatomy of an element

Every HTML element has three parts:

element.html The three parts of an element

The opening tag tells the browser what type of element this is. It can include attributes (extra information). The content is what goes between the tags โ€” it's what the user sees. The closing tag tells the browser the element is finished. The closing tag is always the tag name with a forward slash (/) before it.

Void elements (self-closing tags)

Not all elements have content inside them. Some elements are empty or void elements โ€” they don't wrap any content. These don't have closing tags.

void-elements.html Self-closing elements with no closing tag

Void elements are used when you need to insert something that doesn't have any text content โ€” like an image, a line break, or a form input. You don't close them with a closing tag.

Nesting elements properly

You can put HTML elements inside other HTML elements โ€” this is called nesting. When you nest, remember: tags must close in reverse order. The last tag you opened is the first one you close.

nesting.html Correct vs incorrect nesting

The golden rule: inner tags close before outer tags. Imagine Russian nesting dolls โ€” the smallest doll goes in first, comes out first. If your tags overlap or don't close in the right order, the browser may render the page incorrectly.

Try different element types

Edit the code below to see how different HTML elements render. Try changing the text, adding more elements, or experimenting with <strong>, <em>, and links.

โœ๏ธ Try It Yourself โ€” Element Types
Preview

Block vs inline elements

HTML elements are either block-level or inline. The difference is how they take up space on the page.

Block-level elements take up the full width available โ€” they start a new line and push other content down. Examples: <div>, <p>, <h1>โ€“<h6>, <ul>, <ol>, <section>.

Inline elements only take up as much width as necessary โ€” they stay on the same line as other content. Examples: <span>, <a>, <strong>, <em>, <img>.

โœ๏ธ Try It Yourself โ€” Block vs Inline
Preview
Note:
You can change an element's block/inline behavior using CSS (we'll cover that in the CSS lessons). By default, <div> and <p> are block, while <span> and <strong> are inline.

The <div> and <span> elements

<div> and <span> are generic container elements. They don't have special meaning โ€” they're used to group content and apply styling.

<div> is a block-level container. Use it to group sections of content together โ€” like a wrapper for related paragraphs, images, or other elements.

<span> is an inline container. Use it to apply styling to a small piece of text inside a sentence.

div-span.html Generic containers for grouping content

You'll see <div> and <span> everywhere in real websites. They're the most flexible elements because they can hold any content and be styled in any way.

Common mistakes

  • Forgetting to close a non-void tag โ€” <p>Hello without </p> can cause layout bugs. The browser will try to guess, but you should always close tags explicitly.
  • Overlapping tags โ€” <p><strong>text</p></strong> is invalid. Always close inner tags before outer tags.
  • Using a closing tag on a void element โ€” <br></br> is wrong. Void elements like <br>, <hr>, and <img> don't have closing tags.
  • Forgetting attributes on void elements โ€” <img> without src and alt won't display anything. Always include required attributes.
  • Incorrect nesting with block/inline โ€” You can nest inline elements in block elements, but not the other way around (HTML allows it, but semantically wrong).
Mini Challenge: Build a Structured Page

Task: Create an HTML page with the following structure inside a <div>:

  • An <h2> heading
  • A <p> paragraph with <strong> and <em> text inside
  • An <hr> (horizontal line)
  • Another <p> with a <span> styled with a background color

Make sure the nesting is correct and the page displays properly!

Hint: Start with an opening <div>, then add your elements inside. Remember to close them in the right order. For the styled span, use inline style: <span style="background-color: yellow;">text</span>

Key takeaways

  • An HTML element = opening tag + content + closing tag.
  • Void elements (like <br>, <img>, <hr>) don't have closing tags.
  • Nesting rule: Inner tags close before outer tags. Tags must never overlap.
  • Block-level elements take full width and start a new line (<div>, <p>, headings).
  • Inline elements only take as much width as needed (<span>, <a>, <strong>, <em>).
  • <div> and <span> are generic containers โ€” use them to group and style content.

Test your knowledge

Question 1 of 5
Which of these is a void (self-closing) element?
Question 2 of 5
What is the main difference between block and inline elements?
Question 3 of 5
Which is the correct way to close a nested element?
Question 4 of 5
Which element is inline?
Question 5 of 5
What happens if you forget to close a non-void HTML tag?
0
/5
Great work! You're mastering HTML elements.

Up next

Next lesson: HTML Attributes โ†’ โ€” Learn how to add extra information to elements using attributes like href, src, and class.

Previous lesson: โ† Introduction to HTML

Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You understand HTML elements.
Master the rest of web development.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’