MarkdownViewer

Markdown Code Block: Inline and Fenced Syntax

Code is where Markdown earns its keep: a few backticks turn text into fixed-width, syntax-highlighted code and stop the renderer from interpreting markup characters inside it. There are two forms — inline and fenced — and a language tag that adds highlighting.

Inline code

Wrap short snippets in single backticks: `` `const x = 1` ``. Everything inside is literal — asterisks, underscores and angle brackets are not interpreted. This is how you mention a variable, a command name or a filename in a sentence.

Fenced code blocks

For anything multi-line, put triple backticks on their own line before and after the code. Add a language identifier immediately after the opening fence to get syntax highlighting: `````js```, `````python```, `````bash```, `````json```, `````html``` and so on. GitHub-Flavoured Markdown and most editors support these.

Escaping and nested backticks

If your code contains backticks, use more backticks on the fence than the longest run inside it — for example four backticks around a snippet that contains three. Inside a list item, indent the fence to the item's content level so it stays associated with that item.

Common mistakes

  1. Forgetting the blank line before a fence inside a list
  2. Using a language tag no highlighter knows — harmless, it just renders plain
  3. Mixing tabs and spaces for indentation so a fenced block is read as an indented code block
  4. Not closing the fence, which swallows the rest of the document as code

Frequently Asked Questions

How do I add syntax highlighting?
Put the language name right after the opening fence: ```` ```javascript ````. The renderer highlights the block for that language. Unknown languages fall back to plain text.
How do I show a backtick inside code?
Use a fence with more backticks than the longest run inside the code, or escape single inline backticks with a backslash. Fenced blocks with four backticks are the reliable approach.
What is the difference between inline and fenced code?
Inline code uses single backticks and sits in a sentence; fenced blocks use triple backticks on their own lines and hold multi-line code, optionally with a language for highlighting.
Can I put a code block inside a list?
Yes. Leave a blank line, then indent the fenced block to the list item's content level. Without the indent it ends the list.

Related Markdown Tools