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.
<img src="nature.jpg" alt="Beautiful Nature">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.
<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.
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.
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 Structure Webpage Structure
────────────────── ─────────────────
Chapter Heading
│ │
▼ ▼
Paragraph Paragraph
│ │
▼ ▼
Picture Image
│ │
▼ ▼
Explanation DescriptionTextbook
- 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.
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.
<img src="image.jpg" alt="Description"><img src="nature.jpg" alt="Beautiful Nature">
──── ──────────────────── ──────────────────────
│ │ │
│ │ └── Alternative Text
│ │
│ └── Image Source
│
└── Image Element| Part | Example | Purpose |
|---|---|---|
| Image element | <img> | Displays an image resource |
| src attribute | src="nature.jpg" | Specifies the image location |
| alt attribute | alt="Beautiful Nature" | Provides alternative text |
| width attribute | width="300" | Specifies display width |
| height attribute | height="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.
<img
src="nature.jpg"
alt="Beautiful Nature"
width="600"
height="400"
>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.
<img
src="nature.jpg"
alt="Beautiful Nature"
>| Code | Meaning |
|---|---|
| <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 |
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.
<img
src="flower.jpg"
alt="Flower"
width="300"
height="200"
>| Attribute | Value | Purpose |
|---|---|---|
| src | flower.jpg | Specifies the image file |
| alt | Flower | Provides alternative text |
| width | 300 | Specifies a width of 300 pixels |
| height | 200 | Specifies a height of 200 pixels |
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.
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.
| Format | Common Use | Important Characteristic |
|---|---|---|
| JPG / JPEG | Photographs and complex images | Efficient compression for photographic content |
| PNG | Graphics and images requiring transparency | Supports transparent areas |
| GIF | Simple animations | Supports basic frame animation |
| SVG | Logos, icons, and vector graphics | Scales without losing vector quality |
| WebP | Modern web images | Provides 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.
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.
<img
src="logo.png"
alt="Company Logo"
><img
src="images/logo.png"
alt="Company Logo"
><img
src="https://example.com/images/logo.png"
alt="Company Logo"
>website
│
├── index.html
│
└── images
├── logo.png
├── banner.jpg
└── product.webp
From index.html:
images/logo.png
images/banner.jpg
images/product.webp| Path Type | Example | Common Use |
|---|---|---|
| Same folder | logo.png | Image beside the current HTML file |
| Relative folder path | images/logo.png | Image inside a project folder |
| Absolute URL | https://example.com/logo.png | Externally hosted image |
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.
<h1>Welcome to Bytecrest</h1>
<img
src="logo.png"
alt="Bytecrest Logo"
width="250"
height="150"
>
<p>Learn Programming Easily.</p>Welcome to Bytecrest
The <img> element can be combined with headings, paragraphs, links, figures, and other HTML elements to build complete page layouts.
Image as a Link
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.
<a href="https://www.bytecrest.com">
<img
src="logo.png"
alt="Bytecrest"
>
</a><a href="Destination">
│
▼
<img src="Image">
│
▼
Clickable Image
│
▼
Open Destination| Element | Purpose |
|---|---|
| <a> | Creates the hyperlink and specifies the destination |
| <img> | Displays the clickable image |
| href | Specifies where the link should go |
| src | Specifies which image should be displayed |
| alt | Provides alternative text for the image |
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.
HTML Source
│
▼
Find <img> Element
│
▼
Read src Attribute
│
▼
Resolve Image Path
│
▼
Request Image File
│
▼
Receive Resource
│
▼
Decode Image Data
│
▼
Display Image<img
src="nature.jpg"
alt="Beautiful Nature"
>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.
<img
src="car.png"
alt="Sports Car"
>If car.png cannot be loaded, the browser cannot display the intended visual resource.
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.
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
Images should provide appropriate alternative text when the image communicates meaningful information.
The src path must correctly match the location of the image file.
A file named logo.png will not load when the source incorrectly points to logo.jpg.
Large image resources can increase page loading time and waste bandwidth.
Incorrect width and height proportions can distort the visual appearance.
Alternative text should communicate the purpose or content of meaningful images rather than using vague descriptions.
Different image formats are suitable for different types of visual content.
Externally hosted images can disappear or change when you do not control the source.
<!-- ❌ Missing alt attribute -->
<img src="logo.png">
<!-- ✅ Alternative text provided -->
<img
src="logo.png"
alt="Company Logo"
><!-- 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"
><!-- 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.
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.
<!-- 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=""
>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.