Markdown Code Blocks

Share source code, terminal commands, configuration files, and plain text without losing formatting. Markdown code blocks preserve indentation, whitespace, and special characters exactly as written.

This guide explains inline code, fenced code blocks, syntax highlighting, language identifiers, platform compatibility, common mistakes, and practical examples you can copy directly into your documentation.

🟢 Practice as you learn: Open the Markdown Editor to write and preview fenced code blocks, or keep the Markdown Cheat Sheet open for quick syntax reference.

Open EditorDownload PDF

Markdown Code Blocks

Use Markdown code blocks whenever formatting matters. They're commonly used for source code, terminal commands, configuration files, API requests, SQL queries, JSON responses, and documentation examples.

Unlike normal paragraphs, code blocks preserve indentation, spacing, and special characters without applying Markdown formatting. This makes examples easier to read, copy, and reuse.

Most Markdown editors support fenced code blocks, including GitHub Flavored Markdown (GFM), GitLab, VS Code, Obsidian, Discord, and many static site generators.

Inline Code vs Fenced Code Blocks

Markdown provides two ways to display code, depending on how much content you need to show.

Inline Code#

Use inline code for short commands, filenames, variables, keyboard shortcuts, or small code snippets that appear inside a sentence.

Example

Run `npm install` before starting the project.

Output

Live Preview

Run npm install before starting the project.

Fenced Code Blocks#

Use fenced code blocks whenever your example spans multiple lines. Wrap the content with three backticks on separate lines.

Example

```bash
npm install
npm run dev
```

Fenced code blocks are the preferred choice for programming tutorials, API documentation, configuration files, scripts, HTML snippets, SQL queries, JSON examples, and GitHub README files.

💡 Pro Tip: Use inline code for individual commands or keywords. Switch to fenced code blocks as soon as the example requires multiple lines or preserved indentation.

Creating a Markdown Code Block

Create a fenced code block by placing three backticks before and after your content.

Example

```text
Hello World
```

Output

Live Preview

Hello World

Markdown treats everything inside the fence as literal text. Characters such as #, *, _, and [] are displayed exactly as written instead of being interpreted as Markdown formatting.

If your Markdown editor supports syntax highlighting, you can specify the programming language immediately after the opening backticks. The next section explains how that works.

Syntax Highlighting

Most modern Markdown renderers can highlight keywords, strings, comments, and other language-specific syntax automatically. To enable highlighting, add a supported language identifier immediately after the opening three backticks.

General syntax

```language
Your code here
```

Without a language identifier, the code still renders correctly, but many editors display it as plain text.

Common Language Identifiers#

The language identifier tells the Markdown renderer which syntax highlighting rules to apply.

Language Identifier Common Use
JavaScript javascript or js Web applications
TypeScript typescript or ts Type-safe JavaScript
Python python Scripts, automation, APIs
Bash / Shell bash or sh Terminal commands
SQL sql Database queries
JSON json API responses
YAML yaml Configuration files
HTML html Markup examples
CSS css Stylesheets
Markdown markdown Markdown examples
Dockerfile dockerfile Docker images
PHP php Server-side scripting

Most documentation only needs a handful of language identifiers. If a language isn't recognized, the code is still displayed but may not receive syntax highlighting.

Common Code Block Examples

The following examples cover the formats developers use most often in GitHub README files, developer documentation, tutorials, and API references.

JavaScript#

function greet(name) {
  console.log(`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

README Installation#

npm install
npm run dev

API Response#

{
  "success": true,
  "message": "User created successfully",
  "id": 123
}

Dockerfile#

FROM node:20

WORKDIR /app

COPY . .

RUN npm install

CMD ["npm", "start"]

Code Blocks Inside Lists#

Indented fenced code blocks work correctly inside list items.

1. Install dependencies.

   ```bash
   npm install
   npm run dev
   ```

2. Start the development server.

If you need to include code inside a Markdown table, use inline code where possible. Most Markdown renderers don't support fenced code blocks inside standard table cells.

💡 Workflow Tip: Need to see how fenced code blocks render in a webpage? Open the Markdown to HTML converter to preview the generated HTML while preserving your Markdown source.

Advanced Code Block Techniques

Indented Code Blocks#

Markdown also supports code blocks created with four leading spaces.

Example

    function hello() {
        console.log("Hello");
    }

Indented code blocks are valid, but fenced code blocks are generally easier to edit, support syntax highlighting, and work more consistently across Markdown editors.

Escaping Triple Backticks#

If you need to display a fenced code block inside another fenced code block, wrap the outer example with four backticks instead of three.

Example

````markdown
```javascript
console.log("Hello");
```
````

Using a longer outer fence prevents Markdown from closing the inner code block too early.

Using Tildes Instead of Backticks#

Many Markdown parsers also support tildes (~~~) as an alternative fence.

~~~python
print("Hello, World!")
~~~

This is especially useful when your example already contains multiple backticks.

💡 Pro Tip: Choose one fence style and use it consistently throughout the document. Mixing backticks and tildes can make long documentation harder to maintain.

Platform Compatibility

Most modern Markdown editors support fenced code blocks, but the available features can vary depending on the Markdown renderer.

Platform Syntax Highlighting Notes
GitHub (GFM) ✅ Full Supports fenced code blocks and a wide range of language identifiers in README files, Wikis, Issues, Discussions, and Pull Requests.
GitLab ✅ Full Uses GitHub-style fenced code blocks with syntax highlighting.
VS Code ✅ Full Markdown Preview renders fenced code blocks with language highlighting.
Obsidian ✅ Full Supports fenced code blocks and language identifiers out of the box.
Discord ✅ Partial Supports fenced code blocks and highlighting for many popular languages, but advanced Markdown features are limited.
Notion ⚠️ Partial Converts fenced code blocks into native code blocks when importing Markdown.
Reddit ⚠️ Limited Code blocks are supported, but syntax highlighting depends on the interface and client.

💡 Pro Tip: If your documentation will be published on multiple platforms, preview the rendered output before publishing. Syntax highlighting and supported languages may differ between renderers.

Best Practices

A few simple habits make Markdown code blocks easier to read and maintain.

  • Use fenced code blocks instead of indented blocks whenever possible.
  • Specify the programming language to enable syntax highlighting.
  • Keep each code block focused on a single concept.
  • Use descriptive variable names in examples.
  • Keep indentation consistent throughout the block.
  • Separate unrelated examples into different code blocks.
  • Use inline code for commands, filenames, variables, and short snippets.
  • Preview the rendered output before publishing.

💡 Workflow Tip: After writing documentation, preview it in the Markdown Editor to verify formatting before exporting with the Markdown to PDF tool.

Common Mistakes

Most rendering problems are caused by small formatting errors.

Problem Cause Solution
Code isn't rendered as a block Missing opening or closing fence Make sure both fences use the same number of backticks.
Syntax highlighting doesn't appear Missing or unsupported language identifier Add a valid language such as javascript, python, or bash.
Code formatting looks inconsistent Mixed tabs and spaces Use consistent indentation throughout the example.
Nested code block breaks Inner fence matches the outer fence Wrap the outer example with four backticks instead of three.
Markdown formatting appears inside the code Fence wasn't closed correctly Check that the closing fence is present and matches the opening fence.

Frequently Asked Questions

  1. 1

    What is a Markdown code block?

    A Markdown code block displays code exactly as written without applying Markdown formatting. It's commonly used for source code, terminal commands, configuration files, and documentation examples.

  2. 2

    How do I create a fenced code block?

    Place three backticks on a line before your content and three matching backticks after it. Add a language identifier after the opening backticks if you want syntax highlighting.

  3. 3

    What's the difference between inline code and fenced code blocks?

    Inline code uses single backticks for short snippets inside a sentence. Fenced code blocks use triple backticks and are intended for multi-line examples.

  4. 4

    How do I enable syntax highlighting?

    Add a supported language identifier immediately after the opening fence. Example: ```markdown `javascript console.log("Hello"); ` ```

  5. 5

    Why isn't syntax highlighting working?

    Common causes include: - Missing language identifier - Unsupported language name - Missing closing fence - A Markdown renderer that doesn't support syntax highlighting

  6. 6

    Can I use code blocks inside lists?

    Yes. Indent the fenced code block so it belongs to the list item.

  7. 7

    Can Markdown code blocks contain HTML?

    Yes. HTML inside a fenced code block is displayed as code rather than rendered as HTML.

  8. 8

    Can Markdown add a copy button to code blocks?

    No. Standard Markdown doesn't include copy buttons. Many documentation frameworks and static site generators add this feature automatically.