HTML Image Tag: img Attributes, srcset and picture
November 11, 2026 · 11 min read
HTML Image Tag: How to Use img and picture Correctly
The HTML image tag is <img>, a void element that embeds a picture from the URL in src and describes it with alt text. Add width and height to stop layout shift, loading="lazy" for images below the fold, and srcset or <picture> when one file can't suit every screen.
How Do You Add an Image in HTML?
You add an image with a single <img> element, the standard HTML tag for images. It has no closing tag, because it's a void element: it can't hold content. Two attributes do the essential work. src says where the file lives, and alt says what it shows.
<img src="images/grapefruit.jpg" alt="Half a grapefruit, cut side up">
The browser fetches images/grapefruit.jpg and draws it inline, at its natural size, where the element sits in the text. If the file can't load, the browser shows the alt text instead.
The src value can take three forms.
- An absolute URL, such as
https://example.com/photos/cat.jpg, for files hosted anywhere. - A relative path, such as
images/cat.jpg(a folder next to the page) or../assets/cat.jpg(one folder up). This is how you insert an image in HTML from a folder in your project. Our website folder structure guide shows where that folder belongs. - A data URI, such as
data:image/png;base64,..., which embeds the file bytes inside the page itself.
MDN lists JPEG, PNG, GIF, WebP, AVIF, SVG and APNG among the most common web formats. It advises against BMP and ICO for page content. You may also see <img ... /> with a trailing slash. In HTML that slash is optional and does nothing, so both forms are valid. The same goes for every void element in our HTML tag list.
HTML Image Tag Attributes You Actually Need
The img tag in HTML accepts more than a dozen attributes, but a handful cover nearly every real page. The full list lives in the MDN img reference.
| Attribute | What it does | Example |
|---|---|---|
src | URL of the image file | src="cat.jpg" |
alt | Text that replaces the image for screen readers and failed loads | alt="Tabby cat asleep on a keyboard" |
width | Intrinsic width in pixels, a whole number with no unit | width="800" |
height | Intrinsic height in pixels, a whole number with no unit | height="533" |
loading | lazy defers off-screen images, eager is the default | loading="lazy" |
decoding | Decoding hint: sync, async or auto (default) | decoding="async" |
fetchpriority | Download priority relative to other images: high, low or auto | fetchpriority="high" |
srcset | Candidate files with w or x descriptors | srcset="cat-2x.jpg 2x" |
sizes | How wide the image displays, used with w descriptors | sizes="50vw" |
title | Tooltip text, never a replacement for alt | title="Photo: Ana Ruiz" |
crossorigin | Requests the file with CORS, for use in a canvas | crossorigin="anonymous" |
Older tutorials on the HTML img tag still show align, border, hspace and vspace. MDN marks all four as deprecated and points you to CSS instead: float or vertical-align, border, and margin.
Writing Alt Text That Works
Alt is an attribute, not an "alt tag," and MDN calls it mandatory. Write what the image contributes to the page, in the words you'd use if you read the page aloud. Skip "image of" or "picture of," because screen readers already announce that it's an image.
Decorative images get an empty value, alt="". That tells assistive technology to skip the image. It's not the same as leaving alt out. With no alt at all, some screen readers announce the file name instead, so a listener may hear something like IMG_4382.JPEG.
<img src="chart-q3.png" alt="Sign-ups rose from 1,200 in July to 3,400 in September">
<img src="divider-swirl.svg" alt="">
The first image carries information, so its alt states the data. The second is ornament, so it stays silent. When you're unsure which case you have, the W3C alt decision tree walks you through it. Writing a README instead of a web page? Markdown's own image syntax puts the alt text in brackets, as our Markdown image guide shows.
Why Set width and height on Every img?
Without width and height, the browser doesn't know how tall an image will be until the file arrives. Text below it renders first, then jumps down when the image lands. That jump is layout shift, measured as Cumulative Layout Shift (CLS) in Core Web Vitals.
With both attributes set, browsers compute an aspect ratio before the download starts and reserve the right amount of space. Per web.dev's CLS guide, you then keep the image responsive with two CSS rules.
<img src="team.jpg" alt="Five people around a whiteboard" width="1200" height="800">
<style>
img { max-width: 100%; height: auto; }
</style>
In a 400-pixel-wide column, the image shrinks to 400 by 267 pixels, and the space is held from the first paint. The height: auto rule matters. Without it, the fixed height attribute would stretch the scaled-down image.
We prefer writing both attributes on every image, even when CSS controls the final size. They cost two numbers and remove a whole class of layout bugs.
Edit the example below to try the image tag in HTML. The preview renders raw HTML, so the <img> element displays on the right.
One limitation to know: our editor can't host files, so the image must already be online and linked by its full URL. The Markdown to HTML converter uses the same editor and turns your text into a full HTML document in the browser.
Lazy Loading, decoding and fetchpriority
Three attributes control when and how the browser handles an image. Used together, they speed up the parts of the page readers see first.
loading="lazy"postpones the download until the image comes near the viewport. Use it on images further down the page. Never put it on the hero image, because that delays your Largest Contentful Paint (LCP). MDN notes that loading is deferred only when JavaScript is enabled.decoding="async"lets the browser draw the rest of the page before decoding the image. It's a hint, and browsers may ignore it.fetchpriority="high"moves an image up the download queue. Use it on the single most important image, usually the LCP one, and nowhere else.
<img src="hero.jpg" alt="Sunrise over the harbour" width="1600" height="900" fetchpriority="high">
<img src="map.png" alt="Walking route from the station" width="800" height="600" loading="lazy" decoding="async">
Support for fetchpriority reached Baseline in October 2024, so every current major browser handles it. Browsers that don't recognise it simply ignore the attribute, so adding it never breaks a page.
Responsive Images With srcset and sizes
One file rarely fits every screen. A 1600-pixel photo wastes data on a phone, and a 400-pixel one looks blurry on a large monitor. The srcset attribute lists several versions, and the browser picks the best match.
For images that change width with the layout, use w descriptors plus sizes.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="Red kayak on a calm lake"
width="800" height="533">
Each w number is that file's real pixel width. sizes tells the browser the image fills the full viewport on screens up to 600 pixels wide, and half of it above that. The browser does the arithmetic, accounts for screen density, and downloads one file. Leave out sizes and it defaults to 100vw.
For fixed-size images such as logos, x descriptors are simpler.
<img src="logo.png" srcset="logo@2x.png 2x" alt="Acme" width="120" height="40">
High-density screens get the 2x file, and everything else uses src. Don't mix w and x descriptors in one srcset. MDN's responsive images guide covers the edge cases.
The picture Tag in HTML: Art Direction and Format Fallbacks
srcset gives the browser a choice of sizes of the same image. The picture tag in HTML gives you, the author, control over which file appears. Use it for two jobs.
Art direction means showing a different crop on small screens, such as a tight square instead of a wide landscape.
<picture>
<source media="(max-width: 600px)" srcset="kayak-square.jpg" width="600" height="600">
<img src="kayak-wide.jpg" alt="Red kayak on a calm lake" width="1200" height="600">
</picture>
Format fallback serves a modern format to browsers that support it and a JPEG to the rest.
<picture>
<source srcset="kayak.avif" type="image/avif">
<source srcset="kayak.webp" type="image/webp">
<img src="kayak.jpg" alt="Red kayak on a calm lake" width="1200" height="800">
</picture>
The browser walks the <source> elements in order and uses the first whose media and type match. The <img> comes last and is required. It's also where alt, width, height and loading go, because <picture> only swaps the file.
The one exception is an art-direction crop with a different shape: give that <source> its own width and height, as above, so the browser reserves the right space. A common mistake is wrapping a lone <img> in <picture> with no <source> elements. That does nothing a plain image wouldn't.
How Do I Center an Image in HTML?
Use CSS, since the deprecated align attribute and the obsolete <center> element don't belong in new code. An image is inline by default, so the cleanest fix is to make it a block. Then let auto margins split the leftover space.
<img src="badge.png" alt="Certified badge" width="200" height="200" style="display: block; margin-inline: auto;">
Setting text-align: center on the parent element, or display: flex with justify-content: center, works too. Centering inside a GitHub README follows different rules, which our guide to centering in Markdown covers.
Two related patterns build on the plain image. A visible caption belongs in <figure> and <figcaption>, explained in our post on Markdown image captions. Wrapping an image in an <a> element makes it clickable, and its alt text should then describe where the link goes. The clickable image link guide covers that pattern, including new tabs and email.
Using the HTML image tag well comes down to a few habits. Always write alt and set width and height. Lazy-load what's below the fold, and reach for srcset or <picture> only when one file can't serve every screen. Paste an example into the editor with a full image URL in src, and the preview renders it beside your code. Then adjust the attributes until it behaves the way you want.