Skip to content
All referenceText Formatting

Markdown Code Block

Learn how to write inline code and fenced code blocks in markdown with syntax highlighting. Examples, platform support, and tips.

What is markdown code block?

Markdown supports two types of code formatting: inline code with single backticks (`) for short snippets, and fenced code blocks with triple backticks (```) for multi-line code with optional syntax highlighting.

Syntax

Inline code

Single backticks for inline code within a sentence.

Result
Use the console.log() function

Fenced code block

Triple backticks for multi-line code blocks.

Result
const x = 42
console.log(x)

Syntax highlighting

Add a language identifier after the opening backticks for colored syntax highlighting.

Result
const greeting = "hello"
console.log(greeting)

Platform support

PlatformSupportedNotes
GitHubYesFull syntax highlighting for 200+ languages
GitLabYesFull syntax highlighting
DiscordYesSyntax highlighting for common languages
SlackPartialInline code and blocks, no syntax highlighting
RedditYesIndent with 4 spaces or use fenced blocks
Stack OverflowYes
NotionYesFull syntax highlighting
ObsidianYes

Common mistakes

Wrong
``code``
Right
`code`

Use single backticks for inline code. Double backticks are only needed when the code itself contains a backtick.

Wrong
```
code```
Right
```
code
```

The closing ``` must be on its own line. Putting it on the same line as code will break the block.

Wrong
```js code here```
Right
```js
code here
```

Code must start on the line after the opening backticks. The language identifier goes right after ``` with no space.

Tips for code block

1Always specify the language

Adding a language (```python, ```sql, etc.) enables syntax highlighting and helps readers identify the language at a glance.

```python
def hello():
    print("hi")
```
2Backtick inside inline code

To include a literal backtick in inline code, use double backticks: ``code with ` backtick``.

``code with ` backtick``
3Indent alternative

You can also create code blocks by indenting every line with 4 spaces. But fenced blocks (```) are clearer and support language hints.

Frequently asked questions

Everything you need to know.

1

How do I create a code block in markdown?

Wrap your code with triple backticks (```) on separate lines. For inline code, use single backticks: `code`.

Related elements

Try it in the editor

Paste your markdown and see code block rendered instantly with professional themes.