Skip to main content

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.

Inline code inside a sentence
Use `console.log()` for quick feedback.
Preview

Use console.log() for quick feedback.

Tip: If the snippet itself contains a backtick, wrap it in double backticks: `` 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 fenced code block
```javascript
function greet(name) {
  return `Hello, ${name}!`;
}

console.log(greet("World"));
```
Preview
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 with language identifier
```python
def greet(name: str) -> str:
    return f"Hello, {name}!"
```
Preview
def greet(name: str) -> str:
    return f"Hello, {name}!"
No language identifier
```
Some plain text
inside a code block
```
Preview
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.

Indented code block (4 spaces per line)
    function greet(name) {
      return `Hello, ${name}!`;
    }

    console.log(greet("World"));
Preview
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.

GitHub-style alert callout
> [!NOTE]
> This is a GitHub alert callout inside a code-friendly doc.
Renders as

Note

This is a GitHub alert callout inside a code-friendly doc.

Note: GitHub callouts are a GFM extension. They do not render on Discord, Slack, Reddit, or most static-site generators unless specifically implemented.

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 block showing removed and added lines
```diff
- const oldValue = 1;
+ const newValue = 2;
```
Preview
- 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.

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]

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

``` javascript

Fixed

```javascript
Most parsers require the language identifier directly after the opening backticks with no space. Add a space and the language tag becomes part of the code content.

Broken

```
code
`````

Fixed

```
code
```
Fence counts must match. Three backticks open, three backticks close. Extra backticks at the end are treated as literal text.

Broken

```js
const x = 1;

Fixed

```js
const x = 1;
```
Missing the closing fence. Many renderers will keep treating the rest of the document as a code block until they find a matching fence.

Broken

```python
code line 1
code line 2
```

Fixed

    code line 1
    code line 2
Not a mistake, but a choice. Indented code blocks (4 spaces or 1 tab) work without fences and are part of CommonMark. They are useful when you need to embed code inside a list item that would otherwise confuse a fenced block.

Broken

Use console.log() for output

Fixed

Use `console.log()` for output
Backticks are required for inline code. Without them, the words render as plain text.

Quick Reference

SyntaxWhat 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
codeIndented 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.

PlatformSupported?Notes
GitHubYesFull support. Fenced blocks, language IDs, diff, and Mermaid diagrams all work.
GitHub mobileYesSame as desktop. Code blocks scroll horizontally.
GitLabYesSyntax highlighting with Rouge. Supports collapsible blocks and math.
NotionYesPasting fenced Markdown converts to a Notion code block with language detection.
ObsidianYesLive Preview and Reading view render fenced blocks. Language IDs enable PrismJS highlighting.
VS Code previewYesBuilt-in preview applies highlight.js to fenced blocks with a language tag.
Reddit (new)YesFenced code blocks and inline code work. No syntax highlighting on mobile.
Reddit (old)PartialInline code works. Fenced blocks with triple backticks do not render as code blocks.
DiscordYesFenced blocks work but language hints are only used for color tricks, not syntax highlighting.
SlackYesFenced blocks and inline code work. Slack ignores the language identifier entirely.
Docusaurus / VitePress / AstroYesShiki or Prism via rehype. Language IDs and line highlighting supported.
Standard CommonMarkYesFenced 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: