Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add an image in HTML

A Peek into the World of Web Development

Before we delve into the intricacies of adding an image to your HTML page, it's important to understand the role of images in web development. Pictures are worth a thousand words, they say, and in web development, this couldn't be truer. Images add color, life, and context to your webpage, making it aesthetically pleasing and informative. It's like adding a cherry on top of your sundae, it completes the overall look and feel.

The Anatomy of an HTML Image Tag

In HTML, images are embedded using an img tag. Now, don't panic if this sounds like a tech jargon. Imagine img as a special command given to the computer to display an image in a specific location on your webpage.

The img tag uses two main attributes: src and alt. You can think of these attributes like the instructions on a treasure map. The src attribute (source) points to the location of the image file, and the alt attribute provides a text alternative for the image. If the image can't be displayed for some reason, the alt text will be shown instead.

Here's an example:

<img src="myImage.jpg" alt="A description of the image">

Adding an Image to Your HTML Page

Adding an image to your web page is like adding a new piece of furniture to your room. You need to know where to place it and how it should look.

Let's break down the steps:

Locate Your Image: Before you can add an image, you need to have one! This might be an image you've created, or one you've legally obtained from the internet. Make sure the image is in the same folder as your HTML file to keep things simple.

Use the HTML img tag: Now that you have your image, you can instruct your webpage to display it using the img tag. Remember, this is like giving a command to your computer.

Here's an example:

<img src="myImage.jpg" alt="A beautiful sunset scene">
  1. Preview Your Page: After adding the image, save your HTML file and open it in a web browser to see the changes. You should see your picture displayed on the page. If not, check your img tag for any typos or errors.

Understanding File Paths

Sometimes, your images might not be in the same directory as your HTML file. In such cases, you need to specify the correct file path in the src attribute. A file path is like an address for your file. It tells the computer where to find the file in your system.

Here's an example:

<img src="images/myImage.jpg" alt="A beautiful sunset scene">

In this example, the image is located in a subfolder called "images".

The Role of Alt Text

The alt attribute in the img tag plays a crucial role in making your webpage accessible to all users. It provides a text description of the image which can be read by screen readers (software that helps visually impaired users navigate the internet). Think of alt text as a helpful guide, narrating the visual elements of your webpage to those who can't see them.

Here's an example:

<img src="myImage.jpg" alt="A colorful parrot perched on a tree branch">

In this example, if the image fails to load or if the user is visually impaired, they will hear or see the alt text describing the image.

Wrapping Up

Adding images to your HTML page might seem like a daunting task at first, but with a little practice, it becomes second nature. Remember, the key is to understand the role of the img tag and its attributes. And most importantly, always ensure your webpage is accessible to all by providing meaningful alt text for your images.

Happy coding!