How to Set and Change Heading Levels in Markdown

Learn how Markdown headers work, from one to six hash marks, and how to promote or demote heading levels safely without altering code blocks or other content.

Markdown headers turn a line of plain text into a section title. The number of hash characters (#) in front of the line sets the heading level. This guide covers how levels are written, which details change the result, and how to shift every heading in a document up or down one level without touching other content.

What a heading level does to your text

A heading marker takes a single line as input and changes how that line is interpreted. The text stays the same, but a Markdown renderer outputs it as a heading element (h1 through h6 in HTML) instead of a paragraph. The level tells the renderer, readers and any table-of-contents generator where the section sits in the document's outline. Level 1 is the top of the hierarchy and level 6 is the deepest.

Changing a heading level only changes that line's position in the outline. It does not move, merge or reformat the paragraphs below it. Those paragraphs simply belong to whichever heading comes before them.

Writing headings with the hash symbol

The most common style is the ATX heading. ATX is the name of the older lightweight markup format this syntax comes from. You put one to six # characters at the start of a line, then a space, then the heading text. People often call the # character a "hashtag" sign, but in Markdown it works as a level counter, not a tag.

Markdown | Heading level | HTML element
Title | 1 | h1
Section | 2 | h2
Subsection | 3 | h3
Level four | 4 | h4
Level five | 5 | h5
Level six | 6 | h6

Six is the maximum. In CommonMark and GitHub Flavored Markdown, a line that starts with seven or more # characters is not a heading. It renders as ordinary paragraph text, hashes included.

Details that decide whether a line becomes a heading

  • A space after the hashes is required in CommonMark and GitHub Flavored Markdown. "#Intro" stays plain text, while "# Intro" becomes a level 1 heading. Older implementations, including the original Markdown script, did not require the space, so the same file can render differently depending on the tool.
  • The hashes may be indented by up to three spaces. Four or more spaces turn the line into an indented code block.
  • Optional closing hashes are allowed. "## Setup ##" renders the same as "## Setup", and the number of closing hashes does not have to match the opening ones.
  • To show a literal # at the start of a line, escape it with a backslash. "# not a heading" renders as text that begins with #.
  • A heading occupies exactly one line. Text on the following line starts a new paragraph.
Example (illustrative):

#Intro            <- plain text in CommonMark (no space)
# Intro           <- level 1 heading
   ## Setup       <- level 2 heading (3-space indent is allowed)
    ## Setup      <- indented code block (4 spaces)
## Setup ##       <- level 2 heading; closing hashes are dropped
\# Intro          <- paragraph showing "# Intro"
####### Too deep  <- plain text (more than six hashes)

The underline style for levels 1 and 2

Markdown also supports Setext headings, where you underline a line of text. A row of = characters makes a level 1 heading, and a row of - characters makes a level 2 heading. There is no Setext form for levels 3 through 6, so documents that need deeper levels usually use hashes throughout.

Example (illustrative):

Project Overview
================

Installation
------------

Watch for one pitfall. A line of dashes directly below a paragraph turns that paragraph into a level 2 heading instead of producing a horizontal rule. If you meant a divider, leave a blank line above the dashes.

Changing the level of a single heading

To change one heading by hand, add or remove # characters at the start of its line. Adding one demotes the heading one level deeper (## to ###). Removing one promotes it one level higher (### to ##). A level 1 heading cannot be promoted further, and a level 6 heading cannot be demoted. Adding a seventh hash would turn it into plain text. To convert a Setext heading, delete the underline and put the matching number of hashes in front of the text.

Some editors and editor extensions include commands to promote or demote headings, or to cycle a line through levels. Which commands exist and how they treat edge cases varies by tool, so check the documentation for the editor you use.

Shifting all headings in a document

A common job is to demote every heading by one level, for example when you paste a standalone document under an existing level 1 title. A simple search-and-replace that adds # to every line starting with # is risky. Fenced code blocks often contain lines that begin with #, such as shell comments, Python comments and C preprocessor directives, and those lines must not change.

The awk script below is an illustrative example and has not been verified here. It skips lines inside fenced code blocks (``` or ~~~) and adds one # to ATX headings at levels 1 to 5, leaving level 6 headings unchanged. It writes the result to a new file, so the original stays intact.

awk '
/^ *(```|~~~)/ { in_fence = !in_fence; print; next }
!in_fence && match($0, /^#+[ \t]/) && RLENGTH - 1 <= 5 { $0 = "#" $0 }
{ print }
' input.md > output.md

Promoting works the other way: remove one # from headings at levels 2 to 6. The sed command below is also an illustrative, unverified example. Unlike the awk script, it does not skip code blocks, so use it only on files without fenced code or review every change it makes.

sed -E 's/^#(#{1,5}[ \t])/\1/' input.md > output.md
  • Known limits of both examples:
  • Setext (underlined) headings are not detected or changed.
  • Headings indented by one to three spaces are not matched.
  • The fence detection is simplified. It does not check that a closing fence uses the same character and length as the opening one, and it does not handle fences nested inside lists or block quotes.
  • YAML front matter or HTML blocks that contain lines starting with # are not excluded.
  • The awk script leaves a level 6 heading as it is rather than turning it into plain text, so after demotion a former level 5 and a former level 6 heading end up at the same level.

Checking the result

  • Compare the original and the output with a line-by-line diff, for example "diff input.md output.md". Every changed line should be a heading, and nothing else should differ.
  • Count headings per level before and after the change. The command "grep -E '^#{1,6}[[:space:]]' output.md | cut -d' ' -f1 | sort | uniq -c" gives a rough tally. It also counts matching lines inside code blocks, so treat it as a quick check rather than an exact count.
  • Open the output in a Markdown preview and confirm that code blocks still show their # comments unchanged.
  • Read the outline from top to bottom. Each heading should sit exactly one level below its parent section, with no unintended jumps.

Choosing a sensible heading structure

Widely followed conventions recommend one level 1 heading per document, used as the title, with sections starting at level 2. They also advise against skipping levels, such as jumping from level 2 straight to level 4. These are style and accessibility conventions, not syntax rules. Markdown will render skipped levels without complaint, but screen readers and generated tables of contents depend on a consistent outline.

Gaps in this guide

No tool-specific documentation, version details or test results were provided for this article. The syntax rules above follow the CommonMark specification and GitHub Flavored Markdown. Other Markdown flavors and older processors may differ, especially on the required space after the hashes. The command-line examples are illustrative and were not run, so test them on a copy of your file before relying on them.