Markdown Heading Levels: How Many Are Available and How to Choose One
Markdown supports six heading levels. Learn the syntax for each, how to pick the right level for your structure, and how to shift levels safely and check the result.
Markdown gives you six heading levels. They correspond to the HTML heading elements h1 through h6, and the number of hash characters at the start of a line sets the level: one hash for the top level, six for the lowest. This guide covers what each level does to your text, how to decide which level a heading needs, and how to raise or lower heading levels across a file without changing anything else.
What a heading level does to your text
A heading level is structural information, not a font size. When a Markdown processor converts your file, a line such as "## Installation" becomes a second-level heading element. The look of that heading (size, weight, spacing) comes from the stylesheet of whatever displays it, so the same level can look quite different on two platforms. What stays the same is the document outline: tables of contents, screen readers, search tools and anchor-link generators all read the level to work out which sections belong inside which.
The two heading syntaxes
The CommonMark specification, which many current Markdown processors follow, defines two ways to write a heading.
ATX headings start with one to six hash characters, followed by a space and the heading text. This is the only syntax that reaches all six levels. Example:
# Level 1
## Level 2
### Level 3
#### Level 4
##### Level 5
###### Level 6
Setext headings put the text on one line and underline it on the next: a line of equals signs makes a level 1 heading, and a line of hyphens makes a level 2 heading. Setext has no syntax for levels 3 to 6. Example:
Document Title
==============
Section Title
-------------
For most documents, ATX is the more practical choice. It covers every level, each heading sits on a single line, and that makes headings easy to search for and to adjust with a pattern. Setext is valid, but if you plan to change heading levels later, convert Setext headings to ATX first. Otherwise a level-shifting pattern written for hash characters will miss them.
Syntax details that decide whether a line is a heading
Under CommonMark, small differences in characters and spacing change how a line is read. These rules matter when you write headings by hand and again when you edit them with a pattern.
- A space is required after the hashes: "## Setup" is a heading, while "##Setup" is plain paragraph text. Some older or non-CommonMark processors accept the missing space, so do not rely on that.
- Seven or more hashes do not create a level 7 heading. A line such as "####### Notes" is rendered as ordinary text, hashes included.
- Up to three spaces of indentation before the hashes are allowed. Four spaces turn the line into an indented code block.
- A hash escaped with a backslash, as in "# Not a heading", is literal text.
- Closing hashes are optional and are ignored: "## Setup ##" renders the same as "## Setup".
- Lines inside a fenced code block (between ``` or ~~~ fences) are never headings, even when they start with a hash, as comment lines in shell or Python code often do.
How to choose the right level
Base the level on where the section sits in your outline, not on how large you want the text to look. If you need a different look, change the styling rather than picking a level that misrepresents the structure. These conventions are widely followed:
- Use one level 1 heading for the document title. Many style guides and linters treat a second level 1 heading as a warning sign. Some platforms render the page title as level 1 from front matter or a title field; in that case, start the body at level 2. Check how your platform handles titles.
- Use level 2 for the main sections and level 3 for subsections inside them.
- Go down one level at a time. Jumping from level 2 straight to level 4 leaves a gap in the outline that assistive technology and generated tables of contents expose. Linters such as markdownlint include a rule that flags skipped levels.
- Going back up by more than one level is fine. A level 4 heading can be followed directly by a level 2 heading when a new main section begins.
- Treat levels 5 and 6 as a signal. If you often need them, the document may read better split into separate pages or with a flatter structure.
Here is how a typical outline maps to levels:
| Role in the document | Level | ATX syntax |
| Document title | 1 | # |
| Main section | 2 | ## |
| Subsection | 3 | ### |
| Detail within a subsection | 4 | #### |
| Rarely needed deeper detail | 5 | ##### |
| Lowest available level | 6 | ###### |
Shifting heading levels across a file
A common heading-level task is moving every heading down one level, for example when you paste a standalone document into a larger one as a subsection. The reverse move, up one level, comes up when you extract a section into its own file. You can do this by hand, with your editor's find-and-replace, or with a command-line tool. The main choice is between literal and pattern matching. A literal search for "#" also hits hashes inside code, URLs and body text. A pattern anchored to the start of a line, which only accepts valid heading syntax, is much safer. The one thing a simple pattern cannot do is tell whether a line sits inside a fenced code block. A short script that tracks the fences can.
Before you start
- Work on a copy, or make sure the file is under version control, so you can compare the result with the original and undo it.
- Convert any Setext headings to ATX, since the patterns below only match hash-style headings.
- Check whether any heading is already at the limit. A level 6 heading cannot go down another level, and a level 1 heading cannot go up. Decide how to handle those cases before you shift anything.
- If the file came from Windows, it may use CRLF line endings. Each line then ends with an invisible carriage return (\r), which can stop patterns that expect the line to end right after the hashes, such as an empty heading. Normalizing line endings to LF first removes that variable.
Example: demote headings with sed
This example command adds one hash to every ATX heading at levels 1 to 5 and writes the result to a new file, so the original stays untouched. It matches only lines that start with one to five hashes followed by a space, so level 6 headings are left alone instead of becoming invalid 7-hash text. It does not skip fenced code blocks and does not handle indented headings, so use it only on files without hash-prefixed lines inside code.
sed -E 's/^(#{1,5}) /#\1 /' input.md > output.md
To promote instead (remove one hash from headings at levels 2 to 6), this pattern leaves level 1 headings unchanged:
sed -E 's/^#(#{1,5}) /\1 /' input.md > output.md
Example: demote headings while skipping fenced code
This example awk script switches a flag on and off at each line that starts with ``` or ~~~ and only changes heading lines outside fences. It is a simplification: it does not check that the closing fence matches the opening one in character and length, and it ignores indented fences. It uses interval expressions such as {1,5}, which need an awk that supports them, such as GNU awk. The example has not been run against every edge case, so review its output as described below.
awk '
/^(```|~~~)/ { infence = !infence }
!infence && /^#{1,5}( |$)/ { $0 = "#" $0 }
{ print }
' input.md > output.md
Before-and-after example
Illustrative input, where the code comment must stay unchanged:
# Backup Guide
## Requirements
```bash
# run as admin
backup --all
```
### Optional settings
Intended output after demoting by one level:
## Backup Guide
### Requirements
```bash
# run as admin
backup --all
```
#### Optional settings
Note that the plain sed command above would also have changed "# run as admin" to "## run as admin" inside the code block. That is the kind of unintended change the fence-aware version is meant to prevent.
Check the result
- Compare the files line by line, for example with "diff input.md output.md". Every changed line should be a heading, and each should differ only by one hash.
- Count headings in both files with "grep -cE '^#{1,6}( |$)' input.md" and the same for output.md. The counts should match. A drop means a heading turned into plain text, most likely a level 6 heading that gained a seventh hash.
- List the headings in the new file with "grep -nE '^#{1,6}( |$)' output.md" and read through the outline for skipped levels or a missing title.
- Open the new file in a Markdown preview, or run a linter, to confirm that headings render at the expected levels and code blocks look unchanged.
- Only after these checks pass, replace the original file. If something went wrong, delete output.md and start again from the untouched input.
Limits of this guide
The syntax rules here follow the CommonMark specification. Some platforms and older processors behave differently, for example by accepting headings without a space after the hashes or by adding their own extensions, so check the documentation for the renderer you use. The commands are illustrative examples, not tested outputs for every file. No benchmarks or product-specific limits were available for this guide, so it makes no claims about the speed or compatibility of particular editors or tools.