Markdown

Images in Markdown: A Comprehensive Guide to Embedding, Resizing, Centering, and Displaying the Images with Style and Ease

In today’s digital age, Markdown has emerged as a versatile and user-friendly markup language for creating rich, formatted documents. While it is widely used for creating text-based content, Markdown is also a powerful tool to easily incorporate the images into our documents.

The key to working effectively with images in Markdown lies in mastering the various features and options available
to ensure that our images are displayed exactly as we intend.

This comprehensive guide walks through the details of handling the images in Markdown, from embedding and resizing to centering and adding captions, all with style and ease.

Embedding the Images in Markdown

Before diving into the advanced techniques, let’s start with the basics of adding images to our Markdown document.

To insert an image in Markdown, we use the following syntax:

![Alt Text](Image URL)

In this syntax, the “![Alt Text]” is the alt text for the image. This means that if the picture is unable to load, a brief description of the image will be displayed instead.

The “Image URL” is the web address where the image is located. We can use a direct link to an image file on the internet or the relative path if the image is in the same directory as our Markdown file or in a subdirectory.

For example, if we want to add an image with the “This is an Example Image” alt text and the image file is named “markdown.jpg” in the same directory as our Markdown file, the syntax would be:

![This is an Example Image](markdown.jpg)

This code displays the specified image in our document.

Resizing the Images in Markdown

Markdown itself doesn’t provide a built-in way to resize or scale the images directly, but we can achieve an image resizing using HTML within our Markdown document.

Here are two common methods to resize the images:

1.Use the HTML <img> Tag

We can display the images and control their size in Markdown using the HTML tags.

Here’s the syntax to do this:

Alt Text

Here, the “src” attribute is where we specify the image’s source which can be a URL or a file path. The “alt” attribute is crucial for accessibility, providing an alternative text that describes the image’s content. By setting the “width” and “height” attributes, we can determine the desired dimensions of the image, adjusting them to meet our specific requirements.

Let’s create an example.

Example Image

In this example, we first speciy the image’s path which is “markdown.jpg”. Then, we provide the “Example Image” as the alternative text. After that, the “width” attribute is set to width=”200″ and the “height” attribute is set to “300”. The image is thus shown with a width and height of 200 and 300 pixels, respectively.

When we render this script, the image looks like this:

Specifying a fixed width and height might distort the image if the aspect ratio is not maintained. To maintain the aspect ratio while resizing, we can specify only one of either width or height and leave the other attribute blank.

For example:

Example Image

This code automatically modifies the height to preserve the aspect ratio while setting the image’s width to 300 pixels.

2.Centering the Images in Markdown
Centering the images in Markdown can greatly enhance the visual appeal of our documents or web content. While Markdown itself doesn’t provide a direct way to center the images, we can achieve this effect by incorporating a bit of HTML and CSS.

Here’s how we can do it:

<div style="text-align:center">
    <img src="markdown.jpg" alt="Example Image" width="200" height="150">
</div>

Let’s break down this HTML script:

The script begins with a <div> element; it acts as a container for the image which allows for applying specific styles to the grouped content. Inside the <div> element, there’s a style attribute. This attribute is used for inline CSS styling which allows for the direct application of styles to the element. In this case, the “text-align:center” style is applied. This CSS rule centers the text and any other inline or inline-block elements inside the <<div> horizontally. As a result, any content inside this <div> will be centered along the horizontal axis.

Within the styled <<div>, there's an <img> element. This element is used explicitly to embed the images in a web page.

Then, the script specifies the image file's source, either a URL or a local file path, which is set to "markdown.jpg" which indicates that the image file named "markdown.jpg" is the source. The image's alternate text is presented via the “alt” attribute. Lastly, the width and height of the image are set.

Rendering this gets us an image that is 200 pixels wide and 150 pixels high in the center of the page.

Adding Captions to Images

Adding captions to images in Markdown can enhance the clarity and accessibility of our content. Markdown, being a lightweight markup language, does not offer a native mechanism for image captions. However, there are a few approaches that we can take to accomplish this.

1.Using HTML and CSS

One of the most common ways to add captions to images in Markdown is by incorporating HTML and CSS. We can enclose our image and its caption within a pair of HTML tags: <figure> and <figcaption>. The <figure> tag serves as a container for our image, while the <figcaption> tag contains the caption text.
Let’s create an example here:

<figure>
    <img src="markdown.jpg" alt="Example Image" width="200" height="150">
    <figcaption>Caption: We are learning to add captions to images in Markdown </figcaption>
</figure>

The script begins by using the <figure> tag, an HTML element that is used to a group content, typically an image and its caption which allows us to link the two together for styling. In this case, it encapsulates the image and its caption.

Then, we use the <img> tag. This HTML element is employed to demonstrate the pictures on a web page. This code is set up with several attributes like “src”, “alt”, “width”, and “height”.

We specify the source of the image using the “src” attribute as “markdown.jpg”. Then, the alternative text for the image is provided by utilizing “alt”. Here, it describes the image as an “Example Image”. After that, we determine the width and height of the image in pixels.

The <figcaption> is an HTML element that is used to specify a caption for the content within the <figure>. It is placed immediately after the image and contains the caption text. Afterward, we specify the “Caption: We are learning to add captions to images in Markdown” text. This text acts as the image’s caption. In this example, the caption states that the image is “We are learning to add captions to images in Markdown.”

Here is the output:

When we use this HTML code, it creates a structured element that groups an image and its corresponding caption. It’s a helpful practice to enhance both the visual presentation and accessibility of images in web documents.

2.Using Markdown Extensions

Adding captions to images using Markdown extensions can be more straightforward and is often supported on certain platforms or Markdown processors. These extensions introduce custom syntax to include captions alongside your images.

Here’s how you can do it:

![Example image](markdown.jpg)

Caption: Markdown is a simple language.

Here, the first line of the script uses a standard Markdown syntax to embed the images into our documents. Whereas, in the second line of the script, we specify a text as “Caption: Markdown is a simple language” which serves as the caption for the image. However, it’s important to note that the “Caption” line is not part of the standard Markdown syntax. Instead, we are using a custom or platform-specific syntax to add captions. The caption describes the image which can be useful for providing context or additional information about the image.

When rendered, the result of this code displays the image with the “Example image” alternative text. Additionally, the “Caption: Markdown is a simple language” text appears as a caption below or alongside the image which provides more information about the image content.

When using this method, ensure that the Markdown environment or platform supports this custom caption syntax as it may not be universally recognized across all Markdown processors.

Creating Image Links in Markdown

In Markdown, we can create image links, also known as clickable images, to direct the users to other web pages or resources when they click on the image. To create an image link, we can combine the standard Markdown image syntax and the hyperlink syntax.

Here’s how we can create clickable image links:

[![Alt Text](URL)](Link_URL)

This syntax represents the standard Markdown image syntax where “Alt Text” provides a descriptive alternative text for the image, and “URL” specifies the image’s source URL or file path. The outermost brackets and parentheses form the hyperlink syntax in Markdown. Within the parentheses, “Link_URL” represents the destination URL to which the image will link when clicked.

This syntax allows for the seamless integration of images with hyperlinks which enables an interactive and visually appealing content in Markdown documents.

Let’s implement this into a practical example:

[![Click here to Visit Google](google.png)](https://www.google.com)

In this example, in the inner set of square brackets, “Click here to Visit Google” represents the alternative text for the image. Inside the parentheses, “google.png” specifies the image’s file path that we want to be displayed. The outer set of square brackets and parentheses that contains “https://www.google.com” represents the hyperlink destination.

When we render this Markdown code, the image with “alt” text is displayed.

When a user clicks on the displayed image with the “Click here to Visit Google” alternative text, they are directed to the specified URL which, in this case, is Google’s homepage.

This is a practical way to make the images clickable and link them to other web resources.

Styling the Images in Markdown

In Markdown, our material’s visual appeal and overall effect can be improved by designing the photos. While Markdown doesn’t provide extensive styling options for images, we can still apply the basic CSS styles using HTML within our Markdown document.

1.Adding a Border to Images

Adding a border to an image is a simple yet effective way to make it stand out on the page. We can use the style attribute within the HTML <img> tag to define the border properties.

Example Image

In summary, this provided HTML script inserts an image from the “markdown.jpg” source file with the “Example Image” alternative text. It styles the image by adding a solid yellow border around it which is 4 pixels wide and is displayed at a width of 300 pixels.

We can customize the border property by changing the width, style (dotted, dashed, etc.), and color to match our design preferences.

2.Adding a Drop Shadow

To apply a drop shadow to an image in Markdown, we can use the box-shadow CSS property within the style attribute of the HTML <img> tag.

Example Image

Example Image

You can adjust the values in the box-shadow property to control the shadow’s size, position, and blur to achieve the desired visual effect.

3.Styling with Rounded Corners

To apply the rounded corners to an image in Markdown, you can use the border-radius CSS property within the style attribute of the HTML tag.

Example Image

In this code, the border-radius is used to create the rounded corners. The radius of the rounded corners is set to 30px. This number can be changed to make the corners more or less rounded.

Conclusion

Markdown provides a simple yet powerful way to include, style, and manipulate the images in your web content. This article taught us how to embed, resize, center, and style the images easily, making our content more visually appealing and informative. By understanding the various techniques and possibilities that are outlined in this comprehensive guide, you can effectively utilize Markdown’s capabilities to enhance your content with images.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.