Basics of the <img> Tag:

The <img> tag is used to embed images into an HTML document. Its basic syntax is as follows:

<img src="image-url.jpg" alt="Image Description">
  • src: The src attribute specifies the URL (source) of the image file.
  • alt: The alt attribute provides alternative text for the image, which is displayed if the image fails to load or for accessibility purposes (screen readers).

Example of Using the <img> Tag:

Let's say we have an image file named "example.jpg" in the same directory as our HTML file. We can use the <img> tag to display this image as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Image Example</title>
</head>
<body>
  <h1>My Image Example</h1>
  <img src="example.jpg" alt="Example Image">
</body>
</html>

In this example:
  • The <img> tag is used with the src attribute set to "example.jpg" to display the image.
  • The alt attribute provides descriptive text for the image, which is useful for accessibility.
  • Image Attributes and Best Practices:
  • Width and Height: You can specify the width and height of the image using the width and height attributes within the <img> tag. This helps browsers allocate space for the image before it loads, improving page loading performance.
<img src="example.jpg" alt="Example Image" width="200" height="150">

Responsive Images: For responsive web design, use CSS or the width attribute with a percentage value to make images scale based on screen size.

<img src="example.jpg" alt="Example Image" style="width: 100%;">
 
  • Image Accessibility: Always include descriptive and meaningful alt text for images. This helps users with visual impairments understand the content of the image.
  • Image Formats: Use appropriate image formats such as JPEG (.jpg), PNG (.png), or SVG (.svg) based on the type of image and its requirements (e.g., photographs, logos, icons).

Handling Image Paths:
  • Relative Paths: If the image is in the same directory as the HTML file, you can use a relative path in the src attribute (src="image.jpg").
  • Absolute Paths: For images hosted on external servers or different directories, use absolute paths (src="https://example.com/image.jpg").

Example with Responsive Image:

Let's modify our previous example to include a responsive image:
 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Image Example</title>
  <style>
    img {
      width: 100%;
      height: auto;
    }
  </style>
</head>
<body>
  <h1>Responsive Image Example</h1>
  <img src="example.jpg" alt="Responsive Image">
</body>
</html>

In this example, the CSS style inside the <head> section makes the image responsive by setting its width to 100% of the container and allowing the height to adjust automatically.


Image Optimization:
  • File Size: Optimize images to reduce file size without compromising quality. Tools like Photoshop, GIMP, or online services can help optimize images.
  • Compression: Use image compression techniques to further reduce file size for faster loading times.



Practice Excercise Practice now