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.
Use `console.log()` for quick feedback.Use console.log() for quick feedback.
Use `` `code` `` to show backticks, or ``a`b`` for one inside.Use `code` to show backticks, or a`b for one inside.
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
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
```function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
~~~python
def greet(name: str) -> str:
return f"Hello, {name}!"
~~~def greet(name: str) -> str:
return f"Hello, {name}!"
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.
| Language | Identifiers (checked on GitHub) |
|---|---|
| JavaScript / TypeScript | js, javascript, jsx, ts, typescript, tsx |
| Python | py, python |
| Shell | bash, sh, shell, zsh (console for a prompt plus output) |
| JSON / YAML | json, yaml, yml |
| HTML / CSS / SQL | html, css, sql |
| Markdown | md, markdown |
| Changes | diff |
| No highlighting | text (not txt, which GitHub maps to the Adblock filter grammar) |
```
Some plain text
inside a code block
```Some plain text
inside a code block
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.
````markdown
Here's how to write a code block:
```js
console.log("Hello")
```
````Here's how to write a code block:
```js
console.log("Hello")
```
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.
1. Install the package:
```bash
npm install marked
```
2. Import it in your code.Install the package:
npm install markedImport it in your code.
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.
Some text before the block.
function greet(name) {
return "Hello, " + name;
}Some text before the block.
function greet(name) {
return "Hello, " + name;
}
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.
| Identifier | What GitHub renders |
|---|---|
| ```diff | Lines starting with - turn red and lines starting with + turn green. |
| ```mermaid | Renders a Mermaid diagram: flowcharts, sequence diagrams, pie charts, and more. |
| ```geojson / topojson | Renders an interactive map from GeoJSON or TopoJSON data. |
| ```stl | Renders an interactive 3D model from ASCII STL. |
| ```math | Renders the block as display math (LaTeX syntax). |
```diff
- const oldValue = 1;
+ const newValue = 2;
```- const oldValue = 1;
+ const newValue = 2;
```mermaid
flowchart LR
A[Write Markdown] --> B[Push to GitHub]
B --> C[Diagram renders]
```> [!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.
```diff
- This text appears red
+ This text appears green
```- This text appears red
+ This text appears green
```fix
This text appears yellow/orange
```This text appears yellow/orange
```ini
[This text appears blue]
```[This text appears blue]
``` 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)
```
````Broken
```js
const x = 1;Fixed
```js
const x = 1;
```Broken
````
code
```Fixed
````
code
````Broken
Run this:
npm installFixed
Run this:
npm installBroken
1. Install:
```bash
npm i
```
2. Run itFixed
1. Install:
```bash
npm i
```
2. Run itBroken
Type `a`b` hereFixed
Type ``a`b`` hereQuick Reference
| Syntax | What 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 |
| code | Indented code block (4 spaces, blank line above) |
| ```diff - old + new ``` | Red removed and green added lines (GitHub, Discord) |
| ```mermaid | Diagram on GitHub, GitLab, and Obsidian |
| ```ansi | Colored 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.
| Platform | Supported? | Notes |
|---|---|---|
| GitHub | Yes | ``` and ~~~ fences, indented blocks, highlighting through Linguist, plus diff, mermaid, geojson, topojson, stl, and math blocks. |
| GitLab | Yes | Rouge highlighting and built-in Mermaid diagrams. PlantUML is on at GitLab.com; Kroki needs an admin to enable it. |
| Notion | Yes | Create one with /code and pick the language from the block's menu to get highlighting. Pasted fenced blocks become code blocks. |
| Obsidian | Yes | Prism highlighting in Reading view; Live Preview and Source mode highlight differently. Mermaid blocks render as diagrams. |
| VS Code preview | Yes | The built-in preview highlights fenced blocks that name a language, using highlight.js. |
| Partial | Inline code and four-space indented blocks work everywhere. ``` fences work on www.reddit.com but not on old Reddit. | |
| Discord | Yes | Inline code and ``` blocks. Naming a language turns on syntax highlighting (highlight.js), and ```ansi adds colored text. |
| Slack | Yes | ``` 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 / Astro | Yes | Docusaurus (Prism) and VitePress (Shiki) add line highlighting and line numbers through the info string. Astro highlights with Shiki by default. |
| CommonMark | Yes | Fenced 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.