Markdown offers a straightforward, plain-text syntax for formatting documents, including embedding images, for content creation. Its simplicity allows authors to focus on content rather than complex HTML tags. However, Markdown’s strength in simplicity can also be a limitation when you need more control on how your image are presented. This article explores the fundamental Markdown syntax for images.
Here’s the syntax for adding images in Markdown. The title is optional:
This results in the following HTML:
With the markdown syntax for links, here’s how you would have a linked image:
This is how the above Linked image looks:
Markdown by itself does not support directly resizing the images. However, we can workaround this problem using the HTML <img
> tag. Let’s resize the above image:
This will resize the image to the mentioned width, in this case, to 400.
This method is supported by most platforms that offers Markdown with HTML support, including GitHub and Jupyter Notebook.
Alt text, short for alternative text, is a short description that explains what an image shows. It’s mainly used by screen readers to help people who are visually impaired understand the content of images. It also comes in handy when images don’t load properly. So it is critical that alt texts are clear, specific, and focused on what’s important about the image in its context.
Here are some of the best practices:
To know more about accessibility, you can refer to Web Accessibility For Beginners.
While Markdown provides a simple and efficient syntax for embedding images, it has inherent limitations when it comes to advanced styling or providing fallback options for different scenarios. For these cases, you can leverage raw HTML directly within your Markdown document. Most Markdown processors support embedding raw HTML, making it a powerful “fallback” when Markdown’s syntax isn’t sufficient.
Opt for HTML <img>
tags over Markdown syntax for images when you need to:
<picture>
or srcset for different image sources based on screen size or resolution.loading="lazy"
, class
, id
).Using HTML in Markdown also allows you to use inline CSS for styling images. You can use the style
attribute to apply CSS properties directly to an HTML element. For example:
There are a few other inline styles that you can use for images:
Sizing:
Alignment:
Centering (requires a wrapper element): To center an image, you need to wrap it in a block-level HTML element like a <div>
and apply text alignment or margin auto:
The basic syntax to insert an image is:
The image_url
can be a path/URL to the image.
To add a local image in Markdown, replace image_url
in the basic syntax with the local path to the image. For example:
You can also use relative path:
Here, the image is in the same folder as your Markdown file.
To resize an image, use the HTML <img>
tag:
To center an image, we can use the align
attribute:
To use images in GitHub, you can:
Create a folder in the GitHub repository, upload all the images to that folder, and use the relative path in the basic syntax.
Alternatively, you can upload the images to your preferred storage bucket, and add the URL to the image.
Markdown’s elegant syntax provides a straightforward and efficient way to embed images in your documents, offering a balance of simplicity and functionality. While it handles most image needs, Markdown alone isn’t enough. The ability to incorporate raw HTML directly, and thereby using inline CSS, within your Markdown allows for greater control when advanced styling, specific dimensions, or responsive image techniques are required.
For a more comprehensive review of the HTML5 markup language, explore our series, How To Build a Website in HTML. You can explore more Markdown-related topics with the following tutorials:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
If, by any chance, anyone wants to embed (a.k.a. in the same file) images inside a markdown file, they can do it by converting the image to base64 URI (this can help : https://base64.guru/converter/encode/image) and paste the result inside the link section of the image.
You can check this : https://drive.google.com/file/d/1TB5cRgTHpNN5PtyrpV50yfAGQQym9O89/view?usp=sharing
as reference.