# all·markdown

How to write code blocks in Markdown

Try it - edit the markdown and watch the preview

Visual

Open the full editor Runs in this tab, nothing is uploaded.

Three backticks open and close a code block. A language name after the opening fence turns on syntax highlighting:

```js
const site = 'allmarkdown.com';
```

Inside a block, nothing is interpreted as Markdown. Asterisks stay asterisks and underscores stay underscores, which is exactly what you want when the content is code, a file path or a configuration snippet.

Inline code

Single backticks mark a span inside a sentence: `npm run build`. Use it for anything the reader might copy: commands, filenames, function names, literal values.

When the code contains backticks

Use more backticks on the outside than appear on the inside:

That is how the example at the top of this page is written.

Indented blocks still work

Four leading spaces also make a code block. It is the original syntax and it still parses, but it cannot carry a language name and it is easy to confuse with list indentation. Fenced blocks are the better default.

Common questions

How do I write a code block?
Put three backticks on the line before and the line after the code. Add a language name after the opening fence, like ```js, and most renderers will syntax-highlight it.
What is the difference between inline code and a code block?
Single backticks mark a short span inside a sentence, which stays in the flow of the paragraph. Triple backticks create a separate block that preserves line breaks and indentation exactly.
How do I show backticks inside a code span?
Use more backticks on the outside than appear on the inside. Two backticks can wrap a span containing one, and a four-backtick fence can wrap a block containing a three-backtick fence.
What language names are supported?
That depends on the renderer rather than on Markdown. Common ones (js, ts, python, bash, json, yaml, sql, go, rust, html, css) are near-universal. An unrecognized name is not an error; the block simply renders unhighlighted.
Is indenting four spaces still valid?
Yes, and it is the original syntax, but it cannot carry a language name and it collides with list indentation. Fenced blocks are clearer and are what almost everyone writes now.

Sources