ChatGPT解决这个技术问题 Extra ChatGPT

Changing image size in Markdown

I just got started with Markdown. I love it, but there is one thing bugging me: How can I change the size of an image using Markdown?

The documentation only gives the following suggestion for an image:

![drawing](drawing.jpg)

If it is possible I would like the picture to also be centered. I am asking for general Markdown, not just how GitHub does it.

For top image (like repo logo) I just make a "white padding" in original image before export to PNG.
You should really fix the accepted answer the to HTML one as the current answer uses non-standard markdown features that don't work broadly

T
Tieme

You could just use some HTML in your Markdown:

<img src="drawing.jpg" alt="drawing" width="200"/>

Or via style attribute (not supported by GitHub)

<img src="drawing.jpg" alt="drawing" style="width:200px;"/>

Or you could use a custom CSS file as described in this answer on Markdown and image alignment

![drawing](drawing.jpg)

CSS in another file:

img[alt=drawing] { width: 200px; }

Using inline style does not work in most websites (e.g. GitHub) site it will get sanitized. Prefer width and height instead as mentioned by @kushdillip.
The solution based on the alt attribute is very bad and you shouldn't use it, it breaks accessibility.
The alt in markdown put a caption, the alt in html does something completely different (put text if figure cannot be loaded).
Would it be a good idea to recommend a percentage instead of device-dependent pixels? E.g. <img src="drawing.jpg" alt="drawing" width="50%"/> ? Tested it on GitHub, it works nicely
This solution worked for me <img src="miro.medium.com/max/1400/1*bSLNlG7crv-p-m4LVYYk3Q.png" width="450" height="250">
s
sud007

With certain Markdown implementations (including Mou and Marked 2 (only macOS)) you can append =WIDTHxHEIGHT after the URL of the graphic file to resize the image. Do not forget the space before the =.

![](./pic/pic1_50.png =100x20)

You can skip the HEIGHT

![](./pic/pic1s.png =250x)

And Width

![](./pic/pic1s.png =x250)

also note that you cannot have a space after the '='. good:"![](./pic/pic1s.png =250x)", bad:"![](./pic/pic1s.png = 250x)"
Not in the standard, so it doesn't work with every Markdown parser
Doesn't seem to work with Redcarpet, which I use with Jekyll, so I'd go with HTML, as @Tieme answered. If you end up running your Markdown through a parser that likes the standard, the HTML will stand up.
doesn't work in Bitbucket wiki as well. it's wrongly converted into the title attribute.
Does not work, but the HTML works.
W
Willi Mentzel

The accepted answer here isn't working with any Markdown editor available in the apps I have used till date like Ghost, Stackedit.io or even in the StackOverflow editor. I found a workaround here in the StackEdit.io issue tracker.

The solution is to directly use HTML syntax, and it works perfectly:

<img src="http://....jpg" width="200" height="200" />

This worked great for me! Inline CSS wasn't working with GitHub Markdown but the "old school" height/width attributes worked just fine.
Good thing is that this one also works if you're trying to use a markdown viewer for local files in a browser extension/add-on.
Github likes this.
Note that on Stack Exchange sites you must use this exact format, and no other attributes (note even alt) are seemingly allowed (you may omit width or height, and the space before /> is optional, but other than that no extra whitespace is allowed). GitHub, by contrast, supports (at least) also alt and title attributes, and allows extra whitespace.
On Stack Overflow, the simple solution is to link to a different version of the picture. Each image you upload gets rendered in six different versions, which you can switch between by adding a character to indicate the desired size before the .png extension. For details, see meta.stackoverflow.com/questions/253403/…
B
Bartłomiej Semańczyk

Just use:

<img src="Assets/icon.png" width="200">

instead of:

![](Assets/icon.png)

Most Markdown implementations have a modified syntax for this so you don't need to insert the raw HTML tag, but this is the right thing to do if the implementation you're using doesn't have one.
This is compatible in github
Works on Gitlab
Does not work in Jupyter.
r
rudolfbyker

If you are writing MarkDown for PanDoc, you can do this:

![drawing](drawing.jpg){ width=50% }

This adds style="width: 50%;" to the HTML <img> tag, or [width=0.5\textwidth] to \includegraphics in LaTeX.

Source: http://pandoc.org/MANUAL.html#extension-link_attributes


It is even nicer than specifying size in points directly. I am glad this is the approach Pandoc has chosen!
@m0z4rt GitHub probably does not use PanDoc to render the MarkDown.
@rudolfbyker thank you so much. For Mkdocs-material it worked with adding -attr_list in markdown_extensions```` in mkdocs.yml```.
p
proximab

Combining two answers I came out with a solution, that might not look that pretty, but It works!

It creates a thumbnail with a specific size that might be clicked to brings you to the max resolutions image.

[<img src="image.png" width="250"/>](image.png)

https://i.stack.imgur.com/T3zhy.png

Thanks for feedback we know that this also works on:

GitLab

Jupyter Notebook


Excellent. Works with GitLab Enterprise.
If I have a markdown link: [myLink]: ./image.png, how can I apply this solution, to avoid duplicate src ? Example: [<img src="[myLink]" width="250"/>](image.png)
@VictorZanella You simply cannot do that, that's the drawback, but on the other hand you are able to use other image as a cover, so it's a feature! ;) You always can use some template engine. I've found one for GitHub as an action and I think that It should be convinced enough. variables-in-markdown
Thank you very much, this option is by far the best to use in jupyter notebooks
great answer thanks
s
sayth

Maybe this has recently changed but the Kramdown docs show a simple solution.

From the docs

Here is an inline ![smiley](smiley.png){:height="36px" width="36px"}.

And here is a referenced ![smile]

[smile]: smile.png
{: height="36px" width="36px"}

Works on github with Jekyll and Kramdown.


May have worked in the past but doesn't work now on Github. Adding an old fashioned tag with width and height still works.
This is the best solution if you're using Kramdown or Jekyll (which uses Kramdown by default).
Block attributes as shown here are a good option with kramdown. The syntax here is slightly wrong, which may be why @piratemurray is having trouble. It should be {: height=36 width=36}; this generates HTML attributes, so it should not have the px suffix. Alternately, you can use css with {: style="height:36px; width:36px"}.
Works for jekyll! thx. I don't even need height and width, just one is enough. ![alt text](image.png){:height="36px" }
I had to make a small change to get this to work properly in Jekyll. This answer as-written outputs malformed HTML, as the width and height attributes include the "px" part. For me I needed to use {:height="36" width="36"}
p
prabhu

Replace ![title](image-url.type) with <img src="https://image-url.type" width="200" height="200">


p
petermeissner

One might draw on the alt attribute that can be set in almost all Markdown implementations/renderes together with CSS-selectors based on attribute values. The advantage is that one can easily define a whole set of different picture sizes (and further attributes).

Markdown:

![minipic](mypic.jpg)

CSS:

img[alt="minipic"] { 
  max-width:  20px; 
  display: block;
}

Isn't this the same as Tieme's earlier answer?
This is a misuse of the alt attribute and hurts accessibility.
Yes, it is a hack BUT still seems to be the only thing that works across Markdown flavors. +1 for pointing that out (people using screen readers get problems with that right? They will get also problems with all those not bothering with using alt the right way).
P
Peter Mortensen

If you are using kramdown, you can do this:

{:.foo}
![drawing](drawing.jpg)

Then add this to your Custom CSS:

.foo {
  text-align: center;
  width: 100px;
}

I would recommend against setting the width only in CSS. It is useful to tell the browser how large the image element will be before the image and stylesheet are done loading so that it can optimize the layout of elements around the image without doing a reflow.
P
Peter Mortensen

Building on from Tiemes answer, if you're using CSS 3 you can use a substring selector:

This selector will match any image with an alt tag that ends with '-fullwidth':

img[alt$="-fullwidth"]{
  width:  100%;
  display: block;
}

Then you can still use the alt tag for its intended purpose to describe the image.

The Markdown for the above could be something like:

![Picture of the Beach -fullwidth](beach.jpg)

I've been using this in Ghost markdown, and it has been working well.


Works perfectly on kramdown+jekyll-3.1.2 as well.
If you don't need to render the image at full width, it's better to put the pixel size directly on the tag (not with CSS).
z
zardosht

If you are using reference style images in Gihub Flavored Markdown:

Here is an image of tree: 
![alt text][tree]{height=400px width=500px}


[//]: # (Image References)
[tree]: ./images/tree.png "This is a tree"


This didn't work for me. The reference linked mentions nothing about height/width
@ShubhamChaudhary The referenced link shows "what is a reference style image" in Github Flavored Markdown. Maybe you don't have a reference style image or are using another variant of Markdown.
Oh, you linked it to explain 'reference style'. In context of the answer, the link doesn't mention anything about the height/width syntax {height=100px width=100px}
s
symbolrush

For those intereseted in an rmarkdown and knitr solution. There are some ways to resize images in an .rmd file without the use of html:

You can simply specify a width for an image by adding {width=123px}. Don't introduce whitespace in between the brackets:

![image description]('your-image.png'){width=250px}

Another option is to use knitr::include_graphics:

```{r, fig.cap="image description", out.width = '50%'}
knitr::include_graphics('your-image.png')
```

How can I change both height and width? For the first option specifically. I tried putting height and width in the same {} but failed. Separate {}s fail too.
@NelsonGon: I never needed to specify both, since the height also scales, when width is specified. Therefore I don't know whether that would be possible and how to achieve it. Good question, though..
Thanks, I since figured I can do it like so: {height=x width=y}. It seems this syntax does not recognize commas but I could specify other attributes including style elements.
A
Alfredo Castaneda

This one works for me it's not in one line but i hope it works for you.

<div>
<img src="attachment:image.png" width="500" height="300"/>
</div>

<div><img src="attachment:image.png" width="500" height="300"/></div> <- one line :D
U
Umang Desai

I came here searching for an answer. Some awesome suggestions here. And gold information pointing out that markdown supports HTMl completely!

A good clean solution is always to go with pure html syntax for sure. With the tag.

But I was trying to still stick to the markdown syntax so I tried wrapping it around a tag and added whatever attributes i wanted for the image inside the div tag. And it WORKS!!

<div style="width:50%">![Chilling](https://www.w3schools.com/w3images/fjords.jpg)</div>

So this way external images are supported!

Just thought I would put this out there as it isn't in any of the answers. :)


You cant put markdown inside of HTML, you will need to replace ![chilling](link) with <img src="link" alt="chilling">.
M
Majid Golshadi

You could use this one as well with kramdown:

markdown
![drawing](drawing.jpg)   
{:.some-css-class style="width: 200px"}

or

markdown
![drawing](drawing.jpg)   
{:.some-css-class width="200"}

This way you can directly add arbitrary attributes to the last html element. To add classes there is a shortcut .class.secondclass.


P
Peter Mortensen

I scripted the simple tag parser for using a custom-size img tag in Jekyll.

https://gist.github.com/nurinamu/4ccf7197a1bdfb0d7079

{% img /path/to/img.png 100x200 %}

You can add the file to the _plugins folder.


I
Iulian Onofrei

I know that this answer is a bit specific, but it might help others in need.

As many photos are uploaded using the Imgur service, you can use the API detailed here to change the size of the photo.

When uploading a photo in a GitHub issue comment, it will be added through Imgur, so this will help a lot if the photo is very big.

Basically, instead of http://i.imgur.com/12345.jpg, you would put http://i.imgur.com/12345m.jpg for medium sized image.


Stack Overflow has a similar facility; see the comment I left on another answer for a link. stackoverflow.com/questions/14675913/…
m
mavericks

For all looking for solutions which work in R markdown/ bookdown, these of the previous solutions do/do not work or need slight adaption:

Working

Append { width=50% } or { width=50% height=50% } ![foo](foo.png){ width=50% } ![foo](foo.png){ width=50% height=50% } Important: no comma between width and height – i.e. { width=50%, height=30% } won't work!

![foo](foo.png){ width=50% }

![foo](foo.png){ width=50% height=50% }

Important: no comma between width and height – i.e. { width=50%, height=30% } won't work!

Append { height="36px" width="36px" } ![foo](foo.png){ height="36px" width="36px" } Note: {:height="36px" width="36px"} with colon, as from @sayth, seems not to work with R markdown

![foo](foo.png){ height="36px" width="36px" }

Note: {:height="36px" width="36px"} with colon, as from @sayth, seems not to work with R markdown

Not working:

Append =WIDTHxHEIGHT after the URL of the graphic file to resize the image (as from @prosseek) neither =WIDTHxHEIGHT ![foo](foo.png =100x20) nor =WIDTH only ![foo](foo.png =250x) work

after the URL of the graphic file to resize the image (as from @prosseek)

neither =WIDTHxHEIGHT ![foo](foo.png =100x20) nor =WIDTH only ![foo](foo.png =250x) work


A
Ali Safari

If you have one image in each md file, one handy way to control image size is:

adding css style as follows:

## Who Invented JSON?
`Douglas Crockford`

Douglas Crockford originally specified the JSON format in the early 2000s.
![Douglas Crockford](img/Douglas_Crockford.jpg)

<style type="text/css">
    img {
        width: 250px;
    }
</style>

and the output will be like: [![enter image description here][1]][1]

If you have more images in each md page, then the handy way to control each image or each customized tag is to define each element in css. For this case for the img tag we could have:



 
//in css or within style tags:
    img[alt="Result1"] {
    width: 100px;
    }

    img[alt="Result2"] {
    width: 200px;
    }
    img[alt="Result3"] {
    width: 400px;
    }

// try in md one of the methods shown below to insert image in your document:
 <br/>
<img src="https://i.stack.imgur.com/xUb54.png" alt="Result1"> <br/>
<img src="https://i.stack.imgur.com/xUb54.png" alt="Result2"> <br/>
<img src="https://i.stack.imgur.com/xUb54.png" alt="Result3"> <br/>

<br/>

in md:<br/>
![Result1](img/res-img-1.png) <br/>

![Result2](img/res-img-2.png) <br/>

![Result3](img/res-img-3.png) 
[1]: https://i.stack.imgur.com/xUb54.png

A
Antoni Parellada

For those using Markdown on Google Colaboratory, there is no need to have the image uploaded to the session storage folder, or linked on Google Drive. If the image has a URL, and it can be included on the Jupyter notebook, and its size changed as follows:

<img src="https://image.png" width="500" height="500" />

https://i.stack.imgur.com/6aZkC.png


P
Peter Mortensen

For R-Markdown, neither of the above solutions worked for me, so I turned to regular LaTeX syntax, which works just fine.

\begin{figure}
 \includegraphics[width=300pt, height = 125 pt]{drawing.jpg}
\end{figure}

Then you can use e.g. the \begin{center} statement to center the image.


+1, but better just \centering after \begin{figure} or nothing, if you use ` \includegraphics[width=\linewidth]{drawing.jpg}` that I think that should be the default pandoc output at least when the image is wider that the text.
P
Peter Mortensen

When using Flask (I am using it with flat pages)... I found that enabling explicitly (was not by default for some reason) 'attr_list' in extensions within the call to markdown does the trick - and then one can use the attributes (very useful also to access CSS - class="my class" for example...).

FLATPAGES_HTML_RENDERER = prerender_jinja

and the function:

def prerender_jinja(text):
    prerendered_body = render_template_string(Markup(text))
    pygmented_body   = markdown.markdown(prerendered_body, extensions=['codehilite', 'fenced_code', 'tables', 'attr_list'])
    return pygmented_body

And then in Markdown:

![image](https://octodex.github.com/images/yaktocat.png "This is a tooltip"){: width=200px}

C
Community

There is way with add class and css style

![pic][logo]{.classname}

then write down link and css below

[logo]: (picurl)

<style type="text/css">
    .classname{
        width: 200px;
    }
</style>

Reference Here


M
MrFun

Resizing Markdown Image Attachments in Jupyter Notebook

I'm using jupyter_core-4.4.0 & jupyter notebook.

If you're attaching your images by inserting them into the markdown like this:

![Screen%20Shot%202019-08-06%20at%201.48.10%20PM.png](attachment:Screen%20Shot%202019-08-06%20at%201.48.10%20PM.png)

These attachment links don't work:

<img src="attachment:Screen%20Shot%202019-08-06%20at%201.48.10%20PM.png" width="500"/>

DO THIS. This does work.

Just add div brackets.

<div>
<img src="attachment:Screen%20Shot%202019-08-06%20at%201.48.10%20PM.png" width="500"/>
</div>

Hope this helps!


THE BEST ANSWER!
k
kgkmeekg

For future reference:

Markdown implementation for Joplin allows controlling the size of imported images in the following manner:

<img src=":/7653a812439451eb1803236687a70ca" width="450"/>

This feature was requested here and as promised by Laurent this has been implemented.

It took me a while to figure the Joplin specific answer.


A
Andrea

The sheer said above, did work on my Github Readme.md document.

However my real issue was, that the image was inside a table cell, just compressing the text in the beside cell. So the other way was to set columns width in Markdown tables, but the solutions did not really seem enough markdownish for my morning.

At last I solved both problems by simply forcing the beside text cell with as much "& nbsp;" as I needed.

I hope this helps. Bye and thanks everybody.


d
ddri

The addition of relative dimensions to the source URL will be rendered in the majority of Markdown renderers.

We implemented this in Corilla as I think the pattern is one that follows expectations of existing workflows without pushing the user to rely on basic HTML. If your favourite tool doesn't follow a similar pattern it's worth raising a feature request.

Example of syntax:

![a-kitten.jpg](//corilla.com/a-kitten-2xU3C2.jpg =200x200)

Example of kitten:

https://i.stack.imgur.com/VVQET.jpg


A shame that it doesn't work on GitHub at present, but I'd suggest raising a feature request all the same.
0
0llie

Via plain backward compatible MD:

![<alt>](<imguri>#<w>x<h> "<title>")

where w, h defines the bounding box to aspect fit into, as eg in Flutter package https://pub.dev/packages/flutter_markdown

Code: https://github.com/flutter/packages/blob/9e8f5227ac14026c419f481ed1dfcb7b53961475/packages/flutter_markdown/lib/src/builder.dart#L473

Reconsider html workarounds breaking compatibility as people might use native/non-html components/apps to display markdown.


Y
Yannickv

If changing the initial markdown is not an option for you, this hack might work:

newHtml = oldHtml.replace(/<img/g, '<img height="100"');

I used this to be able to resize images before sending them in an email (as Outlook ignores any image css styling)