Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to insert an image in HTML

Understanding Images in HTML

Incorporating visuals into a webpage can significantly enhance its appeal, making it more engaging. HTML or HyperText Markup Language, the standard language in creating web pages, allows you to do just that. In this post, we will explore how to insert an image in HTML.

The Core Element: img

In HTML, the img element is the key to embedding an image on a web page. Think of it as the door that lets an image enter your webpage. Without this door, the image cannot be displayed. The img element is written as <img /> and it's an empty element, meaning it contains no text content or closing tag.

Here's how you can use it:

<img src="image.jpg" />

The src attribute is vital here. It's like the address that points to the location of the image. In this example, we're telling the browser to fetch the image named "image.jpg" located in the same directory.

Specifying the Image Source: src

The src attribute, short for source, is used to specify the URL of the image you want to insert. The URL could be an absolute path pointing to an image hosted somewhere on the internet or a relative path pointing to an image within your website's directory.

Here's an example of a relative path:

<img src="images/my-image.jpg" />

In this case, the image "my-image.jpg" is located inside an "images" folder in the same directory.

And here's an example of an absolute path:

<img src="https://www.example.com/images/my-image.jpg" />

In this case, the image is located on another server and we're using its full URL to display it.

Alt Text: Making Images Accessible: alt

The alt attribute provides alternative text for an image if it cannot be displayed. This could be due to a broken link, a slow internet connection, or for visually impaired users using screen readers.

Here's how you can include alt text:

<img src="images/my-image.jpg" alt="A description of the image" />

Controlling Image Size: width and height

HTML allows you to control the dimensions of your image using the width and height attributes. These values are specified in pixels.

<img src="images/my-image.jpg" alt="A description of the image" width="500" height="600" />

In the above example, the image's width is set to 500 pixels, and its height is 600 pixels.

Captioning Images: figure and figcaption

Sometimes, you might want to include a caption with your image. HTML provides the figure and figcaption elements for this purpose.

<figure>
    <img src="images/my-image.jpg" alt="A description of the image" />
    <figcaption>This is a caption for the image.</figcaption>
</figure>

In this example, figure is a container that groups the image and its caption together, and figcaption holds the actual caption text.

Conclusion

Incorporating images into your HTML pages can significantly enhance their appeal and accessibility. The img element, coupled with attributes like src, alt, width, height, and elements like figure and figcaption, offers a robust toolkit for managing images on your web pages. Remember, the key to making your pages visually appealing is not just about adding images, but also about ensuring they are sized, captioned, and described correctly. Happy coding!