Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to insert image in HTML from folder

What is an Image Tag in HTML?

In the world of programming, HTML (HyperText Markup Language) is used to structure content on the web. One of the simplest yet most powerful tools it provides is the ability to add images to a webpage. This is done using the <img> tag. Consider this tag as a placeholder where the actual image will appear on your webpage.

The Basics of HTML Image Tag

To insert an image in HTML from a folder, you need to understand two main attributes of an <img> tag:

  1. src (Source) - This attribute tells the browser where the image file is located.
  2. alt (Alternative) - This attribute provides alternative information for an image if a user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).

Here's what a basic <img> tag looks like:

<img src="path_to_your_image_file" alt="alternative_text">

Think of the src attribute as a map that guides the browser to the image file. The alt, on the other hand, is like a backup explanation that tells the user what they're missing if the image can't be displayed.

Inserting an Image from a Folder

To insert an image from a folder, you need to specify the path to the image file in the src attribute.

Let's say you have a folder named 'images' in the same directory as your HTML file, and inside this folder, you have a picture named 'my_pic.jpg'. To add this image to your webpage, the src attribute would look like this:

<img src="images/my_pic.jpg" alt="My Picture">

The browser will start looking for the image file in the same folder as the HTML file. When it finds the 'images' folder, it will look inside and load 'my_pic.jpg'.

Relative vs. Absolute Paths

There are two types of paths you can use in the src attribute: relative and absolute.

A relative path is a shorthand way of pointing to a file or folder. It starts from the location of the current page or style sheet. Using the previous example, "images/my_pic.jpg" is a relative path.

An absolute path, on the other hand, is the full URL of the image. If the image 'my_pic.jpg' is located at 'https://www.example.com/images/my_pic.jpg', then that is its absolute path.

<img src="https://www.example.com/images/my_pic.jpg" alt="My Picture">

Here, no matter where the webpage is, the browser will follow the exact path provided.

The Importance of the 'alt' Attribute

The 'alt' attribute is crucial not just for the web accessibility of visually impaired users, but also if the image fails to load. It's like a friend whispering in your ear, "Hey, there's supposed to be a photo here, but I can't find it. It's a picture of a cute puppy playing in the grass."

In conclusion, HTML provides a straightforward way to add images to your webpages. Understanding how to use the <img> tag, along with the src and alt attributes, will allow you to insert any image from any folder in your HTML files. As you continue your journey into coding, you'll find this skill is a fundamental building block to creating visually engaging websites. Happy coding!