How to Use Backticks Inside Markdown Code
Write inline code and fenced code blocks that contain literal backticks (`).
March 3, 2026
Inline code is wrapped in backticks:
Use `printf` to print.<p>Use <code>printf</code> to print.</p>Use printf to print.
But if your code itself contains a backtick, one backtick as a delimiter breaks:
`a ` b`<p><code>a </code> b`</p>a b`
The fix: Use a longer delimiter than the content. If the content contains a single backtick, wrap it with double backticks:
``a ` b``<p><code>a ` b</code></p>a ` b
If your code contains two consecutive backticks, you need at least three backticks as the delimiter:
```a `` b```<p><code>a `` b</code></p>a `` b
If your code starts or ends with a backtick, add a space on both sides inside the delimiter (so the rendered code doesn’t include the space):
`` echo `command` ``<p><code>echo `command`</code></p>echo `command`
The same idea applies to fenced code blocks: the fence must be longer than any backtick run inside. Here a 4-backtick fence can safely contain a 3-backtick fence:
````md
Here is a nested fenced block:
```js
console.log("nested")
```
````<pre><code class="language-md">Here is a nested fenced block:
```js
console.log("nested")
```
</code></pre>Here is a nested fenced block:
```js
console.log("nested")
```
Why this happens
Markdown inline code and fenced code blocks are both started and ended with backtick delimiters.
When the parser sees a backtick run (like ``, ````, etc.), it uses that run length as the delimiter length, and then looks for a matching run of the same length to end the code span/block.
So the rule of thumb is:
- Pick a delimiter that's at least one backtick longer than the longest backtick sequence inside the code.
- Use the same length for the opening and closing delimiter.
- If the code begins or ends with a backtick, pad the code with a space on both sides inside the delimiters.
From the CommonMark specification: