Resizing Images in Markdown: Why There Is No Size Syntax
Quick answer
Markdown cannot set an image's size. The original documentation says so directly, and the usual fix is to fall back to HTML: an img tag with width and height in pixels. A few tools, Pandoc among them, add their own attribute syntax on top. If neither is available, resize the image file itself.
Here's the portable version:
<img src="photo.jpg" alt="A desk with a laptop" width="400">The rest of this article covers why the gap exists, the three ways to work around it, and what to check when the result looks wrong.
Why Markdown has no size syntax
The Markdown syntax documentation on Daring Fireball is blunt about it: Markdown has no syntax for specifying the dimensions of an image, and if that matters to you, you can use regular HTML img tags. The same page describes Markdown as not a replacement for HTML, and says that for any markup Markdown doesn't cover, you simply use HTML itself. Image size is one of those cases. Small syntax gaps like this are common, and they are why details such as how bullet markers affect Markdown lists differ from one tool to the next.
People have tried to close the gap. A long CommonMark discussion thread on image dimensions collects the proposals: a size after the URL such as 100x100, an =80x10 form chosen so the numbers can't be mistaken for part of the URL, and curly-brace attributes. Some participants argued against any of them, on the grounds that Markdown is for writers and fixed dimensions don't fit responsive layouts. That thread is community discussion, not a decision by the spec's maintainers, and none of those forms is part of standard Markdown.
So the answer depends on where your text will be rendered.
Step-by-step: sizing an image
Use an HTML img tag
This is the most portable option. Nearly every Markdown renderer passes HTML through. Put the tag on its own line, with blank lines above and below:
<img src="diagram.png" alt="Flow of a sync request" width="400" height="250">Both values are pixels. If you set only width, the browser works out the height from the image's own proportions, so nothing gets stretched. MDN's reference for the img element covers both attributes.
Two details from Daring Fireball's page are worth remembering. Block-level HTML must be separated from the surrounding text by blank lines. And Markdown formatting isn't processed inside block-level HTML tags, so write the alt text as plain words, not Markdown.
For images that must never overflow a narrow screen, markdownlang's image guide shows an inline style:
<img src="diagram.png" alt="Flow of a sync request" style="max-width: 100%; height: auto;">Some renderers strip style attributes, so test this one in your target.
Use extension syntax where your tool supports it
Pandoc lets you attach attributes to an image in curly braces right after it, as described in the Pandoc User's Guide:
{width=50% height=20px}Without a unit, the number means pixels. Pandoc accepts px, cm, mm, in, inch and %, with no space between the number and the unit. It's the neatest syntax, but it belongs to Pandoc, not Markdown. In a renderer that doesn't know it, the braces may simply show up as text after the image.
Resize the file instead
The approach that works everywhere is to export the image at the size you want it displayed. There's no attribute to go wrong. Leave some headroom for sharp screens: markdownlang suggests a source around 800 pixels wide for high-resolution displays, which you can then display at a smaller width with the HTML tag.
Common problems and fixes
- The tag shows up as plain text, or disappears. The renderer doesn't allow raw HTML. Resize the file, or check whether the tool has its own sizing option.
- The image looks stretched. You gave both
widthandheightwith the wrong ratio. Set only one and let the other follow. - The page jumps while loading. Without dimensions the browser doesn't know how much room to reserve. MDN notes that giving both
widthandheightlets it set that space aside before the image arrives. - The image runs off the edge of a phone screen. Use
max-width: 100%as above, or a smaller fixed width. - The curly braces print literally. The renderer doesn't support Pandoc-style attributes. Switch to the HTML tag.
The last two problems share a root cause with why a regex works in one editor and not another: the text is fine, but each tool reads it with its own rules.
Doing this with Carets
Because the size lives in your text, not in an app's database, the choice you make should survive a move between tools. Plain files help here: the HTML tag or attribute block is just characters in a Markdown file, and any editor can read it.
Carets is a native notes and code editor for iPhone, iPad and Mac. It handles Markdown, plain text and code in one place, with syntax highlighting, so an img tag or an attribute block is easy to spot and edit inside a note. Your notes are organized into files, projects and tags, and they stay plain text. We think that makes it the best fit for people who want their Markdown portable and their edits quick. Carets doesn't resize images for you. You decide the size in the text.
Download Carets for iPhone, iPad and Mac on the App Store.
Frequently Asked Questions
Can you set an image width in plain Markdown?
Not in the original syntax. Daring Fireball's Markdown documentation says Markdown has no syntax for specifying image dimensions and suggests regular HTML img tags.
What is the most portable way to resize a Markdown image?
An HTML img tag with width and height attributes in pixels, as long as the renderer allows raw HTML. Where it doesn't, resize the file itself.
Does the curly-brace width syntax work everywhere?
No. Pandoc supports an attribute block after an image, but other renderers may show the braces as literal text. Check your target first.
Should I set both width and height?
Setting both lets the browser reserve space before the image loads. Setting only width keeps the aspect ratio, so the image can't be stretched.