<img>: The Image Embed element

The HTML <img> element embeds an image into the document.

The above example shows usage of the <img> element:

  • The src attribute is required, and contains the path to the image you want to embed.
  • The 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:

Supported image formats

The HTML standard doesn't list what image formats to support, so each user agent supports different formats. A complete guide to image formats supported by web browsers is available. Below is a list of the image formats that are most commonly used on the web, as well as some older formats that should no longer be used, despite existing content possibly still using them.

Abbreviation File format MIME type File extension(s) Browser compatibility
APNG Animated Portable Network Graphics image/apng .apng Chrome, Edge, Firefox, Opera, Safari
BMP Bitmap file image/bmp .bmp Chrome, Edge, Firefox, Internet Explorer, Opera, Safari
GIF Graphics Interchange Format image/gif .gif Chrome, Edge, Firefox, Internet Explorer, Opera, Safari
ICO Microsoft Icon image/x-icon .ico, .cur Chrome, Edge, Firefox, Internet Explorer, Opera, Safari
JPEG Joint Photographic Expert Group image image/jpeg .jpg, .jpeg, .jfif, .pjpeg, .pjp Chrome, Edge, Firefox, Internet Explorer, Opera, Safari
PNG Portable Network Graphics image/png .png Chrome, Edge, Firefox, Internet Explorer, Opera, Safari
SVG Scalable Vector Graphics image/svg+xml .svg Chrome, Edge, Firefox, Internet Explorer, Opera, Safari
TIFF Tagged Image File Format image/tiff .tif, .tiff None built-in; add-ons required
WebP Web Picture format image/webp .webp Chrome, Edge, Firefox, Opera

The abbreviation for each format links to a longer description of the format, its capabilities, and detailed browser compatibility information; including which versions introduced support and specific special features that may have been introduced later.

Image loading errors

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:

  • The src attribute is empty ("") or null.
  • The src URL is the same as the URL of the page the user is currently on.
  • The image is corrupted in some way that prevents it from being loaded.
  • The image's metadata is corrupted in such a way that it's impossible to retrieve its dimensions, and no dimensions were specified in the <img> element's attributes.
  • The image is in a format not supported by the user agent.

Attributes

This element includes the global attributes.

alt
Defines an alternative text description of the image.

Note: Browsers do not always display images. There are a number of situations in which a browser might not display images, such as:

  • Non-visual browsers (such as those used by people with visual impairments)
  • The user chooses not to display images (saving bandwidth, privacy reasons)
  • The image is invalid or an unsupported type

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.

Omitting alt 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 (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

Indicates if the fetching of the image must be done using a CORS request. Image data from a CORS-enabled image returned from a CORS request can be reused in the <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 marks the image as tainted and restricts access to its image data, preventing its usage in <canvas> elements.

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.

If the attribute has an invalid value, browsers handle it as if the anonymous value was used. See CORS settings attributes for additional information.

decoding

Provides an image decoding hint to the browser. Allowed values:

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.
height
The intrinsic height of the image, in pixels. Must be an integer without a unit.
importance
Indicates the relative download importance of the resource. Priority hints allow the values:

auto: no preference. The browser may use its own heuristics to prioritize the image.

high: the image is of high priority.

low: the image is of low priority.

intrinsicsize
This attribute tells the browser to ignore the actual intrinsic size of the image and pretend it’s the size specified in the attribute. Specifically, the image would raster at these dimensions and naturalWidth/naturalHeight on images would return the values specified in this attribute. Explainer, examples
ismap
This Boolean attribute indicates that the image is part of a server-side map. If so, the coordinates where the user clicked on the image are sent to the server.

Note: This attribute is allowed only if the <img> element is a descendant of an <a> element with a valid href attribute. This gives users without pointing devices a fallback destination.

loading
Indicates how the browser should load the image:
  • 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
A string indicating which referrer to use when fetching the resource:
  • no-referrer: The Referer header will not be sent.
  • no-referrer-when-downgrade: No Referer header is sent when navigating to an origin without HTTPS. This is the default if no policy is otherwise specified.
  • origin: The Referer header will include the page's origin (scheme, host, and port).
  • origin-when-cross-origin: Navigating to other origins will limit the included referral data to the scheme, host, and port, while navigating from the same origin will include the full path and query string.
  • unsafe-url: The Referer header will always include the origin, path and query string, but not the fragment, password, or username. This is unsafe because it can leak information from TLS-protected resources to insecure origins.
sizes
One or more strings separated by commas, indicating a set of source sizes. Each source size consists of:
  1. A media condition. This must be omitted for the last item in the list.
  2. A source size value.

Media Conditions describe properties of the viewport, not of the image. For example, (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
The image URL. Mandatory for the <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
One or more strings separated by commas, indicating possible image sources for the user agent to use. Each string is composed of:
  1. A URL to an image
  2. Optionally, whitespace followed by one of:
    • A width descriptor (a positive integer directly followed by w). The width descriptor is divided by the source size given in the sizes attribute to calculate the effective pixel density.
    • A pixel density descriptor (a positive floating point number directly followed by x).

If no descriptor is specified, the source is assigned the default descriptor of 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 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
The intrinsic width of the image in pixels. Must be an integer without a unit.
usemap
The partial URL (starting with #) of an image map associated with the element.

Note: You cannot use this attribute if the <img> element is inside an <a> or <button> element.

Deprecated attributes

align
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
The width of a border around the image. Use the border CSS property instead.
hspace
The number of pixels of white space on the left and right of the image. Use the margin CSS property instead.
longdesc
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
A name for the element. Use the id attribute instead.
vspace
The number of pixels of white space above and below the image. Use the margin CSS property instead.

Styling with CSS

<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.

Examples

Alternative text

The following example embeds an image into the page and includes alternative text for accessibility.

<img src="https://udn.realityripple.com/samples/6c/98485e5d8a.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="https://udn.realityripple.com/samples/6c/98485e5d8a.png"
       alt="Visit the MDN site">
</a>

Using the srcset attribute

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="https://udn.realityripple.com/samples/61/869e27c81e.png"
      alt="MDN logo"
      srcset="/static/external/6c/6c98485e5d8acac9fecd7824ce30c9587f0d8548b946ee9fb1293d2eccdb6cf1.png 2x">

Using the srcset and sizes attributes

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="/files/16864/clock-demo-200px.png"
      alt="Clock"
      srcset="/files/16864/clock-demo-200px.png 200w,
          /files/16797/clock-demo-400px.png 400w"
      sizes="(max-width: 600px) 200px, 50vw">

To see the resizing in action, view the example on a separate page, so you can actually resize the content area.

Security and privacy concerns

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.

Accessibility concerns

Authoring meaningful alternate descriptions

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.

Don't

<img alt="image" src="penguin.jpg">

Do

<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.

The title attribute

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.

Technical summary

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

Specifications

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

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
imgChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
align
Deprecated
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
altChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
border
Deprecated
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
crossoriginChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
decodingChrome Full support YesEdge Full support ≀79Firefox Full support 63IE No support NoOpera Full support YesSafari Full support 11.1WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 63Opera Android Full support YesSafari iOS Full support 11.3Samsung Internet Android Full support Yes
heightChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
hspace
Deprecated
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
intrinsicsize
ExperimentalNon-standard
Chrome Full support 71
Disabled
Full support 71
Disabled
Disabled From version 71: this feature is behind the #enable-experimental-productivity-features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support ≀79
Disabled
Full support ≀79
Disabled
Disabled From version ≀79: this feature is behind the #enable-experimental-productivity-features preference.
Firefox .html? IE No support NoOpera Full support 58Safari No support NoWebView Android No support NoChrome Android Full support 71
Disabled
Full support 71
Disabled
Disabled From version 71: this feature is behind the #enable-experimental-productivity-features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android ? Opera Android Full support 50Safari iOS No support NoSamsung Internet Android No support No
ismapChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
loading
Experimental
Chrome Full support 76Edge Full support 79Firefox Full support 75IE No support NoOpera Full support 63Safari No support No
Notes
No support No
Notes
Notes See bug 196698
WebView Android No support NoChrome Android Full support 76Firefox Android No support No
Notes
No support No
Notes
Notes See bug 1542784
Opera Android Full support 54Safari iOS No support No
Notes
No support No
Notes
Notes See bug 196698
Samsung Internet Android No support No
longdescChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
name
Deprecated
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
onerror
Deprecated
Chrome Full support YesEdge Full support ≀79Firefox Full support 51IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 51Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
referrerpolicyChrome Full support 51Edge Full support 79Firefox Full support 50IE No support NoOpera Full support 38Safari Full support 11.1WebView Android Full support 51Chrome Android Full support 51Firefox Android Full support 50Opera Android Full support 41Safari iOS No support NoSamsung Internet Android Full support 7.2
sizesChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
srcChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
srcsetChrome Full support 34Edge Full support ≀18Firefox Full support 38
Full support 38
No support 32 — 52
Disabled
Disabled From version 32 until version 52 (exclusive): this feature is behind the dom.image.srcset.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 21Safari Full support 8WebView Android Full support 37Chrome Android Full support 34Firefox Android Full support 38
Full support 38
No support 32 — 52
Disabled
Disabled From version 32 until version 52 (exclusive): this feature is behind the dom.image.srcset.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 21Safari iOS Full support 8Samsung Internet Android Full support 2.0
usemapChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
vspace
Deprecated
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
widthChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also