To write inline code in Markdown, wrap a short snippet in single backticks (`code`) so it renders in a monospace font within a sentence. Code in Markdown comes in two forms: inline code for short fragments inside a line of prose, and code blocks that set apart whole sections of code, either by indenting every line four spaces or by using fenced code blocks. Reach for inline code for a function name, path, or flag, and a code block for anything spanning multiple lines. Everything inside code is treated literally, so Markdown syntax is not processed.
Markdown code syntax reference
| Goal | Syntax |
|---|---|
| Inline code | `code` |
| Inline code containing a backtick | `` code with ` inside `` |
| Indented code block | four spaces before each line |
| Fenced code block | see fenced code blocks |
Markdown code key facts
- Inline code wraps a short snippet in single backticks (
`code`) and renders it in a monospace font within a sentence. - To include a literal backtick, delimit the span with a longer run of backticks: use two backticks around content that holds one.
- Everything inside code is literal, so Markdown special characters and backslash escapes have no effect there.
- A code block sets apart whole lines: indent every line four spaces, or use a fenced code block with triple backticks.
- Indented code blocks need four spaces (or one tab) and a blank line before them. Three spaces render as a normal paragraph.
- Fenced code blocks are preferred for new documents because they are clearer and can carry a language tag for syntax highlighting.
Choose inline code, an indented block, or a fenced block
Use inline code for commands, filenames, identifiers, and short fragments inside a sentence. Use a fenced code block for anything that spans several lines or needs a language label. Indented code blocks are valid CommonMark, but their four-space indentation is harder to maintain inside lists and cannot carry a syntax-highlighting language in standard Markdown.
When the code itself contains backticks, wrap it with a longer backtick delimiter. For multi-line examples, prefer fenced code blocks and add a language identifier only when it matches the code. See syntax highlighting for renderer-specific language names.
Basic syntax
To create inline code, wrap the text in single backticks. The content between the backticks is rendered verbatim in a monospace font, and Markdown special characters inside it lose their meaning.
Markdown
Use the `printf` function to format output.
Rendered output
Use the printf function to format output.
HTML output
<p>Use the <code>printf</code> function to format output.</p>
Inline code is for short fragments that belong in a line of prose: a function name, a file path, a flag, a variable, or a single command. For anything spanning multiple lines, use a code block instead.
Escaping backticks with double backticks
To include a literal backtick inside inline code, wrap the whole span in two or more backticks. The rule is that a run of code is delimited by a matching run of backticks, so a double-backtick span can safely contain a single backtick.
Markdown
Use `` `code` `` to show backticks in your text.
Rendered output
Use `code` to show backticks in your text.
HTML output
<p>Use <code>`code`</code> to show backticks in your text.</p>
Add a space just inside the outer backticks when the content itself starts or ends with a backtick. The leading and trailing spaces are trimmed by the parser, so ` `foo` ` renders as `foo` without the padding.
If your content contains two backticks in a row, use three backticks as the delimiter, and so on. The number of opening and closing backticks must match, and the delimiter run must be longer than any run of backticks inside the span.
Markdown
Here is a double backtick: ``` `` ``` shown inline.
Rendered output
Here is a double backtick: `` shown inline.
Indented code blocks
Indent every line of a block by four spaces (or one tab) to create an indented code block. The four spaces are stripped from the output, and the remaining text is rendered verbatim inside a preformatted block. No syntax highlighting or language tag is possible with this style.
Markdown
function greet(name) {
return "Hello, " + name;
}
Rendered output
function greet(name) {
return "Hello, " + name;
}
HTML output
<pre><code>function greet(name) {
return "Hello, " + name;
}
</code></pre>
An indented code block needs a blank line before it, or the indented text can be pulled into the preceding paragraph. Inside the block, all characters are literal: asterisks, underscores, backticks, and angle brackets appear exactly as typed.
Extra indentation inside an indented block
Only the first four spaces are consumed as the code marker. Any indentation beyond that is preserved in the output, which is how you keep the internal structure of nested code.
Markdown
def outer():
def inner():
return 42
Rendered output
def outer():
def inner():
return 42
Indented code inside a list
Inside a list item, the indentation baseline shifts. To place a code block under a list item, indent it four spaces past the start of the item's text, which usually means eight spaces from the left margin for a simple bulleted list.
Markdown
- Step one, run the build:
npm run build
Rendered output
- Step one, run the build:
npm run build
Inline code with special characters
Because inline code is literal, it is the safest way to show characters that would otherwise trigger Markdown formatting, such as asterisks, underscores, and angle brackets. You do not need backslash escapes inside code.
Markdown
The glob `*.md` matches every file ending in `.md`.
Rendered output
The glob *.md matches every file ending in .md.
HTML output
<p>The glob <code>*.md</code> matches every file ending in <code>.md</code>.</p>
Indented code blocks versus fenced code blocks
Indented code blocks are the original Markdown style and are supported across the major Markdown specifications covered here. Fenced code blocks, which wrap code in triple backticks or tildes, are newer, more readable, and can carry a language tag for syntax highlighting. Fenced blocks live on their own page: see fenced code blocks.
Prefer fenced code blocks for new documents. Use indented blocks only when you need maximum portability to a very old or strict parser, or when a fence would be awkward, such as showing a fenced block inside another fenced block.
Flavor differences
Inline code, backtick escaping, and indented code blocks are part of the original Markdown syntax and are broadly supported. The core behavior does not vary across flavors.
| Flavor | Inline code and indented blocks | Notes |
|---|---|---|
| CommonMark | Yes | Precisely specified. Indented code blocks require four spaces or one tab; backtick runs must match in length. |
| GitHub Flavored Markdown (GFM) | Yes | Follows CommonMark. Fenced code blocks are the common choice on GitHub. |
| MultiMarkdown | Yes | Standard inline code and indented blocks. Also supports fenced code blocks. |
| Markdown Extra | Yes | Standard inline code and indented blocks. Adds fenced code blocks. |
| Pandoc | Yes | Standard inline code and indented blocks. Inline code can also carry attributes and a class for highlighting. |
The major Markdown specifications treat content inside inline code and code blocks as literal, so Markdown inside code is never processed. The differences between flavors sit almost entirely in fenced code blocks and syntax highlighting, not in inline code or indented blocks.
Other platforms (Slack, Discord, Reddit)
Chat and forum tools reuse the backtick convention with small differences:
- Slack: single backticks produce inline code, and triple backticks produce a code block, but Slack does not support the four-space indented code block style.
- Discord: single backticks are inline code, and triple backticks are a code block. Indented four-space blocks are not treated as code.
- Reddit: supports both indented four-space code blocks and, in the newer editor, fenced code blocks. Inline code uses single backticks.
For the full comparison of how flavors differ across all elements, see Markdown flavors.
How Markdific renders it
Markdific renders inline code as monospace text set apart from surrounding prose. Markdific renders fenced code blocks with syntax highlighting, and indented four-space code blocks render as plain preformatted text without a language tag, which is the expected behavior for that style.
Try it in the Markdific online editor.
Common mistakes and gotchas
- Unbalanced backticks. The opening and closing backtick runs must be the same length.
code` `` will not close correctly. - Backtick inside single-backtick code. A backtick cannot appear inside a single-backtick span. Switch to double backticks and add a space if needed.
- Forgetting the space padding. When the content starts or ends with a backtick, add a space just inside the outer delimiters or the parser miscounts the run.
- Three spaces instead of four. An indented code block needs four spaces or one tab. Three spaces render as a normal paragraph.
- Missing blank line before an indented block. Without a blank line, the indented lines can be absorbed into the paragraph above.
- Using indented blocks for highlighted code. Indented blocks cannot carry a language tag. Use a fenced code block if you want syntax highlighting.
- Escaping inside code. Backslash escapes do not work inside code spans or code blocks. The content is already literal, so a backslash stays a backslash.
Best practices
- Use inline code for short fragments in a sentence: names, paths, flags, and single commands.
- Prefer fenced code blocks over indented blocks for anything multi-line, because they are clearer and can carry a language tag.
- Reach for double or triple backticks only when the content contains backticks.
- Keep a blank line before and after an indented code block so it separates cleanly from surrounding text.
- Do not add backslash escapes inside code. The content is already literal.
- When showing a command, include the exact text a reader would type, with no added formatting.
HTML equivalent
Inline code compiles to a <code> element. An indented or fenced code block compiles to a <pre> element wrapping a <code> element, which preserves whitespace and line breaks.
Markdown
Call `init()` first.
init()
run()
HTML output
<p>Call <code>init()</code> first.</p>
<pre><code>init()
run()
</code></pre>
FAQ
How do you write inline code in Markdown?
Wrap the text in single backticks, like `code`. The content renders in a monospace font, and Markdown characters inside it are treated literally.
How do you show a backtick inside inline code? Wrap the span in two or more backticks. A double-backtick span can safely contain a single backtick, and you add a space inside the outer backticks if the content starts or ends with one.
How do you make an indented code block in Markdown? Indent every line of the block by four spaces or one tab, and leave a blank line before it. The four spaces are removed and the text renders verbatim.
What is the difference between inline code and a code block? Inline code sits inside a line of prose and is delimited by backticks. A code block sets apart whole lines and is created by indenting four spaces or using a fenced block. Use inline code for short fragments and blocks for multi-line code.
Do you need to escape characters inside Markdown code? No. Everything inside inline code and code blocks is literal, so backslash escapes have no effect. Backticks are handled by choosing a longer delimiter run, not by escaping.
Why does my indented code block not render? The most common causes are using three spaces instead of four and missing the blank line before the block. Check that every line has at least four leading spaces or one tab.
What HTML does Markdown code produce? Inline code produces a code element. An indented or fenced block produces a pre element wrapping a code element, which preserves whitespace and line breaks.
Should I use indented code blocks or fenced code blocks? Prefer fenced code blocks. They are easier to read and can carry a language tag for syntax highlighting. Use indented blocks only for maximum portability or when nesting a fenced block would be awkward.
Sources and compatibility references
Use these primary references for syntax rules. Renderer behaviour can still vary by version and configuration, so preview important documents in their final destination.
Related pages
- Markdown documentation hub
- Fenced code blocks
- Syntax highlighting
- Escaping characters
- Inline HTML in Markdown
- Markdown flavors compared
