How to Set and Change Heading Levels in Markdown

How Markdown headings work, from H1 to H6, and how to raise or lower heading levels without changing code blocks, hashtags or other content.

What a heading level is and what changing it does

A Markdown heading is a line of plain text marked as a section title. Its level, from 1 to 6, sets its rank in the document outline. When the file is converted to HTML, a level 1 heading becomes an h1 element, a level 2 heading becomes h2, and so on down to h6. Changing a heading's level changes only that rank, which affects how the heading displays and where it sits in a table of contents. The heading text and the content under it stay the same.

There are two kinds of level change. You can edit a single heading by hand, or you can shift every heading in a file at once. A file-wide shift is usually needed when one document is pasted into another, such as a README becoming a chapter. The main risk of a file-wide shift is touching lines that only look like headings, so most of this guide covers how to avoid that.

Writing headings with hash marks (ATX style)

The common syntax, called ATX style, puts one to six hash characters (#) at the start of a line. The number of hashes is the level. Example:

# Level 1 heading
## Level 2 heading
### Level 3 heading
#### Level 4 heading
##### Level 5 heading
###### Level 6 heading
Markdown prefix | HTML element | Typical use
| h1 | Document title
| h2 | Main sections
| h3 | Subsections
| h4 | Deeper subsections
| h5 | Rarely needed
| h6 | Deepest level available

These rules come from the CommonMark specification. Most modern renderers follow it, but older or custom renderers may differ:

  • Put a space or tab after the hashes. Under CommonMark, "#Title" is a plain paragraph, not a heading. Some older Markdown processors accept it anyway, which is why it may render differently in different tools.
  • Use at most six hashes. A line starting with seven hashes is plain text.
  • You may indent the line by up to three spaces. Four or more spaces turns it into an indented code block.
  • You may add closing hashes, as in "## Title ##". They are optional and do not affect the level.
  • To start a normal line with a literal hash, escape it with a backslash, as in "# not a heading".

Watch for invisible characters. The space after the hashes must be an ordinary space (U+0020) or a tab. A non-breaking space (U+00A0), which can come in when you paste from a word processor or web page, looks the same on screen but is not treated as the required whitespace under CommonMark. If a heading refuses to render, check that character first. Example, with the invisible character written out:

## Works            <- hashes + U+0020 SPACE + text
##[U+00A0]Fails    <- hashes + NO-BREAK SPACE + text (renders as a paragraph)

The underline alternative (Setext style)

Markdown also allows headings marked by a line underneath the text. A row of equals signs makes a level 1 heading. A row of hyphens makes a level 2 heading. This style has no form for levels 3 to 6. Example:

Document Title
==============

Section Title
-------------

A row of hyphens directly under a line of text makes that line a level 2 heading. If you meant a horizontal rule, put a blank line above the hyphens. If you plan to adjust heading levels later, use ATX style throughout. It handles all six levels the same way, and simple find-and-replace methods can match it.

Changing the level of a single heading

To promote a heading (raise its rank), remove one hash. To demote it (lower its rank), add one hash. For a Setext heading, change between = and - to switch between levels 1 and 2. For any deeper level, rewrite the heading in ATX style. Example:

Before:  ### Installation
After:   ## Installation      (promoted from level 3 to level 2)

Then check the headings below it. If a level 3 heading becomes level 2, its level 4 children usually need to move up too, or the outline will skip a level.

Shifting every heading in a document

A file-wide shift adds or removes the same number of hashes on every real heading. Many lines start with # without being headings. A safe shift leaves all of these alone:

  • Lines inside fenced code blocks (between ``` or ~~~ fences), such as shell comments or Python comments.
  • Lines inside indented code blocks.
  • Escaped hashes such as #.
  • Hashtags or issue numbers like #42 at the start of a line. These are not headings under CommonMark because no space follows the hash.
  • Heading text itself, including capitalization, punctuation and any trailing closing hashes.

Example of a correct demotion by one level. The heading lines change, and the comment inside the code fence does not:

Before:
# Setup
## Requirements
```bash
# install dependencies
npm install
```

After:
## Setup
### Requirements
```bash
# install dependencies
npm install
```

Levels stop at 6. If you demote a document that already uses level 6 headings, those headings have nowhere to go. Decide how to handle them before running a bulk change, for example by restructuring that part of the document.

Using Pandoc

Pandoc, a document converter, has a documented option for this: --shift-heading-level-by. Positive numbers demote headings and negative numbers promote them. Example command (not tested against your file):

pandoc input.md -f markdown -t markdown --shift-heading-level-by=1 -o output.md

Pandoc parses the document and writes it out again, so code blocks are recognized correctly. The drawback is that the rewrite can also change formatting you did not ask to change, such as list markers, line wrapping, emphasis characters or heading style. Choose the input and output format names that match your Markdown flavor. Check Pandoc's documentation for how it treats headings shifted above level 1 or below level 6. Then compare the output with the original before replacing the file.

Using a line-based script

A small script changes only the lines it matches and leaves all other formatting alone. The awk command below is an illustrative example, not verified output. It demotes ATX headings of levels 1 to 5 and skips lines between fence markers:

awk '/^(```|~~~)/ { infence = !infence }
     !infence && /^#{1,5}[ \t]/ { $0 = "#" $0 }
     { print }' input.md > output.md

This script has known limits. It does not match headings indented by one to three spaces. It ignores Setext headings. It can misjudge where a fence ends when a block uses longer fences or mixes ``` and ~~~. Some awk implementations do not support interval expressions like {1,5}. A plain sed substitution has the same matching logic with no fence handling at all, so it would also add a hash to comments inside code blocks.

How many H1 headings to use

Markdown allows any number of level 1 headings. A common style convention is to use exactly one H1 as the document title and build the rest of the outline from H2 down, without skipping levels. Some platforms already display the page title as an H1. In that case, starting the file at level 2 avoids having two titles. Check the conventions of the site or tool that will display your file.

Verifying the result

  • Compare the original and changed files with a diff tool. The only changed lines should be heading lines, and on each one only the hashes should differ.
  • Open every code block in the diff and confirm none of its lines changed.
  • Count the headings at each level before and after. With a uniform shift, each count should simply move one level.
  • Render the file in the tool where it will be published. Confirm that the outline or table of contents has no skipped levels.
  • If other pages link to headings in this file, test those links. Anchor generation depends on the renderer.

Gaps in this guide

This guide covers general CommonMark behavior, one documented Pandoc option and an illustrative script. It does not cover editor-specific heading shortcuts or plugins, or how any particular publishing platform handles headings, because no confirmed details about them were available. Before relying on those behaviors, test them in the renderer you actually use.