HTML Images Tutorial with Examples

HTML Images Tutorial with Examples

HTML Images – img Tag, src, alt & Responsive | TipsInLearning
Home HTML Course HTML Images
Lesson 13 of 22

HTML Images

Master the <img> tag — embed images, write proper alt text, control dimensions, make images responsive, and optimize them for fast page loads.

12 min read
Beginner
Images & Media
The alt attribute is the most important part of any image tag — and the part beginners most often skip or fill in with "image". Good alt text is both an accessibility requirement and an SEO signal. Google reads it, screen readers read it aloud, and it's what displays when the image fails to load. Worth taking seriously from your very first image.
What You'll Learn
Embed images with the <img> tag
Write proper descriptive alt text
Control image dimensions correctly
Make images fluid and responsive
Use srcset and picture for different screens
Choose the right image format

What are HTML Images?

The <img> tag embeds an image into a webpage. It's a void element — it has no closing tag and no content between tags. All the information is in its attributes: where to find the image (src), what it shows (alt), and how big it is (width/height).

Importantly, images aren't literally embedded in your HTML file. The browser reads your HTML, sees the src URL, makes a separate HTTP request to fetch the image file, and then displays it in the right place on the page. This is why a broken image shows a placeholder icon — the browser knows where to put it but couldn't fetch the file.

Void element: <img> never has a closing tag. In HTML5, write <img src="..." alt="...">. You may also see <img /> with a self-closing slash — that's valid too but not required in HTML5.

Image Syntax

HTML — img tag
<!-- Minimum required -->
<img src="photo.jpg" alt="Mountain landscape at sunset" />

<!-- With dimensions (always recommended) -->
<img src="photo.jpg"
     alt="Mountain landscape at sunset"
     width="800"
     height="600" />

The src Attribute

The src attribute is required and specifies the image URL or path. Three types of paths work:

Absolute URLs
Full web address — https://cdn.example.com/img.jpg
Relative Paths
Local files — ./images/photo.jpg
CDN URLs
Content delivery networks for speed
HTML — src paths
<!-- From the web -->
<img src="https://example.com/photo.jpg" alt="Photo"/>

<!-- Same folder as your HTML file -->
<img src="logo.png" alt="Logo"/>

<!-- In a subfolder -->
<img src="./images/hero.jpg" alt="Hero image"/>

<!-- In a parent directory -->
<img src="../assets/icon.svg" alt="Icon"/>

The alt Attribute

The alt attribute provides text that displays when the image fails to load, and is read aloud by screen readers. It's also indexed by search engines. Writing good alt text is one of the easiest things you can do for accessibility and SEO simultaneously.

HTML — alt text
<!-- GOOD: descriptive -->
<img src="team.jpg"
     alt="Four developers sitting around a laptop at TipsInLearning office"/>

<!-- BAD: useless -->
<img src="team.jpg" alt="image"/>

<!-- Decorative image: use empty alt -->
<img src="divider.png" alt=""/>
Empty alt is intentional. alt="" on a decorative image is correct — it tells screen readers to skip it entirely. Do NOT omit the alt attribute completely — that forces screen readers to read out the filename, which is confusing and pointless.

Image Sizing

Always specify width and height — even when CSS controls the visual size. These intrinsic dimensions tell the browser how much layout space to reserve before the image loads, preventing page jump (Cumulative Layout Shift).

HTML + CSS — sizing
<!-- Set intrinsic size in HTML (prevents layout shift) -->
<img src="photo.jpg" alt="Photo" width="800" height="600"/>

<!-- Control visual size with CSS -->
<style>
  img { width: 400px; height: auto; }
</style>

Responsive Images

Use CSS to make images fluid, and srcset or <picture> to serve appropriately-sized files to different screen sizes:

CSS + HTML — responsive
<!-- Fluid: scales with container -->
<style>
  img { max-width: 100%; height: auto; }
</style>

<!-- srcset: browser picks the right resolution -->
<img src="photo-sm.jpg"
     srcset="photo-sm.jpg 480w, photo-md.jpg 800w, photo-lg.jpg 1200w"
     alt="Responsive photo"
     loading="lazy"/>

<!-- picture: different crops for different screens -->
<picture>
  <source media="(max-width: 600px)" srcset="portrait.jpg"/>
  <img src="landscape.jpg" alt="Photo"/>
</picture>

Image Formats

WebP
Best modern format — smaller than JPEG/PNG, supports transparency
JPEG
Best for photos — small file, no transparency
PNG
Transparent backgrounds — larger file than JPEG
SVG
Icons & logos — scalable to any size, tiny file

Common Mistakes

Missing or vague alt text
Writing alt="image" is nearly as bad as no alt at all. Describe what the image actually shows. Screen readers read it, Google indexes it, and it appears when the image fails.
Not specifying width and height
No dimensions means no reserved space. The page jumps as images load. This hurts your Core Web Vitals CLS score and the user experience on slow connections.
Uploading uncompressed images
A 5MB photo scaled down to 300px with CSS is still a 5MB download. Compress images before uploading using TinyPNG, Squoosh, or ImageOptim — you can cut 60-80% of file size with no visible quality loss.
Try It — 4 Image Challenges
1Add an image using a URL from the web with descriptive alt text and width/height attributes
2Add max-width:100%; height:auto; in CSS, resize the browser, and watch the image scale
3Add loading="lazy" to the image and verify it in DevTools Network tab
4Wrap the image in an <a> tag to make it a clickable link to your site
Lesson Summary
1<img> is a void element — no closing tag, all information in attributes
2Both src and alt are required — never omit either one
3Write descriptive alt text — describe what the image shows, not "image" or "photo"
4Always specify width and height to prevent Cumulative Layout Shift
5max-width:100%; height:auto makes images fluid — they scale with their container
6Add loading="lazy" to below-fold images to improve initial page load speed

Questions & Answers

Common questions beginners ask about this topic:

Do I need both width and height attributes on every image?

Yes — and this matters more than most beginners realize. When you don't specify dimensions, the browser doesn't know how much space to reserve while the image loads. As the image arrives, the page layout shifts — text moves, buttons jump. Google's Core Web Vitals calls this Cumulative Layout Shift (CLS) and it hurts your search rankings. Specifying width and height in your HTML lets the browser reserve the right amount of space before the image downloads.

What should I write in the alt attribute?

Describe what the image shows — as you'd explain it to someone who can't see it. For a photo of a dog: alt="Golden retriever running through a park with a tennis ball". For a chart: describe what the data shows. For a logo used as a link: describe where the link goes. For purely decorative images — dividers, spacers, backgrounds — use an empty alt: alt="". This tells screen readers to skip the image rather than reading out a confusing filename.

What's the best image format to use for websites?

It depends on the image type. WebP is the best modern format — it's smaller than JPEG and PNG at the same quality and supports transparency. Use JPEG for photos when WebP isn't supported. Use PNG for graphics that need transparent backgrounds. Use SVG for icons, logos, and illustrations — they scale to any size without pixelation. For a practical approach: save as WebP first with a JPEG fallback using the <picture> element.

What's the difference between srcset and the picture element?

Both let you serve different images at different sizes, but for different reasons. srcset gives the browser a list of image files at different resolutions and lets it pick the best one for the screen — useful for serving smaller files on mobile. The <picture> element lets you serve completely different images at different breakpoints — for example, a wide landscape crop on desktop and a tighter portrait crop on mobile. Use srcset for resolution switching. Use picture for art direction (different compositions at different sizes).

My image shows a broken icon. What's wrong?

Almost always a wrong file path in the src attribute. Check: is the filename spelled correctly including case? (image.jpg ≠ Image.jpg on Linux servers.) Is the file in the right folder relative to your HTML file? Are you using forward slashes not backslashes? Open browser DevTools (F12 → Network tab) and look at the failed image request — the exact URL the browser tried will be shown, making the problem immediately obvious.

Post a Comment

Previous Post Next Post