Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to code an image in HTML

Understanding Images in HTML

HTML, or HyperText Markup Language, is the standard language for creating web pages. One of the most common elements we come across in a webpage is an image. But how does an image appear on your webpage? Well, that's exactly what we'll be exploring in this blog.

What is an Image Tag?

An image tag is a special instruction in HTML that tells the web browser to fetch and display an image at a particular location on a web page. Think of it as a sticky note on a blueprint saying "put a window here". The 'window' in this case is our image.

How to Insert an Image

To insert an image, we use the <img> tag. Here's an example:

<img src="image.jpg">

In the above code, src (which stands for source) is an attribute that tells the browser where to find the image file. "image.jpg" is the name of the image file that we want to display.

Imagine the src attribute as a map that guides the browser to the image file. If the path is incorrect or if the image does not exist, no image will be displayed.

Relative vs. Absolute Paths

When specifying the src attribute, we can use either a relative path or an absolute path:

A relative path points to the location of the image file relative to the current directory. For instance, if your HTML file and image file are in the same directory, you can simply use the image file name as the src attribute, just like in our previous example.

An absolute path points to the exact location of the image file, no matter where the current directory is. This is often a URL. For example:

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

Think of relative paths like saying "the grocery store is next to my house", whereas absolute paths are like saying "the grocery store is at 123 Main St."

Adding Alternative Text

There's another attribute that is good practice to include: the alt attribute. This provides alternative text that describes the image, which is used when the image cannot be displayed and by screen readers for visually impaired users.

Here's how to use it:

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

Controlling Image Dimensions

We can also control the width and height of an image using the width and height attributes. These are specified in pixels. Here's an example:

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

In the above example, the image will be displayed with a width of 500 pixels and a height of 600 pixels. It's like specifying the size of the window we're installing in our blueprint analogy.

Keep in mind that specifying both width and height may distort the image if the proportions do not match the original image's aspect ratio (the ratio of width to height). To keep the same aspect ratio, you can specify only one dimension (either width or height), and the browser will automatically adjust the other dimension.

Recap

In HTML, we use the <img> tag to insert an image onto a webpage. This tag uses the src attribute to locate the image file, and can optionally use the alt, width, and height attributes to provide alternative text and control the image's dimensions.

Understanding how to work with images is a fundamental part of learning HTML. With this knowledge, you can start adding visual elements to your web pages and making your content more engaging for your audience.