Markdown Link Image Syntax: Embedding, Linking and Reference-Style Conversion

How to write a Markdown image, link to an image, make a clickable image, and convert inline image links to reference style without changing alt text or paths.

Markdown has three related patterns that are easy to confuse: an image that is displayed in the page, a text link that points to an image file, and an image that is itself clickable. Each uses square brackets and a destination, but small differences, such as a leading exclamation mark or the way brackets are nested, change what the reader sees. This guide explains each pattern, shows how to convert them between inline and reference style, and covers the checks that keep alt text, titles and paths intact.

Note on the examples: the file names and paths below, such as images/chart.png, are placeholders chosen for illustration. Replace them with your own paths. The syntax follows the widely documented CommonMark behavior. Individual renderers and editors can differ, and no specific tool's output has been verified here.

What each pattern does to your text

Pattern | Syntax shape | Result when rendered
Embedded image | alt text | The image is displayed in place, and the alt text is used when the image cannot be shown or for screen readers
Link to an image | link text | Clickable text that opens the image file, with no image shown inline
Clickable (linked) image | alt text | The image is displayed, and clicking it follows the outer link
Reference-style image | ![alt text][label] plus a separate definition line | Same display as an embedded image, with the path stored elsewhere in the document

The only difference between embedding an image and linking to it is the exclamation mark. Removing it turns a displayed image into a plain text link, and adding it turns a text link into an embedded image. Search-and-replace operations that touch the character immediately before an opening bracket can therefore change how content renders.

Embedding an image

Write an exclamation mark, the alt text in square brackets, and the destination in parentheses. You can add an optional title in quotes after the path, separated by a space. Many renderers show the title as a tooltip.

![Quarterly sales chart](images/chart.png)
![Quarterly sales chart](images/chart.png "Sales, Q1 to Q4")

Alt text is the description that stands in for the image. Write it to describe what the image conveys, not its file name. Standard Markdown has no syntax for width or height. Where sizing is needed, many renderers accept an HTML img tag instead, but whether raw HTML is allowed depends on the platform.

Linking to an image without displaying it

To give readers a link that opens the image file, use ordinary link syntax with no exclamation mark. This suits large files, diagrams that are better viewed full size, or pages where an inline image would interrupt the text.

[View the full-size chart](images/chart.png)

Making an image clickable

A clickable image is an embedded image placed inside the text part of a link. The inner brackets hold the image, and the outer parentheses hold the place the click goes. A common use is linking a thumbnail to the full-size file.

[![Quarterly sales chart](images/chart-thumb.png)](images/chart.png)

Read it from the inside out. The part Quarterly sales chart is the image. Everything inside the outer square brackets becomes the link text, and (images/chart.png) is the link target. If a bracket or parenthesis is missing, the renderer will usually show the raw characters rather than the image, so count the brackets when the output looks wrong.

Reference-style images

Reference style moves the destination out of the sentence and into a definition line elsewhere in the document, usually at the end. The image keeps its alt text, and a label in a second pair of brackets connects it to the definition. Definition lines do not appear in the rendered output.

![Quarterly sales chart][sales-chart]

[sales-chart]: images/chart.png "Sales, Q1 to Q4"

CommonMark also documents two shorter forms. In a collapsed reference, the empty second brackets mean the alt text doubles as the label. In a shortcut reference, the second brackets are omitted entirely. Both need a matching definition.

![sales chart][]
![sales chart]

[sales chart]: images/chart.png

Under CommonMark, labels are matched case-insensitively and runs of internal whitespace are treated as one space, so [Sales Chart] and [sales chart] point to the same definition. If two definitions share a label, the first one is used. Not every Markdown flavor follows these rules exactly, so check your renderer if labels differ only by case or spacing.

Reference style also works for clickable images. Both the image and the outer link can use labels:

[![Quarterly sales chart][chart-thumb]][chart-full]

[chart-thumb]: images/chart-thumb.png
[chart-full]: images/chart.png

Converting inline image links to reference style

Converting means rewriting each inline destination as a label plus a definition, while keeping the rendered result the same. Whether you convert by hand, with an editor command or with a script, the steps are the same.

  • 1. Find each inline image or link and note its exact alt or link text, destination and optional title.
  • 2. Choose a label that is unique in the document. Reuse an existing label only when the destination and title are identical.
  • 3. Replace the parenthesized part with the label in square brackets, and keep the leading exclamation mark if it was an image.
  • 4. Add a definition line in the form [label]: destination "optional title" and move the title into the definition.
  • 5. For clickable images, convert the inner image and the outer link separately so that each gets its own label.
  • 6. Render the document before and after the conversion and compare the two.

Example of a before and after conversion:

Before:
See the trend ![Quarterly sales chart](images/chart.png "Sales, Q1 to Q4") below.

After:
See the trend ![Quarterly sales chart][sales-chart] below.

[sales-chart]: images/chart.png "Sales, Q1 to Q4"

Converting back from reference to inline style reverses these steps: look up each label's definition, insert the destination and title into parentheses, and remove the definition once no other reference uses it.

Characters that break image links

Most conversion errors come from characters that have meaning in Markdown syntax. The behavior below is documented in CommonMark. Other flavors may handle some of these cases differently.

  • Spaces in the path: a space ends an inline destination unless the destination is wrapped in angle brackets, as in Logo, or the space is percent-encoded as %20.
  • Parentheses in the path: balanced pairs are usually accepted, but an unbalanced parenthesis ends the destination early. Wrap the path in angle brackets or escape the parenthesis with a backslash.
  • Square brackets in alt text: escape them with a backslash, as in Figure 1, so they are not read as the end of the alt text.
  • Quotes in titles: if the title contains a double quote, use single quotes or parentheses around the title, or escape the inner quote with a backslash.
  • Case in file names: many servers and file systems treat Chart.png and chart.png as different files, so keep the path's capitalization exactly as it is.

Pitfalls when using regex or bulk find-and-replace

A simple pattern like the one below is often used to find inline images. It works on plain cases, but it is an illustration, not a complete Markdown parser:

!\[([^\]]*)\]\(([^)\s]+)(?:\s+"([^"]*)")?\)
  • It fails on escaped brackets in alt text and on parentheses inside paths.
  • It matches the inner image of a clickable image but not the outer link, so a naive replacement can leave the outer link half-converted.
  • It does not know about code. Examples inside inline code spans or fenced code blocks will also be rewritten unless those regions are excluded first.
  • It can match across lines, or fail to, depending on your engine's flags. Test it on a copy of the file.
  • It ignores reference-style images, so a document mixing both styles needs a second pass.

For anything beyond a small, predictable file, a Markdown-aware converter or parser is safer than regex, because it already handles nesting, escapes and code regions. Which tools do this reliably could not be confirmed from the available evidence, so test any tool on a sample file before running it on real content.

Verifying the result

  • Preview the document and confirm that every image that was displayed before is still displayed, and every text link still opens the same file.
  • Compare alt text and titles character by character, especially capitalization, punctuation and escaped brackets.
  • Search for orphaned labels, meaning references with no definition, and for unused definitions. An orphaned reference usually renders as literal bracketed text.
  • Check that code blocks and inline code still contain their original Markdown examples unchanged.
  • If the file is under version control, review the diff to confirm that only link syntax changed and no surrounding prose was altered.

Known gaps

No source-specific evidence was available about particular Markdown editors, converters or platforms. This guide therefore describes the general CommonMark syntax rather than any product's behavior. Support for image sizing, raw HTML, label matching rules and title tooltips varies between renderers, so confirm these details in your platform's documentation or with a test file.