Markdown Code Block Syntax
The complete guide to writing code in Markdown. Inline backticks, fenced triple-backtick blocks, language identifiers, indented blocks, GitHub callouts, Discord colors, and the five mistakes that break them.
Inline Code
Wrap a short code snippet or command in single backticks ( `code`). Inline code ignores Markdown formatting inside it, so asterisks and brackets appear literally.
Use `console.log()` for quick feedback.Use console.log() for quick feedback.
`` function `name` ``.Fenced Code Blocks
Use three backticks before and after the code. Put the opening fence on its own line. The closing fence must also be on its own line and use the same number of backticks.
```javascript
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
```function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
Language Identifiers
Add a language tag after the opening backticks so the renderer can apply syntax highlighting. The tag must touch the backticks with no space. Common tags: javascript, python, bash, json, html, css, sql.
```python
def greet(name: str) -> str:
return f"Hello, {name}!"
```def greet(name: str) -> str:
return f"Hello, {name}!"
```
Some plain text
inside a code block
```Some plain text
inside a code block
Indented Code Blocks
Indent every line by at least four spaces or one tab to create a code block. This is the original CommonMark way. It has no language identifier and is useful inside list items where fenced blocks can be tricky.
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("World"));
GitHub Callouts
GitHub extends blockquotes with alert callouts. Start a blockquote with > [!TYPE] where TYPE is NOTE, TIP, IMPORTANT, WARNING, or CAUTION. They render as colored blocks with icons.
> [!NOTE]
> This is a GitHub alert callout inside a code-friendly doc.Note
This is a GitHub alert callout inside a code-friendly doc.
Diff Code Blocks
Use the diff language identifier to show code changes. Lines starting with - render as removed (red) and lines starting with + render as added (green).
```diff
- const oldValue = 1;
+ const newValue = 2;
```- const oldValue = 1;
+ const newValue = 2;
Discord Code-Block Colors
Discord does not do true syntax highlighting, but you can produce colored text by using specific language identifiers with matching patterns. Use these for decoration, not for accurate language support.
```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]
Common Mistakes
The five most common reasons a Markdown code block fails. Each row shows the broken version and the corrected one side by side.
Broken
``` javascriptFixed
```javascriptBroken
```
code
`````Fixed
```
code
```Broken
```js
const x = 1;Fixed
```js
const x = 1;
```Broken
```python
code line 1
code line 2
```Fixed
code line 1
code line 2Broken
Use console.log() for outputFixed
Use `console.log()` for outputQuick Reference
| Syntax | What it does |
|---|---|
| `code` | Inline code (single backticks) |
| `` a`b `` | Inline code containing a backtick |
| ``` code ``` | Fenced code block |
| ```js code ``` | Fenced block with language tag for highlighting |
| code | Indented code block (4 spaces) |
| ```diff - old + new ``` | Diff block: red removed, green added |
| > [!NOTE] | GitHub alert callout (extended syntax) |
| ```fix ... ``` | Discord color trick: yellow |
Platform Support
Code blocks are core Markdown syntax, so support is broad. The main differences are language-tag handling, syntax highlighting, and whether language identifiers are used for color tricks.
| Platform | Supported? | Notes |
|---|---|---|
| GitHub | Yes | Full support. Fenced blocks, language IDs, diff, and Mermaid diagrams all work. |
| GitHub mobile | Yes | Same as desktop. Code blocks scroll horizontally. |
| GitLab | Yes | Syntax highlighting with Rouge. Supports collapsible blocks and math. |
| Notion | Yes | Pasting fenced Markdown converts to a Notion code block with language detection. |
| Obsidian | Yes | Live Preview and Reading view render fenced blocks. Language IDs enable PrismJS highlighting. |
| VS Code preview | Yes | Built-in preview applies highlight.js to fenced blocks with a language tag. |
| Reddit (new) | Yes | Fenced code blocks and inline code work. No syntax highlighting on mobile. |
| Reddit (old) | Partial | Inline code works. Fenced blocks with triple backticks do not render as code blocks. |
| Discord | Yes | Fenced blocks work but language hints are only used for color tricks, not syntax highlighting. |
| Slack | Yes | Fenced blocks and inline code work. Slack ignores the language identifier entirely. |
| Docusaurus / VitePress / Astro | Yes | Shiki or Prism via rehype. Language IDs and line highlighting supported. |
| Standard CommonMark | Yes | Fenced and indented code blocks are core syntax. Language identifiers are not in the spec but are widely supported. |
Frequently Asked Questions
How do you write a code block in Markdown?
Use three backticks on a line by themselves, followed by the code, then three closing backticks on their own line. Optionally add a language identifier after the opening backticks for syntax highlighting: ```javascript ... ```.
How do you add syntax highlighting to a Markdown code block?
Add a language identifier immediately after the opening three backticks with no space. For example, use ```python for Python, ```javascript for JavaScript, ```bash for shell commands, and ```json for JSON. The renderer uses this tag to pick a highlighter theme.
Can you use code blocks in Discord?
Yes. Discord supports inline code with single backticks and multi-line code blocks with triple backticks. Discord also uses the language identifier for color tricks: ```diff, ```fix, ```ini, and ```bash produce colored text, but there is no real syntax highlighting.
Why is my Markdown code block not rendering?
The most common causes are: (1) a space between the opening backticks and the language identifier, (2) mismatched fence counts (always three on each side), (3) missing the closing fence, or (4) the opening fence is indented so far that the parser treats it as a code line. Put fences at the start of the line with no leading spaces.
What is the difference between fenced and indented code blocks?
Fenced code blocks use triple backticks (```) and support language tags. Indented code blocks use four spaces or one tab on every line and are part of CommonMark. Indented blocks are useful inside list items where fences often break.
How do you escape backticks inside inline code?
Use double backticks to wrap the inline code when it contains a single backtick: `` function `name` ``. The outer double backticks become the delimiters and the single backtick inside is rendered literally.
Does Markdown support line numbers or highlighted lines in code blocks?
Not in standard Markdown. Line numbers and line highlighting are renderer-specific features. Static-site generators like Docusaurus and VitePress support them through a line-numbers attribute or highlighted-line comments, but GitHub and most editors do not.
Do code blocks disable other Markdown formatting?
Yes. Inside a code block — fenced or indented — Markdown symbols are treated as literal text. This is why code blocks are the standard way to show raw Markdown syntax, URLs, or asterisks without them being parsed.
How do GitHub alert callouts work?
GitHub supports a special blockquote syntax for alerts: > [!NOTE], > [!TIP], > [!IMPORTANT], > [!WARNING], and > [!CAUTION]. These render colored callouts with icons inside the .alert class. They are GitHub-specific and do not render on most other platforms.
Related Markdown Guides
Code blocks are core Markdown. For the rest of the syntax surface area: