How to add images to README.md on GitHub

Posted on

Adding images to a README.md file on GitHub can greatly enhance the clarity and appeal of your project documentation. This can be done by including images stored locally in your repository or images hosted online. For local images, you need to ensure the image files are added to your repository and referenced correctly in your Markdown file using the appropriate syntax. For online images, you can directly use the image URL in your Markdown file. Properly integrating images into your README.md helps convey information more effectively and can significantly improve the user experience.

Adding Local Images

Storing images in your repository: First, ensure that the images you want to add to your README.md file are included in your GitHub repository. Create an images folder in the root directory of your project and place your image files there. This helps in organizing your files and making the repository structure cleaner.

mkdir images
mv my_image.png images/

This command creates an images folder and moves my_image.png into it.

Referencing local images: To reference an image stored locally in your repository, use the following Markdown syntax:

![Alt text](images/my_image.png)

Here, Alt text is a description of the image, and images/my_image.png is the path to the image file relative to the README.md file. This ensures that when your repository is viewed on GitHub, the image is displayed correctly.

Example:

![Project Logo](images/logo.png)

This line of code will display the logo.png file stored in the images folder in your README.md.

Adding Online Images

Using image URLs: If the image is hosted online, you can directly use the image URL in your Markdown file. This is useful for images stored on image hosting services or other websites.

![Alt text](https://example.com/my_image.png)

Replace https://example.com/my_image.png with the actual URL of the image.

Advantages: Using online images can reduce the repository size and avoid the need to manage image files within the repository. However, ensure that the URLs are permanent and reliable to avoid broken image links.

Example:

![Hosted Image](https://example.com/hosted_image.png)

This line will display an image hosted at https://example.com/hosted_image.png in your README.md.

Formatting Images in Markdown

Resizing images: While Markdown does not natively support image resizing, you can use HTML to adjust the size of images within your Markdown file.

<img src="images/my_image.png" alt="Alt text" width="500" />

This HTML tag will include the image with a specified width of 500 pixels. Using HTML allows more control over the appearance of images in your README.md.

Aligning images: Similarly, to align images, you can use HTML:

<p align="center">
  <img src="images/my_image.png" alt="Alt text" width="500" />
</p>

This code will center-align the image in your README.md. HTML can be mixed with Markdown to achieve the desired layout and formatting.

Using GitHub Flavored Markdown Features

Image captions: While basic Markdown doesn’t support image captions, GitHub Flavored Markdown (GFM) allows the use of HTML and other enhancements to add captions.

<figure>
  <img src="images/my_image.png" alt="Alt text" width="500" />
  <figcaption>This is the image caption</figcaption>
</figure>

This HTML snippet includes a caption below the image, enhancing the descriptive power of your README.md.

Embedding diagrams and charts: Tools like Mermaid.js allow embedding diagrams and charts using Markdown-like syntax. This can be particularly useful for technical documentation.

graph TD;
    A--&gt;B;
    A--&gt;C;
    B--&gt;D;
    C--&gt;D;

This Mermaid code will render a flowchart in your README.md on GitHub, providing a visual representation of processes or data flows.

Best Practices for Adding Images

Use descriptive alt text: Always include meaningful alt text for your images. Alt text improves accessibility, helping users who rely on screen readers understand the content of the images. It also provides context if the image fails to load.

![Project architecture diagram](images/architecture.png)

This ensures that the purpose of the image is clear even if the image itself is not displayed.

Optimize image size: Large images can slow down the loading time of your README.md on GitHub. Use image optimization tools to reduce file sizes without compromising quality. This ensures that your documentation loads quickly and efficiently.

# Example command using ImageMagick to resize an image
convert images/large_image.png -resize 800x800 images/optimized_image.png

This command resizes the image to a maximum of 800 pixels in width or height, optimizing it for web display.

Organize image files: Keep your repository organized by storing images in a dedicated directory, such as images or assets. This practice helps maintain a clean project structure and makes it easier to manage media files.

mkdir assets
mv my_image.png assets/

This keeps your project directories tidy and organized.

Summary

Adding images to a README.md file on GitHub enhances the documentation by providing visual context and improving user engagement. By properly storing images in your repository, using appropriate Markdown syntax, and leveraging HTML for advanced formatting, you can effectively integrate images into your project documentation. Whether using local images or online URLs, it’s important to follow best practices such as using descriptive alt text, optimizing image sizes, and organizing files systematically. These steps ensure that your README.md is informative, accessible, and visually appealing, ultimately contributing to a better understanding and usability of your project.

👎 Dislike