If you write technical content, you will display code regularly. Markdown gives you two ways to do it: inline code for short snippets and fenced code blocks for multi-line examples.
Inline Code
Inline code is used for short references within a sentence — variable names, file paths, commands, or any short piece of code.
Wrap the text in single backticks `.
Use the `print()` function to display output.
Run `npm install` to install dependencies.
The config file lives at `~/.bashrc`.
Use the print() function to display output.
Run npm install to install dependencies.
The config file lives at ~/.bashrc.
HTML equivalent: <code>print()</code>
Fenced Code Blocks
For multi-line code, use a fenced code block. Start and end with three backticks ``` on their own lines.
```
function greet(name) {
return "Hello, " + name;
}
```
function greet(name) {
return "Hello, " + name;
}
Syntax Highlighting
Add a language identifier after the opening backticks to enable syntax highlighting. Most Markdown renderers (GitHub, GitLab, Hugo, many static site generators) support this.
```javascript
function greet(name) {
return `Hello, ${name}!`;
}
```
function greet(name) {
return `Hello, ${name}!`;
}
```python
def greet(name):
return f"Hello, {name}!"
```
def greet(name):
return f"Hello, {name}!"
```bash
npm install
npm run dev
```
npm install
npm run dev
Common Language Identifiers
| Language | Identifier |
|---|---|
| JavaScript | javascript or js |
| TypeScript | typescript or ts |
| Python | python or py |
| Bash / Shell | bash or sh |
| HTML | html |
| CSS | css |
| SQL | sql |
| JSON | json |
| YAML | yaml |
| Markdown | markdown or md |
| Go | go |
| Rust | rust |
Indented Code Blocks (older syntax)
You can also create a code block by indenting lines with four spaces. This is the original Markdown syntax but fenced blocks are preferred — they’re easier to read and support syntax highlighting.
This is an indented code block.
Every line is indented with 4 spaces.
This is an indented code block.
Escaping Backticks Inside Code
If your code contains a backtick, use double backticks for the outer wrapper:
`` Use `backticks` like this ``
Use `backticks` like this
Best Practices
- Always specify the language for code blocks — it makes content easier to read and helps syntax highlighters.
- Use inline code for anything shorter than a line.
- Don’t use code blocks just to indent text — use them only for actual code or terminal output.
Quick Reference
`inline code`
```language
multi-line
code block
```
Hope you find this helpful!
Keep smiling
Related Articles
Deepen your understanding with these curated continuations.
Horizontal Rules & Line Breaks in Markdown
Master horizontal rules and line breaks in Markdown. Learn how to separate sections with ---, ***, or ___ and when to use trailing spaces vs blank lines.
Images in Markdown - Complete Guide
Learn how to embed images in Markdown with alt text for accessibility and SEO. Covers inline syntax, local images, linked images, and reference-style syntax.
Mastering Blockquotes in Markdown - Complete Guide
Learn how to use blockquotes in Markdown for quotes, callouts, and notes. Covers basic syntax, nested blockquotes, multi-paragraph, and best practices.