The <img>
HTML element embeds an image into the document.
The <img>
HTML element embeds an image into the document.
The above example shows usage of the <img>
element:
src
attribute is required, and contains the path to the image you want to embed.alt
attribute holds a text description of the image, which isn't mandatory but is incredibly useful for accessibility — screen readers read this description out to their users so they know what the image means. Alt text is also displayed on the page if the image can't be loaded for some reason: for example, network errors, content blocking, or linkrot.There are many other attributes to achieve various purposes:
crossorigin
and referrerpolicy
.width
and height
to set the intrinsic size of the image, allowing it to take up space before it loads, to mitigate content layout shifts.sizes
and srcset
(see also the <picture>
element and our Responsive images tutorial).The HTML standard doesn't list what image formats to support, so user agents may support different formats.
Note: The Image file type and format guide provides comprehensive information about image formats and their web browser support. This section is just a summary!
The image file formats that are most commonly used on the web are:
Formats like WebP and AVIF are recommended as they perform much better than PNG, JPEG, GIF for both still and animated images. WebP is widely supported while AVIF lacks support in Safari.
SVG remains the recommended format for images that must be drawn accurately at different sizes.
If an error occurs while loading or rendering an image, and an onerror
event handler has been set on the error
event, that event handler will get called. This can happen in a number of situations, including:
src
attribute is empty (""
) or null
.src
URL is the same as the URL of the page the user is currently on.<img>
element's attributes.This element includes the global attributes.
alt
Note: Browsers do not always display images. There are a number of situations in which a browser might not display images, such as:
In these cases, the browser may replace the image with the text in the element's alt
attribute. For these reasons and others, provide a useful value for alt
whenever possible.
alt=""
) indicates that this image is not a key part of the content (it's decoration or a tracking pixel), and that non-visual browsers may omit it from rendering. Visual browsers will also hide the broken image icon if the alt
is empty and the image failed to display. This attribute is also used when copying and pasting the image to text, or saving a linked image to a bookmark. crossorigin
<canvas>
element without being marked "tainted". If the crossorigin
attribute is not specified, then a non-CORS request is sent (without the Origin
request header), and the browser marks the image as tainted and restricts access to its image data, preventing its usage in <canvas>
elements. If the crossorigin
attribute is specified, then a CORS request is sent (with the Origin
request header); but if the server does not opt into allowing cross-origin access to the image data by the origin site (by not sending any Access-Control-Allow-Origin
response header, or by not including the site's origin in any Access-Control-Allow-Origin
response header it does send), then the browser blocks the image from loading, and logs a CORS error to the devtools console. Allowed values: anonymous
A CORS request is sent with credentials omitted (that is, no cookies, X.509 certificates, or Authorization
request header).
use-credentials
The CORS request is sent with any credentials included (that is, cookies, X.509 certificates, and the Authorization
request header). If the server does not opt into sharing credentials with the origin site (by sending back the Access-Control-Allow-Credentials: true
response header), then the browser marks the image as tainted and restricts access to its image data.
anonymous
value was used. See CORS settings attributes for additional information. decoding
sync
Decode the image synchronously, for atomic presentation with other content.
async
Decode the image asynchronously, to reduce delay in presenting other content.
auto
Default: no preference for the decoding mode. The browser decides what is best for the user.
fetchpriority
Experimental high
Signals a high-priority fetch relative to other images.
low
Signals a low-priority fetch relative to other images.
auto
Default: Signals automatic determination of fetch priority relative to other images.
height
ismap
loading
eager
: Loads the image immediately, regardless of whether or not the image is currently within the visible viewport (this is the default value).lazy
: Defers loading the image until it reaches a calculated distance from the viewport, as defined by the browser. The intent is to avoid the network and storage bandwidth needed to handle the image until it's reasonably certain that it will be needed. This generally improves the performance of the content in most typical use cases. Note: Loading is only deferred when JavaScript is enabled. This is an anti-tracking measure, because if a user agent supported lazy loading when scripting is disabled, it would still be possible for a site to track a user's approximate scroll position throughout a session, by strategically placing images in a page's markup such that a server can track how many images are requested and when.
referrerpolicy
no-referrer
: The Referer
header will not be sent.no-referrer-when-downgrade
: The Referer
header will not be sent to origins without TLS (HTTPS).origin
: The sent referrer will be limited to the origin of the referring page: its scheme, host, and port.origin-when-cross-origin
: The referrer sent to other origins will be limited to the scheme, the host, and the port. Navigations on the same origin will still include the path.same-origin
: A referrer will be sent for same origin, but cross-origin requests will contain no referrer information.strict-origin
: Only send the origin of the document as the referrer when the protocol security level stays the same (HTTPS→HTTPS), but don't send it to a less secure destination (HTTPS→HTTP).strict-origin-when-cross-origin
(default): Send a full URL when performing a same-origin request, only send the origin when the protocol security level stays the same (HTTPS→HTTPS), and send no header to a less secure destination (HTTPS→HTTP).unsafe-url
: The referrer will include the origin and the path (but not the fragment, password, or username). This value is unsafe, because it leaks origins and paths from TLS-protected resources to insecure origins.sizes
(max-height: 500px) 1000px
proposes to use a source of 1000px width, if the viewport is not higher than 500px. Source size values specify the intended display size of the image. User agents use the current source size to select one of the sources supplied by the srcset
attribute, when those sources are described using width (w
) descriptors. The selected source size affects the intrinsic size of the image (the image's display size if no CSS styling is applied). If the srcset
attribute is absent, or contains no values with a width descriptor, then the sizes
attribute has no effect. src
srcset
w
). The width descriptor is divided by the source size given in the sizes
attribute to calculate the effective pixel density.x
).1x
. It is incorrect to mix width descriptors and pixel density descriptors in the same srcset
attribute. Duplicate descriptors (for instance, two sources in the same srcset
which are both described with 2x
) are also invalid. If the srcset
attribute uses width descriptors, the sizes
attribute must also be present, or the srcset
itself will be ignored. The user agent selects any of the available sources at its discretion. This provides them with significant leeway to tailor their selection based on things like user preferences or bandwidth conditions. See our Responsive images tutorial for an example. width
usemap
align
Deprecated
Aligns the image with its surrounding context. Use the float
and/or vertical-align
CSS properties instead of this attribute. Allowed values:
top
Equivalent to vertical-align: top
or vertical-align: text-top
middle
Equivalent to vertical-align: -moz-middle-with-baseline
bottom
The default, equivalent to vertical-align: unset
or vertical-align: initial
left
Equivalent to float: left
right
Equivalent to float: right
border
Deprecated
The width of a border around the image. Use the border
CSS property instead.
hspace
Deprecated
The number of pixels of white space on the left and right of the image. Use the margin
CSS property instead.
longdesc
Deprecated
A link to a more detailed description of the image. Possible values are a URL or an element id
.
Note: This attribute is mentioned in the latest W3C version, HTML 5.2, but has been removed from the WHATWG's HTML Living Standard. It has an uncertain future; authors should use a WAI-ARIA alternative such as aria-describedby
or aria-details
.
name
Deprecated
A name for the element. Use the id
attribute instead.
vspace
Deprecated
The number of pixels of white space above and below the image. Use the margin
CSS property instead.
<img>
is a replaced element; it has a display
value of inline
by default, but its default dimensions are defined by the embedded image's intrinsic values, like it were inline-block
. You can set properties like border
/border-radius
, padding
/margin
, width
, height
, etc. on an image.
<img>
has no baseline, so when images are used in an inline formatting context with vertical-align
: baseline
, the bottom of the image will be placed on the text baseline.
You can use the object-position
property to position the image within the element's box, and the object-fit
property to adjust the sizing of the image within the box (for example, whether the image should fit the box or fill it even if clipping is required).
Depending on its type, an image may have an intrinsic width and height. For some image types, however, intrinsic dimensions are unnecessary. SVG images, for instance, have no intrinsic dimensions if their root <svg>
element doesn't have a width
or height
set on it.
The following example embeds an image into the page and includes alternative text for accessibility.
<img src="favicon144.png"
alt="MDN logo">
This example builds upon the previous one, showing how to turn the image into a link. To do so, nest the <img>
tag inside the <a>
. You should made the alternative text describe the resource the link is pointing to, as if you were using a text link instead.
<a href="https://developer.mozilla.org"> <img src="favicon144.png" alt="Visit the MDN site"> </a>
In this example we include a srcset
attribute with a reference to a high-resolution version of the logo; this will be loaded instead of the src
image on high-resolution devices. The image referenced in the src
attribute is counted as a 1x
candidate in user agents that support srcset
.
<img src="favicon72.png"
alt="MDN logo"
srcset="favicon144.png 2x">
The src
attribute is ignored in user agents that support srcset
when w
descriptors are included. When the (max-width: 600px)
media condition matches, the 200 pixel-wide image will load (it is the one that matches 200px
most closely), otherwise the other image will load.
<img src="clock-demo-200px.png"
alt="Clock"
srcset="clock-demo-200px.png 200w,
clock-demo-400px.png 400w"
sizes="(max-width: 600px) 200px, 50vw">
Note: To see the resizing in action, view the example on a separate page, so you can actually resize the content area.
Although <img>
elements have innocent uses, they can have undesirable consequences for user security and privacy. See Referer header: privacy and security concerns for more information and mitigations.
An alt
attribute's value should clearly and concisely describe the image's content. It should not describe the presence of the image itself or the file name of the image. If the alt
attribute is purposefully left off because the image has no textual equivalent, consider alternate methods to present what the image is trying to communicate.
<img alt="image" src="penguin.jpg">
<img alt="A Rockhopper Penguin standing on a beach." src="penguin.jpg">
When an alt
attribute is not present on an image, some screen readers may announce the image's file name instead. This can be a confusing experience if the file name isn't representative of the image's contents.
Due to a VoiceOver bug, VoiceOver does not correctly announce SVG images images. Include role="img"
to all <img>
elements with SVG source files to ensure assistive technologies correctly announce the SVG as image content.
<img src="mdn.svg" alt="MDN logo" role="img">
The title
attribute is not an acceptable substitute for the alt
attribute. Additionally, avoid duplicating the alt
attribute's value in a title
attribute declared on the same image. Doing so may cause some screen readers to announce the description twice, creating a confusing experience.
The title
attribute should also not be used as supplemental captioning information to accompany an image's alt
description. If an image needs a caption, use the figure
and figcaption
elements.
The value of the title
attribute is usually presented to the user as a tooltip, which appears shortly after the cursor stops moving over the image. While this can provide additional information to the user, you should not assume that the user will ever see it: the user may only have keyboard or touchscreen. If you have information that's particularly important or valuable for the user, present it inline using one of the methods mentioned above instead of using title
.
Content categories | Flow content, phrasing content, embedded content, palpable content. If the element has a usemap attribute, it also is a part of the interactive content category. |
---|---|
Permitted content | None, it is an empty element. |
Tag omission | Must have a start tag and must not have an end tag. |
Permitted parents | Any element that accepts embedded content. |
Implicit ARIA role |
|
Permitted ARIA roles |
|
DOM interface | HTMLImageElement |
Specification |
---|
HTML Standard # the-img-element |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
img |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
align |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
alt |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
aspect_ratio_computed_from_attributes |
79 |
79 |
71 |
No |
66 |
15
14-15
Safari doesn't preserve space for images without a valid
src , which may disrupt layouts that rely on lazy loading (see bug 224197). |
79 |
79 |
79 |
57 |
15
14-15
Safari doesn't preserve space for images without a valid
src , which may disrupt layouts that rely on lazy loading (see bug 224197). |
12.0 |
border |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
crossorigin |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
decoding |
Yes |
≤79 |
63 |
No |
Yes |
11.1 |
Yes |
Yes |
63 |
Yes |
11.3 |
Yes |
fetchpriority |
101 |
101 |
No |
No |
No |
No |
101 |
101 |
No |
70 |
No |
No |
height |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
hspace |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
ismap |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
loading |
77 |
79 |
75 |
No |
64 |
15.4 |
77 |
77 |
79 |
55 |
15.4 |
12.0 |
longdesc |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
name |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
onerror |
Yes |
≤79 |
51 |
No |
Yes |
Yes |
Yes |
Yes |
51 |
Yes |
Yes |
Yes |
referrerpolicy |
51 |
79 |
50 |
No |
38 |
14 |
51 |
51 |
50 |
41 |
14 |
7.2 |
sizes |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
src |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
srcset |
34 |
≤18 |
38 |
No |
21 |
8 |
37 |
34 |
38 |
21 |
8 |
2.0 |
usemap |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
vspace |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
width |
Yes |
12 |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
<picture>
, <object>
and <embed>
elementsobject-fit
, object-position
, image-orientation
, image-rendering
, and image-resolution
.
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img