HTML Images
A page with only text is like a book with no pictures. It can be great, but a photo of a harbor at sunrise, a chart that shows a trend, or a diagram that explains a machine can say in one second what takes a paragraph to describe.
Images are also where web pages most often go wrong. They can make a page slow, break on small screens, shut out people who cannot see them, and cost visitors real money on mobile data. The good news is that every one of those problems has a simple, well-known solution in HTML. In this section, you will learn how to add images that look sharp, load fast, and work for everyone.
What You Will Learnβ
In this section, you will be able to:
- Add an image to a page with the
<img>element - Write a correct
srcand a meaningfulalttext - Set
widthandheightso your page does not jump around while loading - Pick the right image format for photos, graphics, and icons
- Serve the right size to every screen with responsive images,
srcset, andsizes - Choose formats and crops with the
<picture>element - Speed up your pages with lazy loading
- Add captions with
<figure>and<figcaption> - Make images accessible to people who cannot see them
You should know how to write basic HTML elements and how file paths work. If you need a refresher, read Relative URLs first.
Your First Imageβ
An image takes only one element. Here is the smallest useful version.
- HTML Code
- Browser Output
<img
src="images/harbor.jpg"
alt="Fishing boats resting in a calm harbor at sunrise"
width="300"
height="160"
/>
The demo above uses a small drawing built right into the page, because this lesson has no image files to load. In your own project, src points to a real file such as images/harbor.jpg.
The Anatomy of an Imageβ
An <img> is a void element: it has no content and no closing tag, like <br /> and <hr />. All the information lives in its attributes.
<img src="images/harbor.jpg" alt="Fishing boats at sunrise" width="300" height="160" />
| Attribute | What it does | Required? |
|---|---|---|
src | The address of the image file | Yes |
alt | A text alternative for people and tools that cannot see the image | Yes, always |
width and height | The image's size in pixels, so the browser can reserve space | Strongly recommended |
loading | Whether to load the image right away or later (lazy) | Optional |
srcset and sizes | Several versions of the image for different screens | Optional |
Three Questions to Ask About Every Imageβ
Before you add an image, ask yourself these three questions. They guide almost every decision in this section.
| Question | Why it matters | Where you will learn it |
|---|---|---|
| Does the image mean something? | Meaningful images need helpful alt text. Decorative ones need an empty alt | Src and Alt, Accessible Images |
| How big will it be on screen? | A huge file on a small phone wastes data and time | Responsive Images, Srcset and Sizes |
| What is the best file type? | A photo, a logo, and a chart each have a best format | Image Formats |
Choosing a Format at a Glanceβ
You will study formats in depth later. This quick map gets you started.
| Your image is... | Good choice | Why |
|---|---|---|
| A photograph | JPEG, WebP, or AVIF | Small files for rich colors |
| A logo, icon, or simple illustration | SVG | Sharp at any size, and tiny |
| A screenshot or image with sharp text | PNG or WebP | Keeps edges crisp |
| A short animation | Animated WebP or video | Much smaller than a GIF |
Images and Page Speedβ
Images are usually the heaviest part of a web page. On many sites, they make up more than half of everything a visitor has to download. A few habits make a big difference.
- Resize before you upload. Do not publish a 6000-pixel photo to show it at 600 pixels.
- Use modern formats like WebP and AVIF when they fit.
- Give the browser sizes with
widthandheight, so the page does not jump. - Offer several versions with
srcset, so phones download smaller files. - Load later what is not visible yet with
loading="lazy".
Visitors around the world use slow connections and pay for each megabyte. Efficient images are not just a technical nicety. They make your page usable and affordable for everyone.
Images and Accessibilityβ
Some visitors cannot see your images. They may use a screen reader, have a very slow connection where images have not loaded, or use a text-only browser. The alt attribute is how your page speaks to them.
- Not helpful
- Helpful
<img src="chart.png" alt="chart" />
<img src="photo1.jpg" alt="IMG_20261003_0457.jpg" />
<img src="team.jpg" />
<img src="chart.png" alt="Bar chart: course sign-ups doubled between March and June" />
<img src="photo1.jpg" alt="Students building a website together in a classroom" />
<img src="divider.svg" alt="" />
The last image is purely decorative, so its alt is intentionally empty.
Imagine reading the page aloud to a friend over the phone. What would you say in place of the picture? That sentence is your alt text.
Your Learning Pathβ
Follow the lessons in order. Each one builds on the one before.
ποΈ The img Element
Learn how the HTML img element works. Understand how browsers load images, the core attributes, why images are inline, how to fix the small gap below an image, make images fit with CSS, handle broken images, and choose between img, picture, SVG, and CSS backgrounds on CodeHarborHub.
ποΈ Src and Alt
Learn how to write the src and alt attributes of an HTML image. Point to the right file with relative, root-relative and absolute paths, name files well, and write alt text for photos, icons, charts, logos, linked images and decorative graphics in any language on CodeHarborHub.
ποΈ Width and Height
Learn how the width and height attributes stop layout shift in HTML. Understand aspect ratio, how browsers reserve space, responsive sizing with CSS, the aspect-ratio property, high-density screens, and Cumulative Layout Shift on CodeHarborHub.
ποΈ Image Formats
Learn which image format to use on the web. Compare JPEG, PNG, GIF, WebP, AVIF and SVG, understand lossy and lossless compression, transparency and animation, browser support, optimization tools, metadata privacy, and how to choose the best format on CodeHarborHub.
ποΈ Responsive Images
Learn the big picture of responsive images in HTML. Understand screen sizes and pixel density, flexible images with CSS, resolution switching with srcset, art direction with the picture element, fluid galleries with CSS grid, and how to test what the browser downloads on CodeHarborHub.
ποΈ Srcset and Sizes
Learn how srcset and sizes work in HTML. Understand width descriptors and pixel density descriptors, how to write the sizes attribute with media conditions and calc, how browsers choose a file, how to plan image widths, test results, and avoid common mistakes on CodeHarborHub.
ποΈ Picture Element
Learn how the HTML picture element works. Use source and img together to switch image formats with fallbacks, art direction with media queries, combine srcset and sizes, serve dark-mode images, and avoid common mistakes with AVIF, WebP and JPEG on CodeHarborHub.
ποΈ Lazy Loading
Learn how to lazy load images in HTML with the loading attribute. Understand which images to lazy load and which not to, protect your main image, reserve space with width and height, lazy load iframes, use IntersectionObserver, and measure the results on CodeHarborHub.
ποΈ Figure and Figcaption
Learn how to use the HTML figure and figcaption elements to add captions to images, diagrams, code, quotes and tables. Understand captions versus alt text, numbering with CSS counters, credits and licenses, accessibility, styling, and common mistakes on CodeHarborHub.
ποΈ Accessible Images
Learn how to make every image in your HTML pages accessible. Decide between informative, decorative, functional, and complex images, write useful alt text, handle text in images, linked images, SVG, animated GIFs, and test with real tools on CodeHarborHub.
Common Beginner Mistakesβ
Here are a few traps worth knowing about before you start. Each one has its own lesson later.
| Mistake | What goes wrong | Where you will fix it |
|---|---|---|
Leaving out alt, or writing "image" | Screen reader users get nothing, or noise | Src and Alt |
| A wrong file path | The image shows as a broken icon | Src and Alt |
No width and height | The text jumps down when the image finally loads | Width and Height |
| Using a huge photo for a small space | Slow pages and wasted data | Responsive Images |
| Using PNG for every photo | Files several times bigger than needed | Image Formats |
| Loading every image at once | The page feels slow, even for images far below the fold | Lazy Loading |
| Putting important text inside an image | It cannot be searched, translated, or read aloud | Accessible Images |
| Using someone else's picture without permission | Copyright trouble | See the tip below |
Do not copy pictures from other websites. Use your own photos and drawings, or images with a license that allows reuse, and follow its rules for credit. When in doubt, ask or choose a different picture.
Images for a Global Audienceβ
Your visitors come from many countries and cultures. A few thoughtful habits help.
- Write
alttext in the language of the page, and set thelangattribute. - Avoid text inside images. It cannot be translated by browsers or read aloud. Put the words in HTML instead.
- Choose pictures that many people can relate to. Gestures, symbols, and clothing can mean different things in different places.
- Think about data costs. Smaller files are kinder to visitors on limited plans.
- Show diverse people when your pictures include people.
Quick Recapβ
Test yourself. Click a question to reveal the answer.
1. Which element adds an image, and does it need a closing tag?
The <img> element. It is a void element, so it has no closing tag.
2. Which two attributes does every image need?
src, the address of the file, and alt, a text alternative for people and tools that cannot see the image.
3. Why should you set width and height?
They let the browser reserve space before the image loads, so the page does not jump.
4. What should the alt be for a purely decorative image?
An empty value, alt="", so screen readers skip it.
5. Name two ways to make images load faster.
Resize them before uploading, use modern formats like WebP or AVIF, offer smaller versions with srcset, and use loading="lazy" for images below the fold.
Key Takeawaysβ
- Add images with the
<img>element, which needssrcandalt. - Write
alttext that says what the image means. Usealt=""for decoration only. - Set
widthandheightto keep your layout steady while images load. - Choose formats, sizes, and loading behavior with page speed and data costs in mind.
- Make every image work for everyone: describe it, caption it when it helps, and keep important text out of pictures.
Ready to begin? Start with The img Element.