MDUtil

Code Blocks and Inline Code in Markdown

Wrap code in backticks for inline code, or in three backticks on their own lines for a code block. This guide covers syntax highlighting, language names, showing backticks inside code, and diffs.

Quick answer

How do you write a code block in Markdown?

Put three backticks on the line before the code and three on the line after. Add a language name after the opening backticks, such as ```python, to get syntax highlighting. For code inside a sentence, wrap it in single backticks: `code`.

Markdown
```python
def greet(name):
    return f"Hello, {name}!"
```

Call `greet()` with a name.

Result

def greet(name):
    return f"Hello, {name}!"

Call greet() with a name.

Code syntax and examples

Everything inside backticks is shown exactly as typed, so Markdown symbols in code are safe.

Inline code

Single backticks mark code inside a sentence: commands, file names and function names.

Markdown
Run `npm install`, then open `src/index.ts`.

Result

Run npm install, then open src/index.ts.

Fenced code block with a language

The language name after the opening backticks turns on syntax highlighting.

Markdown
```js
const total = items.reduce((sum, item) => sum + item.price, 0);
console.log(total);
```

Result

const total = items.reduce((sum, item) => sum + item.price, 0);
console.log(total);

Showing changes with diff

In a diff block, lines that start with - and + are colored as removed and added.

Markdown
```diff
- const rate = 0.2;
+ const rate = TAX_RATE;
```

Result

- const rate = 0.2;
+ const rate = TAX_RATE;

Backticks inside code

Use two backticks around inline code that contains a backtick, and a longer fence around a block that contains a fence.

Markdown
``Use `code` for inline code``

````markdown
```js
console.log("hi");
```
````

Result

Use `code` for inline code

```js
console.log("hi");
```

Tilde fences

Three tildes work like three backticks, which helps when the code itself is full of backticks.

Markdown
~~~bash
echo "Wrap code in ``` to make a block"
~~~

Result

echo "Wrap code in ``` to make a block"

Indented code blocks

Four spaces at the start of every line also make a code block, but without a language. Fences are clearer.

Markdown
    npm run build
    npm run start

Result

npm run build
npm run start

Code blocks in popular apps

Inline code and fenced blocks work almost everywhere; highlighting varies.

AppSupportNotes
GitHub / GitLabSupportedHighlighting for hundreds of languages; mermaid blocks become diagrams.
DiscordSupportedFenced blocks are highlighted for the language you name.
SlackDiffersCode blocks work, without highlighting.
ObsidianSupportedHighlighting in reading view; mermaid blocks become diagrams.
NotionDiffersThe /code command adds a code block; the language is picked from its label.
RedditDiffersFenced blocks work on the new site; old Reddit needs four-space indentation.

Questions and answers

How do I write inline code in Markdown?

Wrap it in backticks: `npm install`. To show a backtick inside, wrap the code in two backticks with spaces: `` a`b ``.

How do I add syntax highlighting?

Write the language name right after the opening backticks, such as ```python or ```js. The app must support highlighting and know the name.

Which language names can I use?

Common ones are bash, c, cpp, csharp, css, diff, go, html, java, js, json, kotlin, markdown, php, python, ruby, rust, sql, swift, ts and yaml. GitHub accepts hundreds more.

How do I show triple backticks inside a code block?

Open and close the block with four backticks, or with three tildes ~~~.

Why is my code block not rendering?

Check that the opening and closing fences sit on their own lines, use the same character, and that the closing fence is at least as long as the opening one. Inside a list, indent the fence to the item's text.

Can I show line numbers?

Markdown has no line number syntax. Some sites and editors add them through their own settings or plugins.

How do I make a diagram from a code block?

Mark the block mermaid and write the diagram in Mermaid's syntax. GitHub, GitLab and Obsidian draw it, and so does Markdown Live Preview.

Do code blocks work in Discord and Slack?

Both support inline code and fenced blocks. Discord highlights the language you name; Slack shows plain monospaced text.