HTML: HTML Elements

Last updated: 2026-09-08

Understand the difference between block-level and inline elements, and master the correct way to nest tags.

Tags in this section: <html> <body> <head> <dl> <article> <p> <dt> <aside> <fieldset> <dd> <footer> <hr> <header> <table> <ol> <nav> <div> <ul> <main> <form> <li> <section> <a> <ins> <b> <del> <strong> <button> <em> <input> <i> <img> <small> <span> <sub> <sup> <h1>

1. What Are HTML Elements

In HTML, an element refers to the complete structure from the opening tag to the closing tag, including both the tags themselves and the wrapped content. The browser parses each element into a node in the DOM tree, forming the page's hierarchical structure.

A complete HTML element consists of three parts:

▶ Example

▶ Try it Yourself

However, not all elements contain content. Some elements have no content and do not need a closing tag; they are called void elements.

▶ Example

▶ Try it Yourself

The key difference: regular elements have an opening tag, content, and a closing tag; void elements only have an opening tag (usually carrying attributes), with no content and no closing tag.

📌 Elements and DOM Nodes: In the DOM structure, each element corresponds to a node. <html> is the root node, with <body> and <head> as its child nodes. The nesting relationships between elements determine the parent-child and sibling relationships between nodes. You can revisit the HTML Basics chapter to review the DOM concept.


2. Block-level Elements vs Inline Elements

HTML elements are divided into two categories based on how they arrange on the page: block-level elements and inline elements. Understanding their differences is the first step to mastering page layout.

(1) Block-level Elements

Block-level elements are characterized by occupying the full width of their parent. No matter how narrow their content, they always take up the full row width, and elements before and after them will appear on separate lines.

Common block-level elements are shown in the table below:

Tag Description Tag Description Tag Description
<h1>~<h6> Headings 1-6 <dl> Description list <article> Article
<p> Paragraph <dt> Description term <aside> Sidebar
<fieldset> Field group <dd> Description details <footer> Footer
<hr> Horizontal rule <header> Header <table> Table
<ol> Ordered list <nav> Navigation <div> Block container
<ul> Unordered list <main> Main content <form> Form
<li> List item <section> Section

▶ Example

▶ Try it Yourself

(2) Inline Elements

Inline elements are characterized by not occupying a full row. They only take up the width of their content and are arranged left to right within a line, automatically wrapping to the next line when the width is insufficient.

Common inline elements are shown in the table below:

Tag Description Tag Description
<a> Hyperlink <ins> Inserted text
<b> Bold <del> Deleted text
<strong> Important text <button> Button
<em> Emphasized (italic) <input> Form input
<i> Italic <img> Image
<small> Small text <span> Inline container
<sub> Subscript <sup> Superscript

▶ Example

▶ Try it Yourself

(3) Core Differences Between the Two

▶ Example

▶ Try it Yourself
📌 Key Rules:

  • Block-level elements can contain inline elements and other block-level elements.
  • Inline elements can only contain inline elements (or text); they cannot directly contain block-level elements.
  • Exception: In HTML5, the <a> tag can wrap block-level elements, such as turning an entire card into a clickable link.

3. Element Nesting and Hierarchy

HTML elements can wrap one another like Russian nesting dolls. This relationship is called nesting. The nesting structure determines the hierarchical relationships of elements in the DOM tree.

(1) DOM Tree Node Relationships

When elements are nested within each other, three types of relationships form between them:

The following nesting structure clearly shows the hierarchical relationships between elements:

▶ Example

▶ Try it Yourself

The corresponding DOM tree structure is as follows:

PLAIN
body
├── h1 ("Page Title")
└── p
├── text: "This paragraph contains "
├── strong
—   └── text: "emphasized text"
└── text: " within it."

(2) Correct Nesting vs Incorrect Nesting

Nesting must follow the "last in, first out" (LIFO) principle — the innermost element opened first must be closed last. If tags are closed in a crossed manner, it results in incorrect nesting:

▶ Example

▶ Try it Yourself
⚠️ Nesting Precautions:

  • Inline elements (e.g., <span>, <strong>) cannot nest block-level elements (e.g., <div>, <p>).
  • The <p> tag cannot nest other block-level elements inside it, such as <div> or another <p>.
  • Deep nesting, while syntactically valid, reduces readability and maintainability. It's recommended to keep nesting within 3~4 levels.
  • Use the browser's "Inspect" feature (F12) to visually explore the DOM tree structure — an excellent learning method.

Tags in this section: <html> <body> <head> <dl> <article> <p> <dt> <aside> <fieldset> <dd> <footer> <hr> <header> <table> <ol> <nav> <div> <ul> <main> <form> <li> <section> <a> <ins> <b> <del> <strong> <button> <em> <input> <i> <img> <small> <span> <sub> <sup> <h1>

❓ FAQ

Q What's the difference between block-level and inline elements? When should I use each?
A The key difference is whether they start a new line. Block-level elements occupy a full row — like a paragraph — and are used for page structure (headings, sections, containers). Inline elements don't break the flow — like a word — and are used for styling text within content (bold, links, emphasis). In practice, 90% of layout uses block-level elements (div, section, article); inline elements (strong, em, a) are only for text-level styling.
Q Why can't inline elements contain block-level elements?
A This is an HTML semantics requirement. Block-level elements represent "an independent content section," while inline elements represent "part of the text flow." Putting a block inside an inline is like stuffing a paragraph into a word — it makes no semantic sense. Browsers may tolerate it, but it creates messy DOM structure and breaks CSS styling. The only exception in HTML5 is the <a> tag, which can wrap block-level elements to make an entire card clickable.
Q <strong> and <b> both create bold text, <em> and <i> both create italic — what's the difference?
A The difference is semantics. <strong> means "this text is important," and <em> means "this text needs emphasis" — they convey meaning. <b> just "makes text bold," and <i> just "makes text italic" — they only affect appearance. Search engines and screen readers recognize the semantics of <strong> and <em> but ignore <b> and <i>. Rule of thumb: use <strong>/<em> for meaningful emphasis; use <b>/<i> or CSS for purely visual styling.

📖 Summary

📝 Exercises

  1. Block vs Inline Experiment: Create a page using two <div> (block-level) and two <span> (inline) with the same content. Add a border style style="border:1px solid red" and observe how they arrange differently in the browser.
  2. DOM Tree Exercise: Write out the DOM tree structure for the following code (draw the tree diagram manually): <div><h2>Title</h2><p>Text<strong>Emphasis</strong>End</p></div>
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏