HTML Images

Table of Content

Introduction to HTML Images

Role and Importance of Images in Web Development:

Imagine a website without any pictures! It would be dull and unappealing. Images play a vital role in enhancing the user experience and attracting visitors. They help:

  • Visualize Information: Images can convey information quickly and efficiently, making it easier for users to understand complex concepts.
  • Create Impact: High-quality images can draw attention, engage users, and leave a lasting impression.
  • Enhance Design: Images can break up text, add color and texture, and create visual hierarchy to guide users through the website.

Types of Images and Supported Formats:

There are two main types of images used on websites:

  • Raster Images: These are made up of a grid of pixels. They are best suited for detailed and colorful images, such as photographs. Supported formats include JPG, PNG, and GIF.
  • Vector Images: These are made up of mathematical formulas that describe shapes and lines. They are scalable without losing quality, making them ideal for logos, icons, and illustrations. Supported formats include SVG and EPS.

Choosing the Right Image Format:

The choice of image format depends on the purpose of the image:

  • JPG (JPEG): Best for photographs and other detailed images due to its high compression. However, it can result in some loss of quality.
  • PNG: Supports transparency and is suitable for images with large areas of flat colors, such as logos or graphics.
  • GIF: Supports animation and is often used for simple animations and small icons.
  • SVG: Perfect for scalable images that can be enlarged or reduced without losing quality. It is commonly used for logos and vector-based graphics.

Basic Concepts

In HTML, the <img> tag is used to display images on a web page. It has several attributes that control the appearance and behavior of the image.


<img src="image_url" alt="alternative_text" width="image_width" height="image_height">


src Attribute: The Image Source

The src attribute is like the address of the image you want to display. It tells the web browser where to find the image file on the internet.


<img src="">

This code will display an image from the website "," specifically from the file named "image.jpg."

alt Attribute: Alternative Text

The alt attribute is like a backup plan. If, for some reason, the image cannot be displayed (maybe the internet is slow or the file is missing), the alt text will be shown instead. This helps users with disabilities, such as people with impaired vision, to understand the content of the image.


<img src="" alt="A smiling cat">

If the image cannot be displayed, the web browser will show the text "A smiling cat" in its place, so that the user knows what the image is about.

Image Manipulation

Resizing and Scaling Images

You can control the size of an image using the width and height attributes.


This will display the image "image.jpg" with a width of 200 pixels and a height of 150 pixels.

<img src="image.jpg" width="200" height="150">

Cropping Images

You can remove unwanted portions of an image using the crop attribute. The crop attribute takes a value of either "none", "crop", or "pad".

  • none: No cropping is applied.
  • crop: The image is cropped to fit the available space.
  • pad: The image is padded with white space to fill the available space.


This will display the image "image.jpg" with a width of 200 pixels and a height of 150 pixels, and the image will be cropped to fit the available space.

<img src="image.jpg" width="200" height="150" crop="crop">

Types of Image Elements

<img> Element

  • Used to display images on a web page
  • Attributes:
    • src - Path to the image file
    • alt - Alternative text for accessibility (displayed if image doesn't load)
    • width and height - Dimensions of the image

<figure> and <figcaption> Elements

  • Group an image with its caption
  • <figure> element contains the image and its associated text
  • <figcaption> element provides the caption

<picture> and <source> Elements

  • Used to handle responsive images for different screen sizes
  • <picture> element contains multiple <source> elements with different image sources
  • Each <source> element has a media attribute that specifies the screen size it applies to
  • Browser chooses the best image source for the current screen size

Example Code:

<!-- Image with alternative text -->
<img src="image.jpg" alt="A beautiful sunset">

<!-- Image grouped with caption -->
  <img src="image.jpg" alt="A beautiful sunset">
  <figcaption>Caption: A breathtaking view of the setting sun.</figcaption>

<!-- Responsive image -->
  <source media="(max-width: 480px)" srcset="image-small.jpg">
  <source media="(min-width: 480px) and (max-width: 1024px)" srcset="image-medium.jpg">
  <img src="image-large.jpg" alt="A beautiful sunset">

Optimizing Images

Image Compression Techniques

Imagine a big picture as a box filled with colored blocks. To make it smaller, we can either:

  • Reduce the number of blocks: This is like removing some details from the picture. It reduces file size but can make the image look blurry.
  • Make the blocks smaller: This keeps all the details but makes the colors less bright.
  • Use a more efficient way to store the blocks: This is like packing them more tightly without changing their size or color.

Optimizing Images for Fast Loading

When you open a website, it has to download all the images. To make it load faster, we can:

  • Use a smaller file size: By using compression techniques, we can reduce the amount of data that needs to be downloaded.
  • Set the right image size: Make sure the images are the exact size they need to be on the website, so we don't waste time downloading extra pixels.

Using Image Placeholders

Sometimes, images take a while to load. To make the website look better while we wait, we can:

  • Use a placeholder image: This is a simple image that appears while the real image is loading.
  • Add a "loading" animation: This can be a spinning circle or a progress bar that shows how much of the image has loaded.

By using these techniques, we can make our websites faster and more enjoyable for users.

Best Practices

Image Optimization Best Practices:

  • Choose the right file format: Use JPEG for photos and PNG for graphics with transparent backgrounds.
  • Compress images: Reduce file size without losing quality using tools like TinyPNG.
  • Use the correct image dimensions: Use the exact size needed to display the image, avoiding scaling down large images.
  • Use CSS instead of HTML: Use CSS styles to adjust the size and appearance of images, rather than setting width and height attributes in HTML.

Handling Responsive Images:

  • Use the <picture> element with multiple <source> elements to provide different images for different screen sizes.
  • Use CSS media queries to apply different styles to images based on the viewport width.
  • Consider using a responsive image library like Cloudinary that automatically adjusts images for different devices.

Tips for Creating Accessible Images:

  • Add an <alt> attribute to describe the image for users who cannot see it (e.g., screen readers).
  • Use descriptive file names to help identify the image.
  • Provide transcripts or captions for any audio or video content in the image.
  • Explain the different types of image elements and their uses.
  • Describe the steps involved in optimizing images for the web.
  • How do you handle responsive images in HTML?
  • What are the best practices for creating accessible images?

Common Mistakes to Avoid

Imagine trying to look at a picture in a book, but the picture is missing. That's what a broken image link is like on a website. It's an image that isn't there anymore, so you can't see it.

2. Ignoring Alt Text:

Alt text is like a description of an image for people who can't see it. For example, if there's a picture of a cat, the alt text might say "A cute cat is sleeping on a pillow." This helps people who are blind or have low vision understand what the image is about.

3. Overusing Images:

It's great to use images to make your website more interesting, but don't use too many. Too many images can make your website slow to load and hard to read.

4. Not Optimizing Images for Performance:

When you save an image, you can choose different sizes and file formats to make it smaller. This makes your website load faster.

Advanced Techniques

Image Transformations with CSS

Imagine your favorite photo, but you want to make it taller, rounder, or flip it upside down. CSS, or Cascading Style Sheets, lets you do just that without editing the image itself. You can:

  • Scale and Size: Change the image's height and width.
  • Rotate: Turn the image clockwise or counterclockwise.
  • Flip: Mirror the image horizontally or vertically.
  • Skew: Tilt the image to create a slanted effect.

It's like having a magical wand for images!

Creating Image Galleries and Sliders

If you have a collection of images, you can showcase them in a gallery. Or, if you want to feature a few key images, you can create a slider that automatically scrolls through them.

  • Galleries: Display multiple images in a grid or list format.
  • Sliders: Rotate images at a set interval, like a movie slide show.

Working with Vector Graphics (SVGs)

SVGs (Scalable Vector Graphics) are special images that use mathematical equations instead of pixels. This means:

  • Scalable: You can resize them to any size without losing quality.
  • Lightweight: They take up less space than photos.
  • Flexible: You can easily change their color, stroke thickness, and other properties.

SVGs are great for logos, icons, and illustrations that need to look sharp and crisp on websites and devices.