Headings define the document outline
Markdown supports six heading levels using # (ATX-style). Headings are not decoration — they are the document outline. Screen readers, table-of-contents generators, and search engines all depend on them.
Hard rules
- A space is required after the
#characters.#headingwith no space is not a heading in CommonMark. - You can optionally close headings:
## Section ##. The closing hashes are stripped. - Up to 6 levels — anything past
######is just literal hashes.
Best practice
Use # H1 exactly once per document — that is your title. Structure the rest with ## for major sections and ### for subsections. Do not skip levels (H2 → H4 confuses screen readers and breaks auto-generated TOCs).
Setext-style — the alternate H1/H2 syntax
Underline text with = for H1 and - for H2. Older syntax, still valid, but ATX is more common because it works for all six levels and is easier to skim in source.
Trap: Some Markdown editors render
#heading (no space) as a heading, but a CommonMark-compliant renderer (GitHub, pandoc, etc.) will not. Test on the platform you ship to.