HTML Basics

Mastering the standard structure and basic syntax rules of HTML documents is the first step to writing well-formed web pages.

HTML Document Structure

Every HTML document follows a fixed structural framework. The browser uses this framework to understand the components of a page. A complete HTML document contains the following hierarchical relationships:

Example

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
Visible page content
</body>
</html>
▶ Try it Yourself

Structure Breakdown

The various parts of an HTML document are organized in a tree structure called the DOM (Document Object Model). The browser converts each HTML tag into a "node", and nodes form parent-child and sibling relationships.

Understanding this skeleton structure is critical — all tags you learn later will be placed inside either <head> or <body>.

📌 The DOM Concept: The DOM is a programming interface provided by the browser that treats each tag in an HTML document as a "node". <html> is the root node, and <head> and <body> are its child nodes (siblings to each other). A well-structured DOM not only makes code clearer but also helps with SEO and accessibility.

Basic Tag Syntax

The core building block of HTML is the "tag". Browsers identify the type and structure of content through tags. Tags are divided into two categories: paired tags and void tags.

Paired Tags

Most HTML tags come in pairs, wrapping content between an opening tag and a closing tag.

HTML
<p>This is a paragraph</p>
<h1>This is a level-1 heading</h1>
<strong>This is bold text</strong>

Void Tags (Self-Closing Tags)

A few tags have no content and don't need a closing tag. They are called void tags.

HTML
<br>  <!-- line break -->
<hr>  <!-- horizontal rule -->
<img src="/assets/images/tutorials/html-example-photo.webp" alt="A sample photo">  <!-- image -->

In HTML5, the trailing slash on void tags is optional (e.g., both <br /> and <br> are valid), but it's recommended to keep it simple and omit the slash.

Tag Nesting Rules

Tags can be nested within each other, but must follow the "first opened, last closed" principle — the tag opened later must be closed first. Do not cross-nest.

HTML
<!-- Correct nesting -->
<p>This is a <strong>important text</strong> example</p>

<!-- Incorrect nesting (crossed) -->
<p>This is a <strong>important text</p> example</strong>
⚠️ Note

  • Always use lowercase for tag names. Although <P> will render in the browser, lowercase is the industry standard.
  • Nesting must strictly follow hierarchical order. Incorrect cross-nesting can cause the browser to parse incorrectly, resulting in a garbled page layout.
  • Proper nesting is the foundation of a good DOM structure, making maintenance and styling easier.
📌 HTML Comments: For detailed usage of HTML comments (syntax, debugging techniques, common use cases, etc.), we will cover them in Chapter 8: HTML Comments. For now, just know that comments are wrapped in <!-- -->, are not displayed on the page, and are mainly for developer notes.

❓ FAQ

Q Why should I use lowercase for tag names? Uppercase works fine too.
A While browsers are case-insensitive, lowercase is the W3C-recommended standard for three reasons: 1) HTML5 explicitly recommends it; 2) lowercase is more readable in code reviews and team collaboration; 3) XHTML strictly requires lowercase, so staying consistent helps with future migration. Follow the standard so your code works everywhere.
Q What is the DOM, and how does it relate to HTML?
A The DOM (Document Object Model) is the "tree" that browsers build from your HTML code. HTML is your source code; the DOM is the browser's internal representation. You can view it in the Elements panel of DevTools (F12). The DOM matters because JavaScript operates on the DOM — not your HTML file — and CSS selectors match DOM nodes. Understanding the DOM is fundamental to front-end development.
Q Why don't some tags like <br> need a closing tag? When can I omit the slash?
A These are called "void elements" or "self-closing tags" — they don't wrap any content. A line break <br> has no "content of the break," and an image's path is specified via the src attribute, not between tags. In HTML5, the trailing slash is optional (<br> and <br/> are both valid), but keeping it simple without the slash is preferred. Remember: only void elements can omit the closing tag; regular elements like <p> and <div> must have one.

📖 Summary

📝 Exercises

  1. Write a complete page: Create an HTML file with the correct !DOCTYPE, html, head, body structure. In the body, add one h1 heading, one p paragraph, one br line break, and one hr divider.
  2. Nesting practice: Use p and strong tags to write a correctly nested sentence, then deliberately write an incorrectly cross-nested example. Observe how the browser renders the two versions differently.
100%

🙏 帮我们做得更好

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

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