How to Format Code in Markdown
For inline code, wrap text in single backticks: `code`. For multi-line code, use a fenced block — three backticks on a line, your code, then three backticks to close. Add a language name after the opening fence (```javascript) to get syntax highlighting.
Platform Support
Everything inside code formatting is treated literally: asterisks, underscores, hashes, and HTML lose their Markdown meaning. That makes inline code the right tool for filenames, commands, variable names, and any string a reader might copy verbatim. The language tag on fenced blocks is free metadata — use it. Highlighters recognize dozens of identifiers (js, python, bash, json, diff, and more), and `diff` in particular is underused: it colors lines starting with + green and - red, perfect for showing before/after changes.
The classic puzzle is showing a backtick inside code. The rule: use more backticks for the delimiter than appear in the content. To show `code` with its backticks inline, wrap it in double backticks with a space: `` `code` ``. The same trick scales to fences — to display a fenced code block inside a fenced code block, use four backticks (````) for the outer fence. Tildes also work as fence characters (~~~), which is an easy way to nest a backtick fence inside a tilde fence.
Fenced blocks need a blank line before them in some renderers, and the closing fence must use at least as many backticks as the opening one. Inside a list item, indent the entire fence to match the list content (typically 2-4 spaces) so the block stays attached to the item rather than terminating the list.
The older alternative — indenting every line by four spaces — still works in CommonMark but is worth avoiding: it cannot carry a language tag, it is fragile when copy-pasting, and it interacts badly with lists, where the indentation that means "code block" also means "list continuation". This is also why you should be careful about accidentally indenting normal text four spaces: it will silently render as code. Fenced blocks are explicit and unambiguous; prefer them everywhere.
Examples
Use `code` inlineUse code inline
Format code inline using backticks.
```javascript
const greeting = "Hello, World!";
console.log(greeting);
```const greeting = "Hello, World!";
console.log(greeting);Create a code block with syntax highlighting.
const greeting = "Hello";
console.log(greeting);const greeting = "Hello";
console.log(greeting);Create a code block using 4 spaces indentation.
Use `` `backticks` `` to format inline code.Use `backticks` to format inline code.
Double backticks with a space let you display single backticks inside code.
```diff
- const port = 3000;
+ const port = process.env.PORT ?? 3000;
```- const port = 3000;
+ const port = process.env.PORT ?? 3000;The diff language colors removed and added lines — great for showing changes.
1. Install dependencies:
```bash
npm install
```
2. Start the dev server.Install dependencies:
npm installStart the dev server.
Indent the fence to match the list content so the list does not break.
Common Mistakes
``` javascript (space before language)```javascriptMost highlighters tolerate this, but some do not — keep the language identifier flush against the fence for reliable highlighting.
A fenced block opened with ``` and never closedAlways close the fence with matching ```An unclosed fence swallows the entire rest of the document into the code block. If everything below a snippet looks like code, look for a missing closing fence.
Pasting code with 4-space indentation as a "block"Wrap the code in ``` fences insteadIndented code blocks cannot specify a language, break inside lists, and trigger accidentally. Fenced blocks are explicit and support highlighting.
Platform Notes
GitHub
Fenced blocks support hundreds of languages via Linguist. GitHub also supports ```mermaid for diagrams and ```math for LaTeX blocks.
Discord
Supports `inline` and ```fenced``` blocks with language highlighting (```py, ```js). Indented code blocks are not supported.
Slack
Supports `inline code` and triple-backtick blocks, but no syntax highlighting and no language tags.
Obsidian
Full fenced block support with highlighting, plus special fences like ```mermaid and ```dataview (with plugins).
Frequently Asked Questions
How do I add a code block in Markdown?
Put three backticks on a line, your code on the following lines, and three backticks to close. Add a language after the opening fence (```python) for syntax highlighting.
How do I show a backtick inside inline code?
Use more backticks as the delimiter than the content contains: `` `code` `` displays a single-backtick-wrapped word. Leading/trailing spaces inside the delimiters are stripped.
How do I put a code block inside another code block?
Use a longer fence for the outer block — four backticks (````) around an example that itself contains a three-backtick fence. Tilde fences (~~~) also work as the outer delimiter.
Why is there no syntax highlighting in my code block?
Check that you added a language identifier right after the opening fence (```js, not ``` js on some renderers), that the platform supports highlighting at all (Slack does not), and that the language name is one the highlighter recognizes.
How do I keep a code block inside a numbered list?
Indent the entire fenced block to align with the list item content (usually 3-4 spaces for ordered lists). Unindented fences end the list and reset the numbering.
Related Syntax
Lists
Learn Markdown lists: unordered bullets with -, ordered lists with numbers, nesting rules, loose vs tight lists, and how to avoid broken numbering.
Tables
Learn how to create tables in Markdown with pipes and dashes, align columns with colons, escape pipes inside cells, and work around missing features like merged cells.
Blockquotes
Learn how to create blockquotes in Markdown with the > character, including multi-paragraph quotes, nested quotes, callouts/admonitions, and lazy continuation rules.