Git: How to Display Images in the Readme.md File?

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Understanding how to showcase your project effectively is an essential skill every developer should possess. A README.md file with well-presented images can make your GitHub repository stand out. This article is a step-by-step guide on how to include images in your README.md files to improve your project’s documentation on Git repositories.

Understanding Markdown

Markdown is a lightweight markup language used to format text. It’s popular in the development community because it’s straightforward to learn and geared towards creating web content quickly. README.md files on GitHub use the Markdown syntax for formatting. Before we dive into displaying images, let’s quickly revisit two fundamental aspects of Markdown:

  • Text Formatting: Including headers, bold, italics, lists, and more.
  • Links and Images: Embedding hyperlinks and images into your Markdown document.

Embedding Images

To embed an image into a README.md file, you use the following Markdown syntax:

![Alt text](URL_to_image "Optional title")

In this context:

  • Alt text is the alternative text that describes the image if the image fails to load.
  • URL_to_image is the web location of the image you want to display.
  • Optional title is shown as a tooltip when the cursor hovers over the image.

Linking to Images in a Repository

Your first option for displaying images in a README.md is to add them to your repository. Here’s how you link directly to an image file within your repository:

![Alt text](/path/to/image.jpg "Optional title")

Let’s see a practical example. Suppose you have an image called logo.jpg in a directory named images in the root of your repository:

![Logo](/images/logo.jpg "Company Logo")

This code will display the image with the alt text ‘Logo’ and the tooltip ‘Company Logo’ when hovered.

Hosting Images Externally

What if you prefer to host your images externally? No problem. GitHub can display images hosted on any server. Just replace the URL_to_image with the direct link to the image:

![Alt text](https://example.com/path/to/image.jpg "Optional title")

Remember to use HTTPS links to ensure your links are always secure.

Resizing Images

Markdown itself does not support resizing images. However, you’re not completely out of luck. If you’re hosting on GitHub, you can use HTML tags like so:

<img src="URL_to_image" width="150" height="100">

You can adjust the width and height to suit your needs. Here’s an actual example:

<img src="/images/logo.jpg" width="200">

This will display your image with a width of 200 pixels and its corresponding height to maintain the aspect ratio.

Embedding Images from GitHub Issues

You can also upload images to a comment in a GitHub issue or pull request and use the URL for embedding:

  1. Drag and drop the image into an issue or comment box.
  2. Wait for it to upload and GitHub will automatically generate the Markdown code for the image.
  3. Copy the generated code and paste it into your README.md.

Displaying a Series of Images

If you need to display multiple images side by side, HTML can come to the rescue once more.

<p align="center">
  <img src="image1.jpg" />
  <img src="image2.jpg" />
</p>

Wrap your <img> tags within a <p> or <div> with an align attribute. This way, your images will display in a row centered on the page.

Linking Images

Finally, if you want your images to be clickable links, wrap the image Markdown syntax in link syntax like so:

[![Alt text](URL_to_image "Optional title")](URL_to_navigate)

When someone clicks on the image, they’ll navigate to the URL specified in the URL_to_navigate placeholder.

Best Practices for Using Images in README.md

  • Compress images to reduce the load time.
  • Use descriptive alt text for SEO and accessibility.
  • Name the image files meaningfully, to help collaborators understand their use.
  • Maintain consistency in image styles and sizes if possible.

Conclusion

Images are a powerful tool for improving the comprehensibility and visual appeal of your GitHub README.md files. Whether you’re adding screenshots, logos, or diagrams, following the instructions within this guide will enable you to integrate images seamlessly into your project documentation.

Remember to adhere to best practices, and consider the balance between visual richness and readability of your README.md files. With these tips and techniques, you can significantly enhance your project’s presentation on GitHub and provide an excellent experience for other developers encountering your work.