LearnContact
Lesson 169 min read

HTML Images

Learn how to display images in HTML using the image element. Understand src, alt, width, height, image formats, relative and absolute paths, missing images, and clickable images.

Introduction

In the previous lesson, you learned about HTML Links and how hyperlinks connect webpages, websites, files, and other resources.

Text is essential for providing information, but webpages containing only text can feel plain and difficult to scan.

Images make webpages more attractive, improve visual communication, help explain concepts, display products, support branding, and increase user engagement.

HTML provides the <img> element to display images such as photographs, logos, icons, illustrations, diagrams, and graphics on webpages.

Basic HTML Image
<img src="nature.jpg" alt="Beautiful Nature">
Add the Image Element
Specify the Image Source
Provide Alternative Text
Browser Loads the Image File
Image is Displayed on the Page
Images Add Visual Communication

Images allow webpages to communicate information visually instead of relying only on written text.

What is an Image?

An HTML image is a graphical resource displayed on a webpage using the <img> element.

The image itself is usually stored as a separate file. The HTML document contains a reference that tells the browser where that file is located.

Images can include photographs, company logos, product pictures, icons, illustrations, diagrams, charts, screenshots, banners, and many other types of visual content.

Simple Image Element
<img
    src="photo.jpg"
    alt="Mountain Landscape"
>

Text Content

  • Communicates using words.
  • Explains information in detail.
  • Can require more reading.
  • Forms the written structure of a page.

Image Content

  • Communicates visually.
  • Can explain information quickly.
  • Adds visual interest.
  • Supports written content.

Photographs

Display people, places, events, products, and real-world scenes.

Logos

Represent companies, organizations, products, and brands.

Illustrations

Explain ideas using custom visual artwork.

Diagrams

Explain processes, systems, relationships, and data visually.

Icons

Represent actions, features, categories, and concepts.

Screenshots

Show software interfaces, examples, and instructions.

Simple Definition

An HTML image is a visual resource displayed on a webpage using the <img> element.

Why Do We Need Images?

Images are an important part of modern web design because many types of information are easier to understand visually.

A product description becomes more useful when users can see the product. A tutorial becomes easier to follow when it contains screenshots or diagrams. A company becomes easier to recognize when its logo is displayed consistently.

Without Images

  • Pages can appear plain and repetitive.
  • Products are difficult to visualize.
  • Visual concepts require long explanations.
  • Brand identity becomes weaker.
  • Users may lose interest more quickly.

With Images

  • Pages become visually engaging.
  • Products can be displayed clearly.
  • Complex ideas can be explained visually.
  • Brand identity becomes recognizable.
  • Content becomes easier to scan and understand.

Visual Appeal

Make webpages more attractive and interesting.

Understanding

Help explain ideas that may be difficult to describe with text alone.

Product Display

Allow users to see products before making decisions.

Branding

Display logos and visual identities consistently.

Learning

Support tutorials with diagrams, screenshots, and illustrations.

Engagement

Encourage users to explore and interact with content.

User Opens a Webpage
Visual Content Attracts Attention
Image Supports the Written Content
Information Becomes Easier to Understand
User Experience Improves
Images Should Support the Content

Images are most effective when they provide useful information, improve understanding, or support the purpose of the webpage.

Real-World Analogy

Imagine reading a school textbook containing only long paragraphs.

Now imagine the same textbook containing diagrams, photographs, maps, charts, and illustrations alongside the written explanations.

The visual version is usually easier to understand because images help readers connect written information with visual examples.

Textbook and Webpage
Textbook Structure       Webpage Structure
──────────────────       ─────────────────

Chapter                  Heading
    │                         │
    ▼                         ▼
Paragraph                Paragraph
    │                         │
    ▼                         ▼
Picture                  Image
    │                         │
    ▼                         ▼
Explanation              Description

Textbook

  • Contains written explanations.
  • Uses pictures to support concepts.
  • Uses diagrams for complex ideas.
  • Combines text and visuals.

Webpage

  • Contains written content.
  • Uses images to support information.
  • Uses graphics for visual explanation.
  • Combines HTML text and visual resources.
Images Are Visual Explanations

Just as pictures improve a textbook, useful images can make webpage content easier to understand and remember.

HTML Image Element & Parts

Images are displayed using the <img> element.

Unlike elements such as paragraphs and headings, the <img> element does not wrap content between an opening and closing tag.

The image element is an empty element. Its information is provided through attributes such as src, alt, width, and height.

General Image Syntax
<img src="image.jpg" alt="Description">
Image Element Anatomy
<img src="nature.jpg" alt="Beautiful Nature">
──── ──────────────────── ──────────────────────
 │            │                       │
 │            │                       └── Alternative Text
 │            │
 │            └── Image Source
 │
 └── Image Element
PartExamplePurpose
Image element<img>Displays an image resource
src attributesrc="nature.jpg"Specifies the image location
alt attributealt="Beautiful Nature"Provides alternative text
width attributewidth="300"Specifies display width
height attributeheight="200"Specifies display height

1️⃣ src Attribute

Specifies the location or URL of the image resource.

2️⃣ alt Attribute

Provides a text alternative for the image.

3️⃣ width Attribute

Specifies the intended display width.

4️⃣ height Attribute

Specifies the intended display height.

Complete Image Element
<img
    src="nature.jpg"
    alt="Beautiful Nature"
    width="600"
    height="400"
>
The img Element Has No Closing Tag

The <img> element is an empty element because it does not contain child content between opening and closing tags.

Example 1: Displaying an Image

The simplest image requires a source that identifies the image file and alternative text that describes the image when appropriate.

Display an Image
<img
    src="nature.jpg"
    alt="Beautiful Nature"
>
CodeMeaning
<img>Creates the image element
src="nature.jpg"Tells the browser where the image file is located
alt="Beautiful Nature"Provides a text alternative describing the image
Image Example
Browser Finds the img Element
Browser Reads src
Browser Requests nature.jpg
Image File is Received
Image is Displayed
The Browser Loads a Separate Resource

The HTML document references the image file. The browser must locate and load that resource before it can display the image.

Example 2: Setting Width and Height

The width and height attributes can specify the intended display dimensions of an image.

Image Width and Height
<img
    src="flower.jpg"
    alt="Flower"
    width="300"
    height="200"
>
AttributeValuePurpose
srcflower.jpgSpecifies the image file
altFlowerProvides alternative text
width300Specifies a width of 300 pixels
height200Specifies a height of 200 pixels
Sized Image Example

Distorted Image

  • Width and height use the wrong proportions.
  • The image appears stretched.
  • People and objects look unnatural.
  • Visual quality becomes unprofessional.

Correct Proportions

  • The original aspect ratio is preserved.
  • The image appears natural.
  • Objects keep their correct shape.
  • The design looks professional.
Preserve the Aspect Ratio

Avoid forcing an image into width and height values that distort its natural proportions.

Image File Formats

Images are available in different file formats. Each format has different characteristics and is suitable for different types of visual content.

FormatCommon UseImportant Characteristic
JPG / JPEGPhotographs and complex imagesEfficient compression for photographic content
PNGGraphics and images requiring transparencySupports transparent areas
GIFSimple animationsSupports basic frame animation
SVGLogos, icons, and vector graphicsScales without losing vector quality
WebPModern web imagesProvides efficient compression for many web use cases

JPG / JPEG

Commonly used for photographs and images containing many colors.

PNG

Useful for graphics that require transparency.

GIF

Commonly used for simple animated images.

SVG

Ideal for scalable vector graphics such as logos and icons.

WebP

A modern format designed for efficient web image delivery.

Raster Images

  • Built from pixels.
  • Examples include JPG and PNG.
  • Can lose quality when enlarged significantly.
  • Common for photographs.

Vector Images

  • Built from mathematical shapes and paths.
  • SVG is a common web vector format.
  • Can scale without pixelation.
  • Common for logos and icons.
Choose the Format for the Content

Use image formats according to the type of visual content, required quality, transparency needs, scalability, and performance requirements.

Relative vs Absolute Paths

The src attribute must tell the browser where the image resource is located.

Images inside the same project commonly use relative paths, while externally hosted images can use absolute URLs.

Relative Path

  • Points to a resource within the project or website.
  • Does not require the complete domain.
  • Depends on the current document location.
  • Commonly used for project images.

Absolute URL

  • Contains a complete web address.
  • Usually includes the protocol and domain.
  • Can point to an external resource.
  • Does not depend on the local project structure.
Image in the Same Folder
<img
    src="logo.png"
    alt="Company Logo"
>
Image in an Images Folder
<img
    src="images/logo.png"
    alt="Company Logo"
>
External Image URL
<img
    src="https://example.com/images/logo.png"
    alt="Company Logo"
>
Project Structure
website
│
├── index.html
│
└── images
    ├── logo.png
    ├── banner.jpg
    └── product.webp


From index.html:

images/logo.png
images/banner.jpg
images/product.webp
Path TypeExampleCommon Use
Same folderlogo.pngImage beside the current HTML file
Relative folder pathimages/logo.pngImage inside a project folder
Absolute URLhttps://example.com/logo.pngExternally hosted image
Organize Project Images

Keeping project images inside a dedicated images folder makes the project structure easier to understand and maintain.

Example 3: Complete Image Example

Images are commonly combined with headings, paragraphs, links, and other HTML elements to create complete webpage content.

Complete Image Example
<h1>Welcome to Bytecrest</h1>

<img
    src="logo.png"
    alt="Bytecrest Logo"
    width="250"
    height="150"
>

<p>Learn Programming Easily.</p>
Complete Webpage Example

Welcome to Bytecrest

Display the Heading
Load the Logo Image
Display the Image
Display the Paragraph
Complete Page Content Appears
Images Work with Other HTML Elements

The <img> element can be combined with headings, paragraphs, links, figures, and other HTML elements to build complete page layouts.

An image can become clickable by placing the <img> element inside an <a> anchor element.

The anchor element controls the destination, while the image becomes the clickable content.

Clickable Image
<a href="https://www.bytecrest.com">
    <img
        src="logo.png"
        alt="Bytecrest"
    >
</a>
Image Link Structure
<a href="Destination">
        │
        ▼
    <img src="Image">
        │
        ▼
Clickable Image
        │
        ▼
Open Destination
ElementPurpose
<a>Creates the hyperlink and specifies the destination
<img>Displays the clickable image
hrefSpecifies where the link should go
srcSpecifies which image should be displayed
altProvides alternative text for the image
Browser Displays the Image
Image is Inside an Anchor
User Activates the Image
Browser Reads the href Destination
Destination Opens
Use Meaningful Alternative Text

When an image is used as a link, its alternative text should help communicate the purpose or destination of the link.

Browser Rendering Flow

When the browser encounters an <img> element, it reads the source, requests the image resource, decodes the received file, and displays the visual content.

Read HTML Source
Find the img Element
Read the src Attribute
Resolve the Image Location
Request the Image Resource
Receive and Decode the Image
Display the Image on the Page
Simplified Image Processing
HTML Source
    │
    ▼
Find <img> Element
    │
    ▼
Read src Attribute
    │
    ▼
Resolve Image Path
    │
    ▼
Request Image File
    │
    ▼
Receive Resource
    │
    ▼
Decode Image Data
    │
    ▼
Display Image
Source Image
<img
    src="nature.jpg"
    alt="Beautiful Nature"
>
Images Are Separate Resources

The browser normally loads image files separately from the HTML document, which means image size and delivery can affect page performance.

Missing Image Example

An image may fail to load when the file does not exist, the path is incorrect, the filename is wrong, the network request fails, or the resource is unavailable.

Missing Image
<img
    src="car.png"
    alt="Sports Car"
>

If car.png cannot be loaded, the browser cannot display the intended visual resource.

Broken Image Example

Without Useful alt Text

  • The image is missing.
  • Users receive little context.
  • Screen reader users lose information.
  • The purpose of the visual may be unclear.

With Useful alt Text

  • The image may still be missing.
  • A text alternative is available.
  • Assistive technologies can communicate the alternative.
  • Users receive useful context.
Browser Reads src
Image Request Fails
Visual Resource Cannot Be Displayed
Alternative Text Remains Available
Alternative Text Is Important

Useful alt text provides a text alternative when the image cannot be perceived or displayed and supports users of assistive technologies.

Real-World Applications

Images are used throughout almost every type of modern website and web application.

E-Commerce

Display product images, galleries, thumbnails, and category banners.

Company Websites

Display company logos, office photographs, team members, and services.

Educational Platforms

Use diagrams, charts, illustrations, screenshots, and examples.

Social Media

Display profile pictures, posts, stories, and cover images.

News Websites

Display article photographs, event images, and visual reports.

Restaurant Websites

Display food photography, menus, locations, and interior images.

Travel Websites

Display destinations, hotels, rooms, attractions, and galleries.

Documentation

Use screenshots and diagrams to explain technical concepts.

Advantages of Images

Visual Appeal

Make webpages more attractive and engaging.

Better Understanding

Explain concepts visually and support written information.

Branding

Strengthen visual identity through logos and consistent imagery.

Product Presentation

Allow users to see products and services clearly.

Education

Support learning with diagrams, examples, and screenshots.

Engagement

Encourage users to explore content and remain interested.

Communication

Communicate visual information across language barriers.

Recognition

Help users quickly recognize products, people, places, and actions.

  • Improve webpage appearance.
  • Make content visually engaging.
  • Explain complex ideas visually.
  • Support written explanations.
  • Display products clearly.
  • Show people, places, and events.
  • Support company branding.
  • Display logos and visual identities.
  • Improve educational content.
  • Support tutorials with screenshots.
  • Present diagrams and illustrations.
  • Help users recognize content quickly.
  • Create visual navigation elements.
  • Support marketing and advertising.
  • Improve the overall user experience.

Common Beginner Mistakes

Forgetting the alt Attribute

Images should provide appropriate alternative text when the image communicates meaningful information.

Using the Wrong Image Path

The src path must correctly match the location of the image file.

Using the Wrong File Extension

A file named logo.png will not load when the source incorrectly points to logo.jpg.

Using Very Large Image Files

Large image resources can increase page loading time and waste bandwidth.

Stretching Images

Incorrect width and height proportions can distort the visual appearance.

Writing Poor Alternative Text

Alternative text should communicate the purpose or content of meaningful images rather than using vague descriptions.

Using the Same Format for Everything

Different image formats are suitable for different types of visual content.

Depending on External Images

Externally hosted images can disappear or change when you do not control the source.

Provide Alternative Text
<!-- ❌ Missing alt attribute -->

<img src="logo.png">


<!-- ✅ Alternative text provided -->

<img
    src="logo.png"
    alt="Company Logo"
>
Use the Correct Image Path
<!-- Project Structure

website/
├── index.html
└── images/
    └── logo.png

-->


<!-- ❌ Wrong -->

<img
    src="logo.png"
    alt="Company Logo"
>


<!-- ✅ Correct -->

<img
    src="images/logo.png"
    alt="Company Logo"
>
Match the File Extension
<!-- Actual file: logo.png -->


<!-- ❌ Wrong extension -->

<img
    src="logo.jpg"
    alt="Company Logo"
>


<!-- ✅ Correct extension -->

<img
    src="logo.png"
    alt="Company Logo"
>

Poor Image Usage

  • Missing alternative text.
  • Incorrect file paths.
  • Oversized image files.
  • Distorted proportions.
  • Unoptimized formats.

Better Image Usage

  • Appropriate alternative text.
  • Correct file paths.
  • Optimized image resources.
  • Preserved proportions.
  • Suitable file formats.
Check the Exact Filename

When an image does not appear, verify the folder path, filename, file extension, and relevant letter case.

Best Practices

  • Provide appropriate alt text for meaningful images.
  • Write alternative text that communicates the image purpose or information.
  • Use empty alternative text for purely decorative images when appropriate.
  • Keep image filenames clear and meaningful.
  • Organize project images inside dedicated folders.
  • Use correct relative paths for project images.
  • Use external image URLs carefully.
  • Do not depend on external resources you do not control when reliability is important.
  • Choose image formats according to the type of content.
  • Use JPEG or other suitable compressed formats for photographic content.
  • Use PNG when transparency is required and appropriate.
  • Use SVG for suitable vector graphics such as logos and icons.
  • Consider modern formats such as WebP for efficient delivery.
  • Compress images before publishing them.
  • Avoid uploading images much larger than their intended display size.
  • Preserve the original aspect ratio.
  • Avoid stretching or squashing images.
  • Specify useful image dimensions when appropriate.
  • Use responsive image styling for different screen sizes.
  • Test images on mobile devices.
  • Check image loading on slower connections.
  • Verify image paths after moving files.
  • Verify image paths after renaming folders.
  • Use meaningful filenames instead of random names when possible.
  • Regularly check for missing and broken images.
Professional Image Examples
<!-- Project image -->

<img
    src="images/company-logo.svg"
    alt="Bytecrest"
    width="240"
    height="80"
>


<!-- Product image -->

<img
    src="images/products/laptop.webp"
    alt="Silver laptop open on a desk"
    width="800"
    height="600"
>


<!-- Clickable image -->

<a href="/products/laptop">
    <img
        src="images/products/laptop-thumbnail.webp"
        alt="View Silver Laptop"
        width="300"
        height="225"
    >
</a>


<!-- Decorative image -->

<img
    src="images/decorative-pattern.svg"
    alt=""
>
Optimize Before Uploading

Image optimization should happen before large files are published. Smaller appropriate resources can significantly improve page loading performance.

Frequently Asked Questions

Which HTML element displays an image?

The <img> element displays an image.

Which attribute specifies the image location?

The src attribute specifies the image location or URL.

What does src mean?

src means source and identifies the image resource that the browser should load.

Why is the alt attribute important?

The alt attribute provides a text alternative for an image and is important for accessibility and situations where the visual resource cannot be perceived or displayed.

Does the <img> element require a closing tag?

No. The <img> element is an empty element and does not contain a separate closing tag.

What does the width attribute do?

The width attribute specifies the intended display width of the image.

What does the height attribute do?

The height attribute specifies the intended display height of the image.

What happens if the image path is wrong?

The browser cannot load the intended image resource, and the image may appear broken or unavailable.

What is a relative image path?

A relative path identifies an image location relative to the current project, website, or document location.

What is an absolute image URL?

An absolute image URL contains a complete web address including the protocol and domain.

Which image format is best for photographs?

JPEG is commonly used for photographs, although modern formats such as WebP may also provide efficient delivery depending on the project requirements.

Which image format supports transparency?

PNG supports transparency, and other formats such as WebP can also support transparent image content.

Which format is suitable for logos and icons?

SVG is often suitable for logos and icons because vector graphics can scale without pixelation.

What is WebP?

WebP is a modern image format designed to provide efficient compression for web images.

Can an image be clickable?

Yes. Place the <img> element inside an <a> anchor element.

Can an image link to another page?

Yes. The surrounding anchor element can link the image to another page or resource.

Why should large images be optimized?

Large image files can increase loading time, consume more bandwidth, and reduce website performance.

What is image distortion?

Image distortion happens when an image is displayed using proportions that do not match its natural aspect ratio.

What is an aspect ratio?

An aspect ratio describes the proportional relationship between an image width and height.

Should every image have descriptive alt text?

Meaningful images should have useful text alternatives. Purely decorative images may use empty alternative text when appropriate.

Why is my image not appearing?

Common causes include an incorrect path, wrong filename, wrong extension, unavailable resource, or relevant filename case mismatch.

Should images be stored in a separate folder?

Organizing project images in a dedicated folder is a common practice that improves project structure and maintainability.

Can I use images from another website?

Technically an absolute URL can reference an external image, but you should consider permission, reliability, performance, privacy, and whether you control the resource.

What should I learn after HTML Images?

The next lesson is HTML Lists, where you will learn how to organize related information into ordered and unordered lists.

Key Takeaways

  • HTML images are displayed using the <img> element.
  • The <img> element is an empty element.
  • The src attribute specifies the image resource.
  • The alt attribute provides a text alternative.
  • The width attribute specifies the intended image width.
  • The height attribute specifies the intended image height.
  • Images are normally separate resources loaded by the browser.
  • Images can include photographs, logos, icons, diagrams, and illustrations.
  • JPEG is commonly used for photographic content.
  • PNG is useful when transparency is required.
  • GIF can display simple animations.
  • SVG is suitable for scalable vector graphics.
  • WebP is a modern format for efficient web image delivery.
  • Relative paths are commonly used for project images.
  • Absolute URLs can reference externally hosted images.
  • Image paths must match the actual file location.
  • Filenames and file extensions must be correct.
  • Alternative text supports accessibility.
  • Images can become clickable when placed inside anchor elements.
  • The browser reads src and requests the image resource.
  • Missing resources can result in broken images.
  • Large image files can reduce page performance.
  • Images should be optimized before publishing.
  • Image proportions should be preserved.
  • Images are essential to visual communication on modern websites.

Summary

Images are an important part of modern webpages because they improve visual communication, support written content, strengthen branding, display products, and make information more engaging.

HTML displays images using the <img> element. The src attribute identifies the image resource, while the alt attribute provides a text alternative.

The width and height attributes can specify intended display dimensions, but image proportions should be preserved to avoid distortion.

Different image formats serve different purposes. JPEG is commonly used for photographs, PNG is useful for transparency, GIF supports simple animation, SVG is suitable for scalable vector graphics, and WebP provides efficient modern image compression.

Project images commonly use relative paths, while externally hosted resources can use absolute URLs. Correct folder paths, filenames, extensions, and relevant letter case are essential for successful image loading.

An image can become clickable by placing the <img> element inside an <a> anchor element.

When the browser encounters an image element, it reads the src value, resolves the resource location, requests the image file, decodes the received data, and displays the visual content.

If an image fails to load, useful alternative text helps preserve the meaning or purpose of the missing visual resource.

Professional websites use appropriately sized, optimized, accessible, and well-organized images to improve both user experience and performance.

In the next lesson, you will learn about HTML Lists and how to organize related information using ordered and unordered list structures.

Next Lesson →

Lists