On this page

4.2ATX headings

An ATX heading consists of a string of characters, parsed as inline content, between an opening sequence of 1–6 unescaped # characters and an optional closing sequence of any number of unescaped # characters. The opening sequence of # characters must be followed by spaces or tabs, or by the end of line. The optional closing sequence of #s must be preceded by spaces or tabs and may be followed by spaces or tabs only. The opening # character may be preceded by up to three spaces of indentation. The raw contents of the heading are stripped of leading and trailing space or tabs before being parsed as inline content. The heading level is equal to the number of # characters in the opening sequence.

Simple headings:

# foo
## foo
### foo
#### foo
##### foo
###### foo
<h1>foo</h1>
<h2>foo</h2>
<h3>foo</h3>
<h4>foo</h4>
<h5>foo</h5>
<h6>foo</h6>

More than six # characters is not a heading:

####### foo
<p>####### foo</p>

At least one space or tab is required between the # characters and the heading’s contents, unless the heading is empty. Note that many implementations currently do not require the space. However, the space was required by the original ATX implementation, and it helps prevent things like the following from being parsed as headings:

#5 bolt

#hashtag
<p>#5 bolt</p>
<p>#hashtag</p>

This is not a heading, because the first # is escaped:

\## foo
<p>## foo</p>

Contents are parsed as inlines:

# foo *bar* \*baz\*
<h1>foo <em>bar</em> *baz*</h1>

Leading and trailing spaces or tabs are ignored in parsing inline content:

#                  foo                     
<h1>foo</h1>

Up to three spaces of indentation are allowed:

 ### foo
  ## foo
   # foo
<h3>foo</h3>
<h2>foo</h2>
<h1>foo</h1>

Four spaces of indentation is too many:

    # foo
<pre><code># foo
</code></pre>
foo
    # bar
<p>foo
# bar</p>

A closing sequence of # characters is optional:

## foo ##
  ###   bar    ###
<h2>foo</h2>
<h3>bar</h3>

It need not be the same length as the opening sequence:

# foo ##################################
##### foo ##
<h1>foo</h1>
<h5>foo</h5>

Spaces or tabs are allowed after the closing sequence:

### foo ###     
<h3>foo</h3>

A sequence of # characters with anything but spaces or tabs following it is not a closing sequence, but counts as part of the contents of the heading:

### foo ### b
<h3>foo ### b</h3>

The closing sequence must be preceded by a space or tab:

# foo#
<h1>foo#</h1>

Backslash-escaped # characters do not count as part of the closing sequence:

### foo \###
## foo #\##
# foo \#
<h3>foo ###</h3>
<h2>foo ###</h2>
<h1>foo #</h1>

ATX headings need not be separated from surrounding content by blank lines, and they can interrupt paragraphs:

****
## foo
****
<hr />
<h2>foo</h2>
<hr />
Foo bar
# baz
Bar foo
<p>Foo bar</p>
<h1>baz</h1>
<p>Bar foo</p>

ATX headings can be empty:

## 
#
### ###
<h2></h2>
<h1></h1>
<h3></h3>