Adding Images to a Web Page

In this section of the course, we will explore the fundamentals of adding images to a web page. Images play a vital role in enhancing the visual appeal and conveying information effectively. Whether you're building a personal blog, an e-commerce website, or a news portal, knowing how to insert images into your HTML documents is an essential skill. We will cover the necessary HTML tags and attributes, as well as best practices for optimizing images for fast loading times and ensuring a seamless user experience.

Inserting Images using the HTML <img> Tag

The HTML <img> tag is used to insert images into a web page. It is a self-closing tag, which means it does not require a closing tag. The <img> tag requires at least two attributes: src and alt. The src attribute specifies the source or URL of the image file, while the alt attribute provides alternative text for the image, which is displayed when the image cannot be loaded or for users with visual impairments. Let's look at an example:

<img src="image.jpg" alt="A beautiful landscape">

In the above example, the src attribute specifies the image file name as "image.jpg," and the alt attribute provides a short description of the image as "A beautiful landscape." It is essential to include descriptive alt text for accessibility purposes, as it ensures that visually impaired users can understand the content of the image.

Specifying Image Sources

When inserting images, you need to specify the source or location of the image file. The src attribute of the <img> tag is used for this purpose. You can provide a relative or absolute URL as the image source. Let's take a look at some examples:

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

In this example, the image file "image.jpg" is located within a folder named "images" in the same directory as the HTML file. The relative URL "images/image.jpg" specifies the path to the image file relative to the current HTML file.

Absolute URL:

<img src="" alt="A beautiful landscape">

In this example, the image file "image.jpg" is located on a different website. The absolute URL "" provides the complete path to the image file, starting with the protocol (https://) and followed by the domain and the file path.

Adjusting Image Dimensions

To control the display size of an image, you can use the width and height attributes of the <img> tag. These attributes specify the width and height of the image in pixels. It is recommended to specify either the width or the height attribute while maintaining the aspect ratio of the original image to avoid distortion. Let's see an example:

<img src="image.jpg" alt="A beautiful landscape" width="500" height="300">

In the above example, the width attribute is set to 500 pixels, and the height attribute is set to 300 pixels. The image will be displayed with these dimensions on the web page. It is important to note that specifying image dimensions can help improve the loading time of the page since the browser can reserve the required space for the image in advance.

Aligning Images within the Page Layout

You can use the align attribute to control the alignment of an image within the surrounding text or container. The align attribute has several values, including left, right, center and top. Let's see how it works:

<img src="image.jpg" alt="A beautiful landscape" align="left">

In this example, the align attribute is set to left, which aligns the image to the left side of the text or container. Similarly, you can use right to align the image to the right side and center to align it in the center. The top value aligns the image to the top of the container. However, the align attribute is deprecated in HTML5, and it is recommended to use CSS for precise alignment.

Best Practices for Image Optimization

Optimizing images is crucial for ensuring fast loading times and an optimal user experience. Large image files can significantly slow down a web page, especially on mobile devices with limited bandwidth. Here are some best practices for image optimization:

By following these best practices, you can optimize your images for fast loading times and ensure a seamless user experience on your web pages.


In this section, we explored the fundamentals of adding images to a web page using the HTML <img> tag and various attributes. We covered topics such as specifying image sources, adjusting image dimensions, and aligning images within the page layout. By mastering these techniques, you have learned how to enhance the visual appeal and effectively convey information through images in your HTML projects.

In the next section, "Image Formats and Optimization," we will delve deeper into the world of image optimization. While adding images to a web page is essential, it is equally important to optimize them for fast loading times and optimal performance. We will explore different image formats such as JPEG, PNG, and GIF, understanding their strengths and limitations and how to choose the most appropriate format for different types of images.

By mastering image formats and optimization, you will be equipped with the knowledge and skills to make informed decisions about image selection, compression, and delivery. These techniques will not only enhance the performance of your web pages but also improve the user experience by ensuring fast loading times and visually appealing images.

So, join us in the next section as we dive into the exciting world of "Image Formats and Optimization" and discover how to optimize your images for the web.