Markdown Code Blocks
Markdown code blocks let you display source code, terminal commands, configuration files, and other preformatted text without losing indentation or special characters.
This guide covers inline code, fenced code blocks, language identifiers and syntax highlighting, code inside lists, advanced techniques, platform compatibility, and fixes for the most common problems.
Quick answer: To create a Markdown code block, place three backticks on a line before your content and three matching backticks after it. Add a language name after the opening backticks, such as
javascriptorpython, to enable syntax highlighting where supported.
Code Block Syntax at a Glance
Markdown code blocks preserve indentation, whitespace, and special characters exactly as written, which makes them ideal for source code, terminal commands, configuration files, and other preformatted text.
```javascript
console.log("Hello, World!");
```
For short snippets inside a sentence, use single backticks instead. Need a refresher on basic formatting? See the Markdown Syntax Guide.
| What you need | Syntax |
|---|---|
| Inline code | Single backticks around the text |
| Basic fenced block | Three backticks before and after the content |
| Syntax-highlighted block | Three backticks followed by a language identifier |
| Alternative fence | Three tildes (~~~) |
| Indented code block | Four leading spaces |
The most common method is a fenced code block using three backticks.
π‘ Practice as you read: Try each example in the Markdown Editor with live preview.
Inline Code vs Code Blocks
Markdown provides two ways to display code.
Inline code#
Use inline code for short commands, filenames, variables, or small snippets inside a sentence.
Run `npm install` before starting the project.
Output
Live Preview
Run npm install before starting the project.
Fenced code blocks#
Use a fenced block when the example contains multiple lines or needs preserved indentation.
```bash
npm install
npm run dev
```
Fenced blocks suit programming examples, terminal commands, configuration files, JSON, SQL queries, HTML snippets, and README documentation.
π‘ Simple rule: Use inline code for short text inside a sentence, and fenced code blocks for multi-line content.
Fenced Code Blocks and Language Identifiers
A fenced code block starts and ends with matching fences.
```text
Hello World
```
Everything inside the fence is treated as literal content. Markdown characters such as #, *, _, and [] appear as written instead of being interpreted as formatting.
To request syntax highlighting, add a language identifier immediately after the opening fence.
```language
Your code here
```
For example:
```python
print("Hello, World!")
```
The exact highlighting result depends on the Markdown renderer and whether it supports that language.
Common Language Identifiers
These identifiers are commonly used in Markdown documentation:
| Language | Identifier | Common use |
|---|---|---|
| JavaScript | javascript or js |
Web applications |
| TypeScript | typescript or ts |
Type-safe JavaScript |
| Python | python |
Scripts and automation |
| Bash / Shell | bash or sh |
Terminal commands |
| SQL | sql |
Database queries |
| JSON | json |
API responses and data |
| YAML | yaml |
Configuration files |
| HTML | html |
Markup examples |
| CSS | css |
Stylesheets |
| Markdown | markdown |
Markdown examples |
| Dockerfile | dockerfile |
Docker configuration |
| PHP | php |
Server-side code |
If a renderer does not recognize the language identifier, the code block usually still displays normally, but syntax highlighting may not appear.
Common Code Block Examples
The following examples cover formats developers use most often in README files, developer documentation, tutorials, and API references.
JavaScript#
function greet(name) {
console.log(`Hello ${name}`);
}
greet("MDConvertHub");
Python#
def greet(name):
print(f"Hello, {name}")
greet("MDConvertHub")
Bash#
npm install
npm run dev
JSON#
{
"success": true,
"message": "User created successfully",
"id": 123
}
YAML#
name: Build
on:
push:
branches:
- main
Dockerfile#
FROM node:20
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
Code Blocks Inside Lists
You can place a fenced code block inside a list item by indenting it correctly.
1. Install dependencies.
```bash
npm install
npm run dev
```
2. Start the development server.
The indentation keeps the code block associated with the list item.
If you need to show code inside a standard Markdown table, inline code is usually the safer choice. Support for multi-line fenced blocks inside table cells varies between renderers.
Advanced Code Block Techniques
Indented code blocks#
Markdown also supports code blocks created with four leading spaces.
function hello() {
console.log("Hello");
}
Indented blocks are valid, but fenced blocks are usually easier to edit and can support language-specific syntax highlighting.
Showing backticks inside a code block#
If your example needs to contain triple backticks, use a longer fence around the outer block.
````markdown
```javascript
console.log("Hello");
```
````
The longer outer fence prevents Markdown from closing the block too early.
Using tildes instead of backticks#
Many Markdown renderers also support tildes as fences.
~~~python
print("Hello, World!")
~~~
This can be useful when the content already contains multiple backticks.
π‘ Tip: Pick one fence style and use it consistently throughout a document unless there is a reason to switch.
Compatibility Across Markdown Platforms
Fenced code blocks are widely supported, but syntax highlighting and available language identifiers can vary by renderer.
| Platform | Fenced code blocks | Syntax highlighting |
|---|---|---|
| GitHub | Supported | Supported |
| GitLab | Supported | Supported |
| VS Code Markdown Preview | Supported | Supported |
| Obsidian | Supported | Supported |
| Discord | Supported | Supported for many languages |
| Static site generators | Usually supported | Depends on configuration |
| Other Markdown editors | Usually supported | Varies by renderer |
For GitHub-specific Markdown behavior, see the GitHub Flavored Markdown Guide.
If you publish the same Markdown on multiple platforms, preview it on the target platform before relying on a particular highlighting feature.
Best Practices
- Use fenced blocks for multi-line code and commands.
- Add a language identifier when syntax highlighting improves readability.
- Keep each example focused on one concept.
- Use realistic, copyable examples where possible.
- Keep indentation consistent.
- Separate unrelated examples into different blocks.
- Use inline code for short commands, filenames, variables, and keywords.
- Preview the rendered output before publishing documentation.
π‘ Workflow tip: Check formatting in the live Markdown editor before converting your document with Markdown to PDF.
Common Problems and Fixes
| Problem | Likely cause | Fix |
|---|---|---|
| Code does not render as a block | Missing or mismatched fence | Check that the opening and closing fences match |
| Syntax highlighting does not appear | Missing or unsupported language identifier | Add a supported identifier such as javascript, python, or bash |
| Code formatting looks inconsistent | Mixed indentation | Use spaces consistently within the example |
| A nested block breaks | Inner and outer fences conflict | Use a longer outer fence |
| Markdown formatting appears unexpectedly | The code fence is missing or closed incorrectly | Check the opening and closing fences |
| Code looks different on another platform | Different renderer or highlighting support | Preview and test on the target platform |
Frequently Asked Questions
- 1
What is a Markdown code block?
A Markdown code block displays multi-line content as preformatted text. It is commonly used for source code, terminal commands, configuration files, and documentation examples.
- 2
How do I create a code block in Markdown?
Place three backticks on a line before your content and three matching backticks after it.
- 3
How do I add syntax highlighting?
Add a supported language identifier immediately after the opening fence, such as
javascript,python,bash, orjson. Highlighting depends on the Markdown renderer. - 4
What's the difference between inline code and a code block?
Inline code uses single backticks and is best for short snippets within a sentence. Fenced code blocks use triple backticks and are intended for multi-line content.
- 5
Why isn't my syntax highlighting working?
The language identifier may be missing or unsupported, or the Markdown renderer may not provide syntax highlighting.
- 6
Can I use code blocks inside Markdown lists?
Yes. Indent the fenced code block so it remains part of the relevant list item.
- 7
Can a Markdown code block contain HTML?
Yes. HTML placed inside a fenced code block is normally displayed as code rather than rendered as HTML.
- 8
Can Markdown create a copy button for code blocks?
Standard Markdown does not define copy buttons. Documentation frameworks, editors, and websites can add them through their own interface or scripts.
