The <img>
embeds an image into the document. It is a replaced element.
The above example shows very simple usage of the <img>
element. The src
attribute is required, and contains the path to the image you want to embed. The alt
attribute contains a textual description of the image, which isn't mandatory but is incredibly useful for accessibility — screenreaders read this description out to their users so they know what the image shows, and it is also displayed on the page if the image can't be loaded for some reason.
There are many other attributes that can be specified to achieve various purposes, for example:
crossorigin
and referrerpolicy
.width
and height
, which is useful when you want to set the space taken up by an image to ensure the page layout is stable before it loads.sizes
and srcset
(see also the <picture>
element, and our Responsive images tutorial).The HTML standard doesn't give a list of image formats that must be supported, so each user agent supports a different set of formats.
The image formats supported by Firefox are:
If an error occurs while trying to load or render an image, and an onerror
event handler has been configured to handle 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 the image referenced by the element. This is the case for non-graphical browsers (including those used by people with visual impairments), if the user chooses not to display images, or if the browser cannot display the image because it is invalid or an unsupported type. In these cases, the browser may replace the image with the text defined in this element's alt
attribute. You should, for these reasons and others, provide a useful value for alt
whenever possible.
Note: Omitting this attribute altogether indicates that the image is a key part of the content, and no textual equivalent is available. Setting this attribute to an empty string (alt=""
) indicates that this image is not a key part of the content (decorative), and that non-visual browsers may omit it from rendering.
crossorigin
<canvas>
element without being "tainted." The allowed values are:anonymous
Origin:
HTTP header) is performed, but no credential is sent (i.e., no cookie, X.509 certificate, or HTTP Basic authentication). If the server does not give credentials to the origin site (by not setting the Access-Control-Allow-Origin
HTTP header), the image will be tainted and its usage restricted.use-credentials
Origin
HTTP header) performed along with credentials sent (i.e., a cookie, certificate, or HTTP Basic authentication). If the server does not give credentials to the origin site (through the Access-Control-Allow-Credentials
HTTP header), the image will be tainted and its usage restricted.Origin
HTTP header), preventing its non-tainted usage in <canvas>
elements. If invalid, it is handled as if the anonymous
value was used. See CORS settings attributes for additional information.decoding
Provides an image decoding hint to the browser. The allowed values are:
sync
async
auto
height
importance
auto
: Indicates no preference. The browser may use its own heuristics to decide the priority of the image.
high
: Indicates to the browser that the image is of high priority.
low
: Indicates to the browser that the image is of low priority.
intrinsicsize
naturalWidth
/naturalHeight
on images would return the values specified in this attribute. Explainer, examples
ismap
referrerpolicy
no-referrer:
The Referer
header will not be sent.no-referrer-when-downgrade:
No Referer
header will be sent when navigating to an origin without TLS (HTTPS). This is a user agent’s default behavior if no policy is otherwise specified.origin:
The Referer
header will include the page of origin's scheme, the host, and the port.origin-when-cross-origin:
Navigating to other origins will limit the included referral data to the scheme, the host and the port, while navigating from the same origin will include the referrer's full path.unsafe-url:
The Referer
header will include the origin and the path, but not the fragment, password, or username. This case is unsafe because it can leak origins and paths from TLS-protected resources to insecure origins.sizes
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 (w
) descriptor, then the sizes
attribute has no effect.
src
<img>
element. On browsers supporting srcset
, src
is treated like a candidate image with a pixel density descriptor 1x
unless an image with this pixel density descriptor is already defined in srcset,
or unless srcset
contains 'w
' descriptors.srcset
w
'. The width descriptor is divided by the source size given in the sizes
attribute to calculate the effective pixel density.x
'.If no descriptor is specified, the source is assigned the default descriptor: 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.
The user agent selects any one 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
float
and/or vertical-align
CSS properties instead. You can also use the object-position
property to position the image within the element's box. The allowed values are:top
vertical-align: top;
or vertical-align: text-top;
middle
vertical-align: -moz-middle-with-baseline;
bottom
vertical-align: unset;
or vertical-align: initial;
left
float: left;
right
float: right;
border
border
CSS property instead.hspace
margin
CSS property instead.longdesc
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
id
attribute instead.vspace
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. You can set properties like border
/border-radius
, padding
/margin
, width
/height
, etc. on an image.
Often however it is a good idea to set images to display: block;
so that you have maximum control over the styling (e.g. margin: 0 auto
doesn't work on inline images, and it is easier to place images in context with surrounding elements when they are block-level).
<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 container's 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 not necessary. SVG images, for instance, may have no intrinsic dimensions if their root <svg>
element doesn't have a width
or height
set on it.
The following simple example embeds an image into the page, and includes alternative text to improve accessibility.
<img src="https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png" alt="MDN logo — a dinosaur logo with the text MDN">
This example builds upon the previous one, showing how to turn the image into a link. It is very simple to do so — you just nest the <img>
tag inside the <a>
. One consideration is that you should made the alternative text describe the resource the link is pointing to.
<a href="https://developer.mozilla.org"> <img src="https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png" alt="Visit the MDN site"> </a>
srcset
attributeIn this example we include a srcset
attribute containing 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="mdn-logo-sm.png" alt="MDN" srcset="mdn-logo-HD.png 2x">
srcset
and sizes
attributesThe src
attribute is ignored in user agents that support srcset
when 'w
' descriptors are included. When the (max-width: 600px)
media condition matches, the 200px wide image will be loaded (it is the one that matches 200px most closely), otherwise the other image will be loaded.
<img src="clock-demo-thumb-200.png" alt="Clock" srcset="clock-demo-thumb-200.png 200w, clock-demo-thumb-400.png 400w" sizes="(max-width: 600px) 200px, 50vw">
Although <img>
elements have many 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 for presenting the content 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.
title
attributeThe title
attribute is not an acceptable substitute for an 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 a combination of the figure
and figcaption
elements.
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. |
Permitted ARIA roles | Any |
DOM interface | HTMLImageElement |
Specification | Status | Comment |
---|---|---|
Referrer Policy The definition of 'referrer attribute' in that specification. | Candidate Recommendation | Added the referrerpolicy attribute. |
HTML Living Standard The definition of '<img>' in that specification. | Living Standard | |
HTML5 The definition of '<img>' in that specification. | Recommendation | |
HTML 4.01 Specification The definition of '<img>' in that specification. | Recommendation |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
align
|
Yes | Yes | Yes | Yes | Yes | Yes |
alt |
Yes | Yes | Yes | Yes | Yes | Yes |
border
|
Yes | Yes | Yes | Yes | Yes | Yes |
crossorigin |
Yes | Yes | Yes | Yes | Yes | Yes |
decoding |
Yes | ? | 63 | No | Yes | No |
height |
Yes | Yes | Yes | Yes | Yes | Yes |
hspace
|
Yes | Yes | Yes | Yes | Yes | Yes |
ismap |
Yes | Yes | Yes | Yes | Yes | Yes |
longdesc |
Yes | Yes | Yes | Yes | Yes | Yes |
name
|
Yes | Yes | Yes | Yes | Yes | Yes |
onerror
|
Yes | ? | 51 | ? | Yes | Yes |
referrerpolicy |
51 | No | 50 | No | 38 | 11.1 |
sizes |
Yes | Yes | Yes | Yes | Yes | Yes |
src |
Yes | Yes | Yes | Yes | Yes | Yes |
srcset |
34 | Yes | 38
|
No | 21 | 8 |
usemap |
Yes | Yes | Yes | Yes | Yes | Yes |
vspace
|
Yes | Yes | Yes | Yes | Yes | Yes |
width |
Yes | Yes | Yes | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
align
|
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
alt |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
border
|
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
crossorigin |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
decoding |
Yes | Yes | ? | 63 | Yes | No | Yes |
height |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
hspace
|
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
ismap |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
longdesc |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
name
|
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
onerror
|
Yes | Yes | ? | 51 | Yes | Yes | Yes |
referrerpolicy |
51 | 51 | No | 50 | 38 | No | 7.2 |
sizes |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
src |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
srcset |
37 | 34 | Yes | 38
|
21 | 8 | Yes |
usemap |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
vspace
|
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
width |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
<picture>
, <object>
and <embed>
elementsobject-fit
, object-position
, image-orientation
, image-rendering
, and image-resolution
.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img