Markdown: Markdown Heading Syntax and Hierarchy Guidelines

Headings are the skeleton of your document — they tell both readers and search engines how your content is organized.

1. What You'll Learn


2. A Document Maintainer's Real Story

(1) Pain Point: Chaotic Heading Hierarchy

Sarah took over a technical blog project and found that past articles used headings haphazardly — some used #, some used ##, some had no headings at all, and others jumped from H1 straight to H3, skipping H2 entirely. As a result, the site's table of contents generator was completely broken, and readers complained they couldn't find content.

(2) Solution: Standardized Heading Rules

Sarah established heading rules: each article has only one # heading, and headings must progress step by step (## → ### → ####) without skipping levels. She used a script to fix all 50 articles in bulk. After the fix, the auto-generated table of contents worked again, and reader time-on-page increased by 40%.


3. Two Heading Syntaxes

Markdown provides two heading syntaxes:

100%
graph TB
    A[Markdown Headings] --> B[ATX Style]
    A --> C[Setext Style]
    B --> D[# through ######]
    B --> E[Most common]
    C --> F[=== and ---]
    C --> G[H1 and H2 only]
Syntax Notation Supported Levels Best For
ATX # through ###### H1–H6 All scenarios, most universal
Setext === / --- H1, H2 only Niche editor preference, poor compatibility

ATX style uses the number of # symbols to indicate heading level — one # for H1, two ## for H2, and so on:

MARKDOWN
# Heading Level 1 (H1)
## Heading Level 2 (H2)
### Heading Level 3 (H3)
#### Heading Level 4 (H4)
##### Heading Level 5 (H5)
###### Heading Level 6 (H6)
💡 Tip: There must be a space after the # before the heading text; otherwise, some parsers won't recognize it as a heading.

(2) Setext Style

Setext style places === or --- under the heading text:

MARKDOWN
Heading Level 1
=======

Heading Level 2
-------
⚠️ Note: Setext style only supports H1 and H2. It works fine in GitHub and other GFM parsers but may not be supported by some niche parsers. Only use it when compatibility is assured and you want stylistic variety.

▶ Example: Comparing the Two Heading Styles

MARKDOWN
# ATX Style H1
ATX Style H2
============

Note: the line with === underneath renders as an H1, even though the text says "H2".

4. Heading Hierarchy Guidelines

(1) Using Hierarchy Correctly

Document headings should have a clear hierarchy, like the table of contents of a book:

MARKDOWN
# Document Title (only one H1)
## Chapter 1 (H2)
### 1.1 Section (H3)
#### 1.1.1 Subsection (H4)
### 1.2 Section (H3)
## Chapter 2 (H2)
⚠️ Note: Don't skip levels! Going from H2 straight to H4 breaks the outline structure. If your content doesn't need an H3, staying at H2 → H2 is perfectly fine.

(2) Impact on SEO and Accessibility

Heading hierarchy matters a lot for SEO and screen readers:

Aspect Recommended Avoid
H1 count One per page Multiple H1s confuse search engines
Keywords H1 contains core terms, H2 contains related terms Keyword stuffing
Hierarchy Step by step, no level skipping Chaotic H1→H3→H2 jumps
Length H1 ≤ 60 chars, H2 ≤ 40 chars Entire paragraphs as headings

▶ Example: Correct vs. Incorrect Heading Hierarchy

MARKDOWN
✅ Correct:
# CSS Layout Tutorial
## Flexbox
### Flex Container Properties
### Flex Item Properties
## Grid
### Grid Container Properties

❌ Incorrect:
# CSS Layout Tutorial
### Flex Container Properties (skipped H2)
## Flexbox
#### Flexbox Properties in Depth (H3→H4 awkward jump)
## Grid
💡 Tip: Think of H1 as a book title, H2 as chapter names, and H3 as sections within a chapter — this analogy helps you maintain a natural hierarchy.


5. Formatting and Special Characters in Headings

(1) You Can Use Bold, Italic, and Code in Headings

MARKDOWN
## Installing Dependencies With `npm install`
## Understanding **flex-grow**, **flex-shrink**, and **flex-basis**
## What Is *Responsive Design*?

(2) Avoid Overly Long Heading Content

MARKDOWN
❌ Avoid:
## A Detailed Tutorial on How to Use Python's requests Library to Send HTTP Requests

✅ Recommended:
## Sending HTTP Requests With the requests Library
💡 Tip: Headings get truncated in tables of contents and search results. Keep them short and clear so readers know what the section is about at a glance.

▶ Example: Before and After Heading Optimization

MARKDOWN
❌ Too long:
## This Article Will Teach You How to Set Up a Python Development Environment in VS Code on Windows

✅ Optimized:
## Setting Up Python in VS Code
💡 Tip: Put detailed explanations in body paragraphs; keep only the core keywords in headings.


6. Full Example: Heading Structure of an Article

MARKDOWN
# Data Analysis With Python

## 1. Data Preparation
### (1) Importing Libraries
### (2) Reading Data
### ▶ Example: Reading a CSV File

## 2. Data Cleaning
### (1) Handling Missing Values
### ▶ Example: Filling Null Values
### (2) Removing Duplicates

## 3. Data Visualization
### (1) Line Charts
### ▶ Example: Plotting a Trend Chart
### (2) Bar Charts

Expected result: A clearly layered document structure that both readers and search engines can quickly understand.


❓ FAQ

Q Can an article have multiple H1s?
A Technically yes, but strongly discouraged. An article should have only one H1 (usually the title). Multiple H1s confuse search engines about what the main content is.
Q Should I add a period at the end of a heading?
A No. Headings aren't complete sentences, so don't end them with punctuation. FAQ questions can end with a question mark since they are questions.
Q Is the space between # and the heading text required?
A Yes, it's required. #Title won't be recognized as a heading — it will be treated as plain text. # Title is the correct way.
Q Can I use non-English characters in headings?
A Yes. However, URL anchors are generated in English. If you need stable anchors, add a custom ID after the heading like {#custom-id}.
Q H5 and H6 are rarely used. Do they matter?
A Yes. They're useful in deeply nested technical documents (legal clauses, API parameter descriptions). For regular articles, going up to H3 or H4 is usually enough.

📖 Summary


📝 Exercises

  1. Beginner: Write a short Markdown piece with H1, H2, and H3 headings (choose the topic yourself — a reading note or study plan). Make sure each heading level has exactly one more # than the level above it.

  2. Intermediate: Open a document you recently wrote and check if its heading hierarchy follows the rules. If there are level skips or messes, fix them. Then count how many H1s you have (the correct answer is 1).

  3. Challenge: Use the Markdown All in One extension in VS Code to generate a table of contents (type [TOC] or use the command) and verify your heading hierarchy is correct. If the generated TOC looks off, your heading levels need adjusting.

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%

🙏 帮我们做得更好

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

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