|
1 | 1 | --- |
2 | 2 | id: images-overview |
3 | | -title: "Working with Images in HTML" |
4 | | -sidebar_label: "Working with Images" |
| 3 | +title: "HTML Images" |
| 4 | +sidebar_label: "Images" |
5 | 5 | sidebar_position: 1 |
6 | 6 | slug: /html/images |
7 | | -description: "Master HTML image elements, responsive image techniques, performance optimizations, semantic figures, and image accessibility with CodeHarborHub." |
| 7 | +description: "Learn how to add and manage images in HTML. Master the img element, src and alt, width and height, image formats, responsive images with srcset and the picture element, lazy loading, figure and figcaption, and accessible images with CodeHarborHub." |
8 | 8 | keywords: |
9 | | - - HTML images overview |
10 | | - - HTML img tag tutorial |
11 | | - - responsive images HTML |
| 9 | + - HTML images |
| 10 | + - HTML img tag |
| 11 | + - HTML alt text |
| 12 | + - HTML image formats |
| 13 | + - HTML responsive images |
| 14 | + - HTML srcset and sizes |
| 15 | + - HTML picture element |
| 16 | + - HTML lazy loading images |
| 17 | + - HTML figure and figcaption |
| 18 | + - HTML accessible images |
12 | 19 | - web image optimization |
| 20 | + - learn HTML images |
13 | 21 | - CodeHarborHub HTML |
14 | | -authors: [ajay-dhangar] |
15 | | -tags: [html, images, media, beginner] |
16 | | ---- |
| 22 | +tags: [html, web-development, beginner, images, media, frontend] |
| 23 | +--- |
| 24 | + |
| 25 | +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. |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +<AdsComponent /> |
| 30 | +<br /> |
| 31 | + |
| 32 | +## What You Will Learn |
| 33 | + |
| 34 | +In this section, you will be able to: |
| 35 | + |
| 36 | +- Add an image to a page with the **`<img>` element** |
| 37 | +- Write a correct **`src`** and a meaningful **`alt`** text |
| 38 | +- Set **`width` and `height`** so your page does not jump around while loading |
| 39 | +- Pick the right **image format** for photos, graphics, and icons |
| 40 | +- Serve the right size to every screen with **responsive images**, `srcset`, and `sizes` |
| 41 | +- Choose formats and crops with the **`<picture>` element** |
| 42 | +- Speed up your pages with **lazy loading** |
| 43 | +- Add captions with **`<figure>` and `<figcaption>`** |
| 44 | +- Make images **accessible** to people who cannot see them |
| 45 | + |
| 46 | +:::info Prerequisites |
| 47 | +You should know how to write basic HTML elements and how file paths work. If you need a refresher, read [Relative URLs](/tutorials/html/links-and-navigation/relative-urls) first. |
| 48 | +::: |
| 49 | + |
| 50 | +## Your First Image |
| 51 | + |
| 52 | +An image takes only one element. Here is the smallest useful version. |
| 53 | + |
| 54 | +<Tabs> |
| 55 | +<TabItem value="code" label="HTML Code" default> |
| 56 | + |
| 57 | +```html title="index.html" |
| 58 | +<img |
| 59 | + src="images/harbor.jpg" |
| 60 | + alt="Fishing boats resting in a calm harbor at sunrise" |
| 61 | + width="300" |
| 62 | + height="160" |
| 63 | +/> |
| 64 | +``` |
| 65 | + |
| 66 | +</TabItem> |
| 67 | +<TabItem value="output" label="Browser Output"> |
| 68 | + |
| 69 | +<BrowserWindow url="http://127.0.0.1:5500/index.html"> |
| 70 | +<> |
| 71 | +<img |
| 72 | + src={"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 160'><rect width='300' height='160' fill='%23bfdbfe'/><circle cx='235' cy='48' r='22' fill='%23fbbf24'/><path d='M0 110 Q40 95 80 110 T160 110 T240 110 T300 110 V160 H0 Z' fill='%232563eb'/><path d='M0 130 Q40 118 80 130 T160 130 T240 130 T300 130 V160 H0 Z' fill='%231d4ed8'/><path d='M120 100 V55 L160 100 Z' fill='%23ffffff'/><rect x='112' y='100' width='56' height='8' rx='3' fill='%23334155'/></svg>"} |
| 73 | + alt="A small sailboat on blue waves under a bright sun" |
| 74 | + width="300" |
| 75 | + height="160" |
| 76 | +/> |
| 77 | +</> |
| 78 | +</BrowserWindow> |
| 79 | + |
| 80 | +</TabItem> |
| 81 | +</Tabs> |
| 82 | + |
| 83 | +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`. |
| 84 | + |
| 85 | +## The Anatomy of an Image |
| 86 | + |
| 87 | +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. |
| 88 | + |
| 89 | +```html |
| 90 | +<img src="images/harbor.jpg" alt="Fishing boats at sunrise" width="300" height="160" /> |
| 91 | +``` |
| 92 | + |
| 93 | +| Attribute | What it does | Required? | |
| 94 | +| :-- | :-- | :-- | |
| 95 | +| `src` | The **address** of the image file | Yes | |
| 96 | +| `alt` | A **text alternative** for people and tools that cannot see the image | Yes, always | |
| 97 | +| `width` and `height` | The image's **size in pixels**, so the browser can reserve space | Strongly recommended | |
| 98 | +| `loading` | Whether to load the image right away or later (`lazy`) | Optional | |
| 99 | +| `srcset` and `sizes` | Several versions of the image for different screens | Optional | |
| 100 | + |
| 101 | +## Three Questions to Ask About Every Image |
| 102 | + |
| 103 | +Before you add an image, ask yourself these three questions. They guide almost every decision in this section. |
| 104 | + |
| 105 | +| Question | Why it matters | Where you will learn it | |
| 106 | +| :-- | :-- | :-- | |
| 107 | +| **Does the image mean something?** | Meaningful images need helpful `alt` text. Decorative ones need an empty `alt` | [Src and Alt](./images/src-and-alt), [Accessible Images](./images/accessible-images) | |
| 108 | +| **How big will it be on screen?** | A huge file on a small phone wastes data and time | [Responsive Images](./images/responsive-images), [Srcset and Sizes](./images/srcset-and-sizes) | |
| 109 | +| **What is the best file type?** | A photo, a logo, and a chart each have a best format | [Image Formats](./images/image-formats) | |
| 110 | + |
| 111 | +## Choosing a Format at a Glance |
| 112 | + |
| 113 | +You will study formats in depth later. This quick map gets you started. |
| 114 | + |
| 115 | +| Your image is... | Good choice | Why | |
| 116 | +| :-- | :-- | :-- | |
| 117 | +| A photograph | JPEG, WebP, or AVIF | Small files for rich colors | |
| 118 | +| A logo, icon, or simple illustration | SVG | Sharp at any size, and tiny | |
| 119 | +| A screenshot or image with sharp text | PNG or WebP | Keeps edges crisp | |
| 120 | +| A short animation | Animated WebP or video | Much smaller than a GIF | |
| 121 | + |
| 122 | +## Images and Page Speed |
| 123 | + |
| 124 | +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. |
| 125 | + |
| 126 | +1. **Resize before you upload.** Do not publish a 6000-pixel photo to show it at 600 pixels. |
| 127 | +2. **Use modern formats** like WebP and AVIF when they fit. |
| 128 | +3. **Give the browser sizes** with `width` and `height`, so the page does not jump. |
| 129 | +4. **Offer several versions** with `srcset`, so phones download smaller files. |
| 130 | +5. **Load later what is not visible yet** with `loading="lazy"`. |
| 131 | + |
| 132 | +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. |
| 133 | + |
| 134 | +## Images and Accessibility |
| 135 | + |
| 136 | +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. |
| 137 | + |
| 138 | +<Tabs> |
| 139 | +<TabItem value="bad" label="Not helpful" default> |
| 140 | + |
| 141 | +```html |
| 142 | +<img src="chart.png" alt="chart" /> |
| 143 | +<img src="photo1.jpg" alt="IMG_20261003_0457.jpg" /> |
| 144 | +<img src="team.jpg" /> |
| 145 | +``` |
| 146 | + |
| 147 | +</TabItem> |
| 148 | +<TabItem value="good" label="Helpful"> |
| 149 | + |
| 150 | +```html |
| 151 | +<img src="chart.png" alt="Bar chart: course sign-ups doubled between March and June" /> |
| 152 | +<img src="photo1.jpg" alt="Students building a website together in a classroom" /> |
| 153 | +<img src="divider.svg" alt="" /> |
| 154 | +``` |
| 155 | + |
| 156 | +The last image is purely decorative, so its `alt` is intentionally empty. |
| 157 | + |
| 158 | +</TabItem> |
| 159 | +</Tabs> |
| 160 | + |
| 161 | +:::tip The one-sentence test |
| 162 | +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. |
| 163 | +::: |
| 164 | + |
| 165 | +## Your Learning Path |
| 166 | + |
| 167 | +Follow the lessons in order. Each one builds on the one before. |
| 168 | + |
| 169 | +<DocCardList /> |
| 170 | + |
| 171 | +## Common Beginner Mistakes |
| 172 | + |
| 173 | +Here are a few traps worth knowing about before you start. Each one has its own lesson later. |
| 174 | + |
| 175 | +| Mistake | What goes wrong | Where you will fix it | |
| 176 | +| :-- | :-- | :-- | |
| 177 | +| Leaving out `alt`, or writing "image" | Screen reader users get nothing, or noise | [Src and Alt](./images/src-and-alt) | |
| 178 | +| A wrong file path | The image shows as a broken icon | [Src and Alt](./images/src-and-alt) | |
| 179 | +| No `width` and `height` | The text jumps down when the image finally loads | [Width and Height](./images/width-and-height) | |
| 180 | +| Using a huge photo for a small space | Slow pages and wasted data | [Responsive Images](./images/responsive-images) | |
| 181 | +| Using PNG for every photo | Files several times bigger than needed | [Image Formats](./images/image-formats) | |
| 182 | +| Loading every image at once | The page feels slow, even for images far below the fold | [Lazy Loading](./images/lazy-loading) | |
| 183 | +| Putting important text inside an image | It cannot be searched, translated, or read aloud | [Accessible Images](./images/accessible-images) | |
| 184 | +| Using someone else's picture without permission | Copyright trouble | See the tip below | |
| 185 | + |
| 186 | +:::warning Use images you have the right to use |
| 187 | +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. |
| 188 | +::: |
| 189 | + |
| 190 | +## Images for a Global Audience |
| 191 | + |
| 192 | +Your visitors come from many countries and cultures. A few thoughtful habits help. |
| 193 | + |
| 194 | +- **Write `alt` text in the language of the page,** and set the `lang` attribute. |
| 195 | +- **Avoid text inside images.** It cannot be translated by browsers or read aloud. Put the words in HTML instead. |
| 196 | +- **Choose pictures that many people can relate to.** Gestures, symbols, and clothing can mean different things in different places. |
| 197 | +- **Think about data costs.** Smaller files are kinder to visitors on limited plans. |
| 198 | +- **Show diverse people** when your pictures include people. |
| 199 | + |
| 200 | +<AdsComponent /> |
| 201 | +<br /> |
| 202 | + |
| 203 | +## Quick Recap |
| 204 | + |
| 205 | +Test yourself. Click a question to reveal the answer. |
| 206 | + |
| 207 | +<details> |
| 208 | +<summary>1. Which element adds an image, and does it need a closing tag?</summary> |
| 209 | + |
| 210 | +The `<img>` element. It is a void element, so it has no closing tag. |
| 211 | + |
| 212 | +</details> |
| 213 | + |
| 214 | +<details> |
| 215 | +<summary>2. Which two attributes does every image need?</summary> |
| 216 | + |
| 217 | +`src`, the address of the file, and `alt`, a text alternative for people and tools that cannot see the image. |
| 218 | + |
| 219 | +</details> |
| 220 | + |
| 221 | +<details> |
| 222 | +<summary>3. Why should you set `width` and `height`?</summary> |
| 223 | + |
| 224 | +They let the browser reserve space before the image loads, so the page does not jump. |
| 225 | + |
| 226 | +</details> |
| 227 | + |
| 228 | +<details> |
| 229 | +<summary>4. What should the `alt` be for a purely decorative image?</summary> |
| 230 | + |
| 231 | +An empty value, `alt=""`, so screen readers skip it. |
| 232 | + |
| 233 | +</details> |
| 234 | + |
| 235 | +<details> |
| 236 | +<summary>5. Name two ways to make images load faster.</summary> |
| 237 | + |
| 238 | +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. |
| 239 | + |
| 240 | +</details> |
| 241 | + |
| 242 | +## Key Takeaways |
| 243 | + |
| 244 | +- Add images with the `<img>` element, which needs `src` and `alt`. |
| 245 | +- Write `alt` text that says what the image means. Use `alt=""` for decoration only. |
| 246 | +- Set `width` and `height` to keep your layout steady while images load. |
| 247 | +- Choose formats, sizes, and loading behavior with page speed and data costs in mind. |
| 248 | +- Make every image work for everyone: describe it, caption it when it helps, and keep important text out of pictures. |
| 249 | + |
| 250 | +Ready to begin? Start with [The img Element](./images/img-element). |
0 commit comments