HTML Images

In previous lessons, youโ€™ve learned about HTML Links and Navigation. Now we will learn about how to add images to your web page.

๐Ÿ–ผ๏ธ The Image Element

The <img> tag is used to insert images in your webpage. Unlike other HTML elements, img tag is self-closing we do not need to add a closing tag like </img>.

๐Ÿ—๏ธ img tag Syntax

<img src="path/to/image.jpg" alt="Description of the image">
  • src = Source - the path to your image file
  • alt = Alternative text - text that display if the image cannot be loaded

โš ๏ธ Always Include Alt Text

The alt attribute is important for user with disabilities, as it provides a text instead of image to describe what the image is about. It also helps with search engines to learn about your image content since they cannot โ€œseeโ€ images.

๐ŸŒŸ Your First Image

<img src="FreecodingSchool.jpg" alt="Freecoding School is a free coding school for everyone">

this will display the image FreecodingSchool.jpg with the alt text โ€œFreecoding School is a free coding school for everyoneโ€ as below:

Freecoding School is a free coding school for everyone

When images are not available, the alt text will be displayed instead. For example, if image FreecodingSchool.jpg is not found, the text โ€œFreecoding School is a free coding school for everyoneโ€ will be shown as below:

Freecoding School is a free coding school for everyone

๐Ÿ“ Image File Paths

Providing the correct path to your image file is very important. There are two main types of paths you can use:

  1. Relative Paths - its path from the current file location
  2. Absolute URLs - its full URL from the root of the website or an external website

๐Ÿ  Relative Paths (Images in Your Project)

Relative paths are used when your images are stored in the same project folder or subfolders.

We have to understand how we can go up or down the folder structure to find the correct image file. Here are some examples:

  1. Same Folder: If your image is in the same folder as your HTML file, you can just use the image name.
  2. Subfolder: If your image is in a subfolder, you need to include the subfolder name in the path.
  3. Parent Folder: If your image is in a parent folder, you can use ../ to go up one level in the folder structure.
  4. Subfolder of Parent Folder: You can combine ../ with the subfolder name to access images in a subfolder of the parent folder.

๐Ÿ  Relative Path Examples

<!-- Image in the same folder as your HTML file -->
<img src="photo.jpg" alt="My photo">
<!-- Image in a subfolder -->
<img src="images/photo.jpg" alt="My photo">
<!-- Image in a parent folder -->
<img src="../photo.jpg" alt="My photo">
<!-- Image in a subfolder of parent folder -->
<img src="../images/photo.jpg" alt="My photo">

๐ŸŒ Absolute URLs (Images from Other Websites)

Absolute URLs are taken from the root of the website. They start with / and after that the complete path to the the image file.

Like if we have image photo.jpg in the root of our website, we can use the following absolute URL:

  1. Root Image: If the image is in the root folder of your website, you can use a path starting with /.
  2. Nested Folder: If the image is in a nested folder, you can specify the full path from the root.
  3. External Image: If the image is hosted on another website, you can use the full URL.
<!-- Image in the root of your website -->
<img src="/photo.jpg" alt="External photo">
<!-- Image in a nested folder -->
<img src="/images/folder1/photo.jpg" alt="External photo">
<!-- External image from another website -->
<img src="https://example.com/images/photo.jpg" alt="External photo">

๐ŸŽฏ Best Practice

Keep your images organized in a dedicated images or assets folder. This makes your project cleaner and easier to manage!

Its a good practice to organize your images in a separate folder within your project.

my-website/
โ”‚
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ about.html
โ”œโ”€โ”€ contact.html
โ”‚
โ””โ”€โ”€ images/
โ”œโ”€โ”€ logo.png
โ”œโ”€โ”€ hero-banner.jpg
โ”œโ”€โ”€ team/
โ”‚ โ”œโ”€โ”€ john.jpg
โ”‚ โ””โ”€โ”€ sarah.jpg
โ””โ”€โ”€ products/
โ”œโ”€โ”€ product1.jpg
โ””โ”€โ”€ product2.jpg

๐ŸŽจ img Tag Attributes

The <img> tag has several attributes that help you control how images are displayed in browser like:

Attribute Purpose Example
src Image file path (required) src="images/photo.jpg"
alt Alternative text (required) alt="Beautiful sunset"
width Image width in pixels width="300"
height Image height in pixels height="200"
title Tooltip text on hover title="Sunset at the beach"
loading When to load the image loading="lazy"

๐Ÿ“ Controlling Image Size

We can use width and height attributes to control the size of the image. There are some things to keep in mind:

<!-- Set specific dimensions -->
<img src="photo.jpg" alt="My photo" width="300" height="200">
<!-- Set only width (height adjusts proportionally) -->
<img src="photo.jpg" alt="My photo" width="300">
<!-- Set only height (width adjusts proportionally) -->
<img src="photo.jpg" alt="My photo" height="200">

๐Ÿ’ก Pro Tip

When you specify only width OR height, the browser automatically scales the other dimension proportionally so that the image maintains its aspect ratio and it does not look stretched or squished.

โšก Lazy Loading for Performance

Sometimes, we may want to load images once the user scrolls to them, instead of loading all images at once when the page loads. This can speed up page loading times, especially for pages with many images.

<!-- Load image only when user scrolls near it -->
<img src="large-image.jpg" alt="Description" loading="lazy">

๐ŸŽฏ Writing Great Alt Text

We have to remember that Alt text is very important for accessibility and SEO. Hereโ€™s how to write it well:

โœ… Good Alt Text Examples:

<!-- Descriptive and specific -->
<img src="golden-retriever.jpg" alt="Golden retriever playing fetch in a sunny park">
<!-- For decorative images, use empty alt -->
<img src="decorative-border.png" alt="">
<!-- For functional images (like buttons) -->
<img src="search-icon.png" alt="Search">
<!-- For informational images -->
<img src="sales-chart.png" alt="Sales increased 25% from January to March 2024">

โŒ Poor Alt Text Examples:

<!-- Too vague -->
<img src="dog.jpg" alt="dog">
<!-- Redundant -->
<img src="photo.jpg" alt="Photo of a dog">
<!-- Too long -->
<img src="dog.jpg" alt="This is a photo I took last summer of my golden retriever named Max who loves playing fetch and swimming in the lake near our house">

๐ŸŽฏ Alt Text Guidelines

  • Be descriptive but concise (125 characters or less)
  • Donโ€™t start with โ€œImage ofโ€ or โ€œPicture ofโ€
  • For decorative images, use empty alt: alt=""
  • For complex images (charts, graphs), describe the key information

๐Ÿ–ผ๏ธ Common Image Formats

There are different image formats that work best for different purposes:

Format Best For Pros Cons
JPEG (.jpg) Photos, complex images Small file size, good quality No transparency, lossy compression
PNG (.png) Graphics, logos, transparency Transparency support, lossless Larger file sizes
GIF (.gif) Simple animations, small graphics Animation support, small files Limited colors (256)
SVG (.svg) Icons, logos, simple graphics Scalable, small files, editable Not good for photos
WebP (.webp) Modern web images Excellent compression, quality Not supported by older browsers

๐Ÿ“ฑ Responsive Images

This is very important concept in modern web development. We should always make sure that the images that we want to display on our website looks good on all devices like for phones it should not be too big and for desktops it should not be too small.

๐ŸŽฏ Basic Responsive Image

To make an image responsive, we can set its width to 100% and height to auto in style. This will make the image only take the width of its parent container and adjust its height automatically to maintain the aspect ratio.

<img src="photo.jpg" alt="Description" style="max-width: 100%; height: auto;">

๐Ÿ–ผ๏ธ Different Images for Different Screen Sizes

We can use the <picture> element to provide different images for different screen sizes. Based on the deviceโ€™s screen width, the browser will choose the correct image and display it.

Here is an example of how we can use the <picture> element to provide different images for different screen sizes:

<picture>
<source media="(max-width: 600px)" srcset="small-image.jpg">
<source media="(max-width: 1200px)" srcset="medium-image.jpg">
<img src="large-image.jpg" alt="Responsive image">
</picture>

As you can see we have used the <source> tag along with the media attribute to make browser display image based on the screen size. Like if the screen width is less than or equal to 600px, it will display small-image.jpg, if the screen width is less than or equal to 1200px, it will display medium-image.jpg, and for larger screens, it will display large-image.jpg.

๐ŸŽฎ Try It Yourself!

We have learned a lot about images in HTML. Now itโ€™s time we use our knowledge.

  1. Make a travel blog post featuring:
    • Multiple photos with descriptive captions
    • A short video tour
    • Audio narration or local music

๐ŸŽŠ What Youโ€™ve Mastered

Fantastic work! You now know how to:

  • โœ… Add images with proper src and alt attributes
  • โœ… Organize images with folder structures
  • โœ… Create responsive images for different devices
  • โœ… Write accessible alt text for all users
  • โœ… Optimize images for performance
  • โœ… Use different media formats appropriately

๐Ÿš€ Whatโ€™s Next?

In our next lesson, weโ€™ll look into HTML Tables - learn how to organize and display data in rows and columns.

Share & Connect