Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to put an image in HTML

Understanding the Basics

If you're learning programming, then chances are you're familiar with HTML, the markup language used for creating web pages. One of the key aspects of creating an engaging and visually appealing website is the use of images. But how do you put an image in HTML? It's actually quite simple and I'm going to show you how.

The <img> Tag

In HTML, images are embedded using the <img> tag. Picture it as a signpost that tells the browser: "Hey, there's an image supposed to be here, go fetch it from this location". This tag is an empty tag which means it doesn't have a closing tag like </img>.

Here's a basic example of how it's used:

<img src="myimage.jpg">

In the example above, the src attribute (think of it like a property or characteristic of the image) is used to specify the location of the image file. In this case, the image file (myimage.jpg) is located in the same directory as the HTML file.

Absolute vs Relative Paths

When specifying the location of an image file, you can use either an absolute path or a relative path. Imagine you're giving someone directions to your house. An absolute path is like giving them the full address, including the city, state, and zip code. A relative path, on the other hand, is like telling them how to get to your house from a specific location.

For example, if the image file is located in an 'images' folder within the same directory as your HTML file, you would use a relative path as follows:

<img src="images/myimage.jpg">

An absolute path, on the other hand, would specify the full URL of the image:

<img src="http://www.example.com/images/myimage.jpg">

The Importance of the alt Attribute

The alt attribute provides an alternative text for an image if it cannot be displayed. Think of it as a backup description that's used in case the image can't be shown for some reason. For example, if the image file is missing or if the user is using a screen reader due to a visual impairment.

Here's how you would include it:

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

Resizing Images with HTML

While it's generally better to resize your images before you upload them to your HTML file, you can adjust the height and width of an image using the height and width attributes. Consider these attributes as the measurements indicating how much space on the webpage the image should occupy.

Here's an example:

<img src="myimage.jpg" alt="My Image" width="500" height="600">

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

The Importance of Image Formats

There are several image formats that you can use in HTML including JPEG, PNG, GIF, SVG, and more. Each format has its strengths and weaknesses. For instance, JPEG is great for photographs while PNG is better for images that require transparency. It's like choosing the right tool for the job - some are better suited for certain tasks than others.

Wrapping Up

Adding images to your HTML is not just about making your webpage look good. It's about improving the user experience, accessibility, and SEO of your webpage. So, remember to use the alt attribute for accessibility and SEO purposes, choose the correct image format, and resize your images appropriately.

Now, go ahead and start experimenting with adding images to your HTML pages. The more you practice, the more comfortable you will become with it. Happy coding!