Articles
Published · November 20, 2026

Markdown Indented Code Blocks: When Four Spaces Mean Code

Four spaces or a tab after a blank line turn Markdown lines into code. Here is how the rule works, why it breaks inside lists, and when a fence is safer.

Also in: Español中文हिन्दी

The short answer

In Markdown, a line indented by four or more spaces (or one tab) becomes code, as long as a blank line comes before it. No backticks needed. The catch: the same indentation also shows up in lists and pasted text, so it can turn into code when you didn't mean it to.

The four-space rule

The original Markdown syntax page puts it in one line: to make a code block, indent every line by at least 4 spaces or 1 tab. The block runs until a line that is not indented. Inside it, ampersands and angle brackets are escaped for you, and other Markdown, such as asterisks for emphasis, is left alone.

The CommonMark spec tightens this into a definition. An indented code block is one or more chunks. A chunk is a run of non-blank lines, each starting with four or more spaces. Four spaces are stripped, and everything after them is literal:

    a simple
      indented code block

That renders as a code block whose first line is a simple and whose second line keeps its two extra spaces. Blank lines between chunks stay inside the same block, and any indentation beyond the four-space prefix is kept.

The block ends at the first non-blank line with fewer than four spaces. Put a paragraph straight after the code and it is a paragraph again, with no blank line needed in between.

Why a blank line has to come first

An indented code block cannot interrupt a paragraph. Take this input:

Foo
    bar

You might expect bar to be code. It is not. CommonMark treats it as a continuation of the paragraph, so you get one paragraph containing both words. Add a blank line after Foo and the second line becomes code.

This is the usual reason an indented snippet shows up as plain text. If your code block isn't rendering, look for a missing blank line first.

Code blocks inside list items

Lists are where four spaces stop being simple, and it's the part that trips most people. A list item's content starts after the marker and its spacing, and every block that belongs to the item must be indented to that point. A code block needs four more spaces on top.

For a list item that starts with a paragraph, CommonMark gives this example:

- foo

      bar

The item's content offset is two (the dash and a space), so the code needs six spaces. Gruber's original description assumes the older style where list content is indented four spaces, so a code block inside an item needs eight spaces or two tabs.

When the indentation could be read as either list content or a code block, the list item reading wins. So pasting code under a numbered step often lands as a paragraph or a nested item, not a code block. If you also mix markers, see how bullet markers affect Markdown lists for how the list itself gets parsed.

Tabs and columns

CommonMark does not expand tabs in your source. Where indentation matters, a tab acts as a stop every four columns, so one tab can stand in for four spaces. A tab inside the code content is kept as a literal tab.

Mixing tabs and spaces is where it gets confusing. The file looks right in one editor and different in another. Whitespace you cannot see is a common cause of strange results, and invisible characters are worth ruling out when a block refuses to behave. Pick spaces or tabs and keep to it.

Indented or fenced

An indented block has no info string, so you cannot attach a language name for syntax highlighting. A fenced block can. The GitHub Flavored Markdown spec, a strict superset of CommonMark, defines fences as three or more backticks or tildes, indented at most three spaces:

print("hello")

Fences can interrupt a paragraph and need no blank lines around them. An unclosed fence runs to the end of its container or the document, which is easy to spot, while a stray indented line quietly changes meaning.

A practical split:

  • Use a fence for anything you share, document or highlight, including on GitHub.
  • Use an indented block only when a tool you target supports nothing else, or when you are editing a file that already uses it.
  • When you see code appear where you wrote ordinary indented text, remove the extra indentation or put a paragraph break before it.

Conclusion

Four spaces and a blank line make a code block; anything less is a paragraph, and inside a list you need four more spaces than the item's content. For new notes, fences are easier to read and to get right. In Carets you can see the result as you type, which makes a missing blank line easy to catch.

Frequently Asked Questions

How many spaces make an indented code block in Markdown?

Four or more spaces, or one tab, at the start of each line, with a blank line before the first one when it follows a paragraph.

Why is my indented text not turning into code?

Most often it directly follows a paragraph line. An indented code block cannot interrupt a paragraph, so add a blank line before it. Inside a list, you also need to indent past the item's content.

Can an indented code block have a language for syntax highlighting?

No. Indented code has no info string. Use a fenced block with a language name after the opening fence instead.