Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a image in HTML

Understanding the Basics

When learning programming, it's crucial to grasp the concepts behind each line of code you write. Think of it as learning a new language, where each word or phrase has a unique meaning and purpose.

In the case of adding an image to a webpage using HTML (HyperText Markup Language), we're essentially telling the 'browser' (the program you use to navigate the web, like Chrome or Firefox) to fetch an image from a certain location and display it on the page. This is similar to how you might tell a friend to fetch a book from a shelf and place it on a table.

The Image Tag in HTML

HTML uses 'tags' - pieces of code that denote different elements on a webpage. To add an image, we use the 'img' tag. This tag doesn't have a closing tag, unlike many others in HTML. It's like a one-way street sign; it only needs to be placed once to convey its purpose.

Here is the basic syntax of an 'img' tag:

<img src="URL" alt="Alternate Text">

The 'src' Attribute

The 'src' (short for source) attribute in the 'img' tag is like the address you give to your friend to fetch the book. It tells the browser where the image file is located. This could be a location on your own website (a relative URL) or on another website (an absolute URL).

Here is an example with a relative URL:

<img src="/images/photo.jpg" alt="A beautiful landscape">

And here is an example with an absolute URL:

<img src="https://example.com/images/photo.jpg" alt="A beautiful landscape">

The 'alt' Attribute

The 'alt' attribute is like a backup plan. If for some reason the browser cannot display the image (maybe the location is wrong, or the image file is missing), it will display the text you've put in the 'alt' attribute instead. This text also helps screen-reading tools describe images to visually impaired readers.

Here is how you might include an 'alt' attribute:

<img src="/images/photo.jpg" alt="A beautiful landscape">

Adjusting Image Size

Sometimes, the image might be too big or too small for your webpage. In these cases, you can use the 'width' and 'height' attributes to adjust the size of the image. These values are usually specified in pixels.

Here is an example:

<img src="/images/photo.jpg" alt="A beautiful landscape" width="500" height="600">

This is like telling your friend to resize the book to a specific size before placing it on the table.

The 'title' Attribute

The 'title' attribute is used to provide additional information about the image. This text will display when a user hovers their mouse over the image.

Here's an example:

<img src="/images/photo.jpg" alt="A beautiful landscape" title="Taken in Yosemite National Park">

Wrapping Up

Adding an image to a webpage with HTML is as simple as using the 'img' tag with the necessary attributes. Remember, the 'src' attribute is like the address to the image, the 'alt' attribute is a backup plan and helps with accessibility, and the 'width' and 'height' attributes allow you to adjust the size of the image.

Just like learning any new language, practice is key. So, try adding different images to a webpage and experiment with different attribute values. Happy coding!