Skip to main content

Markdown Code Blocks

Inline code, fenced blocks with backticks or tildes, language tags for syntax highlighting, showing backticks inside code, code in lists, and the GitHub, Discord, and Slack quirks.

Short answer

Put three backticks (```) on their own line before and after the code. Add a language after the opening fence, like ```python, for syntax highlighting. For code inside a sentence, use single backticks: `code`. To show ``` inside a block, open and close it with four backticks or with ~~~.

Inline Code

Wrap a short snippet or command in single backticks (`code`). Inline code ignores Markdown formatting inside it, so asterisks and brackets appear literally. To include a backtick, wrap the code in a longer run of backticks, and add a space inside the delimiters when the code starts or ends with one: one space is stripped from each side.

Inline code inside a sentence
Use `console.log()` for quick feedback.
Preview
Backticks inside inline code
Use `` `code` `` to show backticks, or ``a`b`` for one inside.
Preview

Fenced Code Blocks

Put three or more backticks (```) or tildes (~~~) on a line by themselves before and after the code. The closing fence must use the same character and be at least as long as the opening one. Fences can be indented up to three spaces; four makes them part of an indented code block. A blank line before and after the block keeps the source readable.

JavaScript fenced code block
```javascript
function greet(name) {
  return `Hello, ${name}!`;
}

console.log(greet("World"));
```
Preview
Tilde fence (~~~) with a language
~~~python
def greet(name: str) -> str:
    return f"Hello, {name}!"
~~~
Preview

Language Identifiers and Syntax Highlighting

Add a language name after the opening fence so the renderer can highlight the code. The renderer reads the first word of that line (the "info string"), so ``` js with a space works too and extra words are ignored by GitHub, though site generators use them for titles and line highlighting. GitHub picks the highlighter with Linguist, so aliases work and case does not matter; GitHub Pages wants lowercase names. With no language, the block is plain monospace.

Common language identifiers that GitHub highlights
LanguageIdentifiers (checked on GitHub)
JavaScript / TypeScriptjs, javascript, jsx, ts, typescript, tsx
Pythonpy, python
Shellbash, sh, shell, zsh (console for a prompt plus output)
JSON / YAMLjson, yaml, yml
HTML / CSS / SQLhtml, css, sql
Markdownmd, markdown
Changesdiff
No highlightingtext (not txt, which GitHub maps to the Adblock filter grammar)
No language identifier
```
Some plain text
inside a code block
```
Preview

Nesting Code Blocks and Code in Lists

To show a code block inside a code block (say, when documenting Markdown itself), make the outer fence longer than any fence inside it. A block opened with four backticks can only be closed by four or more, so the inner ``` lines are shown as text. Tildes work too, because a fence only closes on the same character.

Four-backtick outer fence around a ``` block
````markdown
Here's how to write a code block:

```js
console.log("Hello")
```
````
Preview

Inside a list, indent the fence and the code to line up with the item's text: 3 spaces under 1., 2 under -. Leave a blank line before the fence. A fence at the start of the line ends the list instead.

Fenced block inside a numbered list
1. Install the package:

   ```bash
   npm install marked
   ```

2. Import it in your code.
Preview

Indented Code Blocks

Indent every line by four spaces or one tab to make a code block. This is the original Markdown form and is still part of CommonMark, but it has no language name and cannot interrupt a paragraph, so it needs a blank line above it. Inside a list item, GitHub's docs recommend eight spaces. Fences are the safer default.

Indented code block (4 spaces per line)
Some text before the block.

    function greet(name) {
      return "Hello, " + name;
    }
Preview

GitHub: Diff, Mermaid, Maps, and Math

On GitHub, a few language identifiers do more than color the code. Diagram rendering works in issues, discussions, pull requests, wikis, and Markdown files.

Special code block identifiers on GitHub
IdentifierWhat GitHub renders
```diffLines starting with - turn red and lines starting with + turn green.
```mermaidRenders a Mermaid diagram: flowcharts, sequence diagrams, pie charts, and more.
```geojson / topojsonRenders an interactive map from GeoJSON or TopoJSON data.
```stlRenders an interactive 3D model from ASCII STL.
```mathRenders the block as display math (LaTeX syntax).
Diff block showing removed and added lines
```diff
- const oldValue = 1;
+ const newValue = 2;
```
Renders on GitHub as

- const oldValue = 1;

+ const newValue = 2;

Mermaid flowchart (renders as a diagram on GitHub)
```mermaid
flowchart LR
  A[Write Markdown] --> B[Push to GitHub]
  B --> C[Diagram renders]
```
Looking for GitHub alerts? Boxes like > [!NOTE] are blockquote syntax, not code blocks. See GitHub alerts on the quotes guide.

Discord and Slack Code Blocks

Discord renders `inline code` and ``` blocks, and naming a language turns on syntax highlighting. People also bend that highlighter into colored text with the tricks below. For reliable colors, use an ```ansi block: the Discord color text generator writes one for you, and the Discord guide covers both methods.

Red & Green (diff)Discord only
```diff
- This text appears red
+ This text appears green
```
Use - for red, + for green.

- This text appears red

+ This text appears green

Yellow (fix)Discord only
```fix
This text appears yellow/orange
```
Everything inside a fix block turns yellow.

This text appears yellow/orange

Blue (ini)Discord only
```ini
[This text appears blue]
```
Wrap text in square brackets inside an ini block.

[This text appears blue]

Slack: ``` blocks and inline code work in the composer, but they render in plain monospace and the language is ignored. Syntax highlighting only exists inside a Block Kit markdown block, added in March 2026. Shortcuts: ⌘⇧C for inline code, ⌘⌥⇧C for a code block. More in the Slack guide.

Common Mistakes

The six most common reasons a Markdown code block breaks. Each row shows the broken version and the corrected one side by side.

Broken

```
Use a fence like this:
```js
console.log(1)
```
```

Fixed

````
Use a fence like this:
```js
console.log(1)
```
````
The inner ``` closes the outer block early, and the last fence opens a new block that swallows the rest of the document. Open the outer block with four backticks (or ~~~) so only a fence of four or more can close it.

Broken

```js
const x = 1;

Fixed

```js
const x = 1;
```
Missing closing fence. CommonMark renderers treat everything to the end of the document (or of the enclosing list item or quote) as code.

Broken

````
code
```

Fixed

````
code
````
The closing fence must be at least as long as the opening one. Three backticks cannot close a block opened with four, so the block never ends. A longer closing fence is fine.

Broken

Run this:
    npm install

Fixed

Run this:

    npm install
An indented code block cannot interrupt a paragraph. Without a blank line above it, the indented line just joins the paragraph as plain text.

Broken

1. Install:
```bash
npm i
```
2. Run it

Fixed

1. Install:

   ```bash
   npm i
   ```

2. Run it
A fence at the start of the line ends the list. The code drops out of the item, and on GitHub the next item starts a new list. Indent the fence to line up with the item's text: 3 spaces under 1., 2 under -.

Broken

Type `a`b` here

Fixed

Type ``a`b`` here
A single backtick inside inline code ends it early. Wrap the code in a longer run of backticks than any run it contains.

Quick Reference

SyntaxWhat it does
`code`Inline code
``a`b``Inline code containing a backtick
``` code ```Fenced code block
~~~ code ~~~Fenced code block with tildes
```js code ```Fenced block with a language for highlighting
```` ``` ````Outer fence that can contain ``` lines
codeIndented code block (4 spaces, blank line above)
```diff - old + new ```Red removed and green added lines (GitHub, Discord)
```mermaidDiagram on GitHub, GitLab, and Obsidian
```ansiColored text on Discord

Platform Support

Code blocks are core Markdown syntax, so support is broad. The differences are which fences and indented blocks work, whether the language name turns on highlighting, and what special blocks exist.

PlatformSupported?Notes
GitHubYes``` and ~~~ fences, indented blocks, highlighting through Linguist, plus diff, mermaid, geojson, topojson, stl, and math blocks.
GitLabYesRouge highlighting and built-in Mermaid diagrams. PlantUML is on at GitLab.com; Kroki needs an admin to enable it.
NotionYesCreate one with /code and pick the language from the block's menu to get highlighting. Pasted fenced blocks become code blocks.
ObsidianYesPrism highlighting in Reading view; Live Preview and Source mode highlight differently. Mermaid blocks render as diagrams.
VS Code previewYesThe built-in preview highlights fenced blocks that name a language, using highlight.js.
RedditPartialInline code and four-space indented blocks work everywhere. ``` fences work on www.reddit.com but not on old Reddit.
DiscordYesInline code and ``` blocks. Naming a language turns on syntax highlighting (highlight.js), and ```ansi adds colored text.
SlackYes``` blocks and inline code, monospace only: the composer and mrkdwn ignore the language. Highlighting works only inside a Block Kit markdown block (since March 2026).
Docusaurus / VitePress / AstroYesDocusaurus (Prism) and VitePress (Shiki) add line highlighting and line numbers through the info string. Astro highlights with Shiki by default.
CommonMarkYesFenced and indented code blocks are core syntax. The spec defines the info string after the fence but leaves highlighting to the renderer.

Frequently Asked Questions

How do you write a code block in Markdown?

Put three backticks (```) on a line by themselves, then the code, then three backticks on their own line to close it. Three tildes (~~~) work the same way. Add a language name after the opening fence for syntax highlighting: ```python. For a short snippet inside a sentence, use single backticks instead: `code`.

What does ``` mean in Markdown?

Three backticks on a line of their own open a fenced code block, and a second line of three or more backticks closes it. Everything between is shown exactly as typed in a monospace font, with no Markdown formatting applied. A word right after the opening backticks, such as ```js, names the language for syntax highlighting.

How do you add syntax highlighting to a Markdown code block?

Write the language name after the opening fence: ```python, ```js, ```bash, ```json. A space before it (``` js) also works, because the renderer reads the first word of the line. GitHub picks the highlighter with Linguist, so common aliases like py, ts, sh, and yml work and case does not matter. GitHub Pages wants lowercase names. Use text for no highlighting.

How do I show triple backticks inside a code block?

Make the outer fence longer than any backtick run inside it: open and close with four backticks (````) and the ``` lines inside are shown as text. Or use tildes (~~~) for the outer fence. A closing fence has to use the same character and be at least as long as the opening one, so the inner ``` cannot end the block.

How do you escape backticks inside inline code?

Wrap the code in a longer run of backticks than it contains: ``a`b`` renders as a`b. If the code starts or ends with a backtick, add a space inside the delimiters: `` `code` `` renders as `code`, because one space is stripped from each side.

How do I put a code block inside a list?

Leave a blank line after the item's text, then indent the fence and the code to line up with that text: 3 spaces under a 1. item, 2 spaces under a - item. An unindented fence ends the list. For an indented (non-fenced) code block inside a list, GitHub's docs recommend eight spaces.

Can I put a code block inside a Markdown table?

No. A table cell is a single line, so a fenced block cannot fit in one. Use inline code in the cell, separate lines with <br>, and escape any pipe as \|, even inside the backticks. For real multi-line code in a cell, write the table in HTML.

How do I make a collapsible code block?

Wrap it in <details> and <summary> tags, and leave a blank line after </summary> and before </details>. Without the blank line, GitHub shows the backticks as text instead of a code block. This works on GitHub, GitLab, and other renderers that allow raw HTML.

Why is my Markdown code block not rendering?

The common causes: (1) the closing fence is missing or shorter than the opening one; (2) the code contains ``` and closes the block early, so use a four-backtick outer fence; (3) an indented block has no blank line above it; (4) a fence inside a list is not indented to the item's text; (5) the fence itself is indented four or more spaces, which makes it part of an indented code block.

What is the difference between fenced and indented code blocks?

Fenced blocks use ``` or ~~~ lines, take a language name for highlighting, and can follow a paragraph directly. Indented blocks are the original Markdown form: every line indented four spaces or one tab, a blank line before the block, and no language name. Fenced blocks are the better default.

Does Markdown support line numbers or highlighted lines in code blocks?

Not in standard Markdown, and GitHub has neither. They are renderer features: Docusaurus uses showLineNumbers and {1,3} after the language, and VitePress uses :line-numbers and {1,3}.

Do code blocks disable other Markdown formatting?

Yes. Inside a code block, fenced or indented, and inside inline code, Markdown symbols are literal text. That is why code blocks are the standard way to show raw Markdown syntax, asterisks, or underscores without them being parsed.

Can you use code blocks in Discord and Slack?

Yes. Discord supports `inline code` and ``` blocks, and naming a language (```js) turns on syntax highlighting; an ```ansi block adds colored text. Slack supports both too, but the composer and mrkdwn show code in plain monospace and ignore the language. In Slack's composer, ⌘⇧C (Ctrl+Shift+C) is inline code and ⌘⌥⇧C (Ctrl+Alt+Shift+C) is a code block.