HTML - <img>
The HTML <img>
element represents an image in the document.
Examples
Example 1: Alternative text
<img src="mdn-logo-sm.png" alt="MDN">
Example 2: Image link
<a href="https://developer.mozilla.org/">MDN<img src="mdn-logo-sm.png" alt="MDN"></a>
Example 3: Using the srcset attribute
The src
attribute is a 1x
candidate in user agents that support srcset
.
<img src="mdn-logo-sm.png" alt="MDN" srcset="mdn-logo-HD.png 2x">
Example 4: Using the srcset and sizes attributes
The src
attribute is ignored in user agents that support srcset
when using 'w
' descriptors. When the (min-width: 600px)
media condition matches, the image will be 200px wide, otherwise it will be 50vw wide (50% of the viewport width).
<img src="clock-demo-thumb-200.png" alt="Clock" srcset="clock-demo-thumb-200.png 200w, clock-demo-thumb-400.png 400w" sizes="(min-width: 600px) 200px, 50vw">
Description
The HTML <img>
element represents an image in the document.
Usage 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 vision 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.
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 parent elements | Any element that accepts embedded content. |
DOM interface | HTMLImageElement |
Attributes
Includes the Global Attributes.
Name | Version | Description |
---|---|---|
align |
The alignment of the image with respect to its surrounding context. In HTML5, use the float and/or vertical-align CSS properties instead. |
|
alt |
This attribute defines the alternative text describing the image. Users will see this text displayed if the image URL is wrong, the image is not in one of the supported formats, or if the image is not yet downloaded.
Usage 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 ( |
|
border |
The width of a border around the image. In HTML5, use the border CSS property instead. |
|
crossorigin |
HTML5 | This enumerated attribute indicates if the fetching of the related image must be done using CORS or not. CORS-enabled images can be reused in the <canvas> element without being "tainted." The allowed values are:
Origin: HTTP header), preventing its non-tainted usage in <canvas> elements. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes for additional information. |
height |
The intrinsic height of the image in pixels. In HTML 4, the height could be defined pixels or as a percentage. In HTML5, however, the value must be in pixels. | |
hspace |
The number of pixels of white space to insert to the left and right of the image. In HTML5, use the margin CSS property instead. |
|
ismap |
This Boolean attribute indicates that the image is part of a server-side map. If so, the precise coordinates of a click are sent to the server. | |
longdesc |
A link to a more detailed description of the image. Possible values are a URL or an element id. | |
name |
A name for the element. It is supported in HTML 4 only for backward compatibility. Use the id attribute instead. |
|
referrerpolicy |
A string indicating which referrer to use when fetching the resource:
|
|
sizes |
HTML5 | A list of one or more strings separated by commas indicating a set of source sizes. Each source size consists of:
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 |
src |
The image URL. This attribute is 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 |
HTML5 | A list of one or more strings separated by commas indicating a set of possible image sources for the user agent to use. Each string is composed of:
If no descriptor is specified, the source is assigned the default descriptor: It is incorrect to mix width descriptors and pixel density descriptors in the same 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. |
width |
The intrinsic width of the image in pixels. In HTML 4, either a percentage or pixels were acceptable values. In HTML5, however, only pixels are acceptable. | |
usemap |
The partial URL (starting with '#') of an image map associated with the element. | |
vspace |
The number of pixels of white space to insert above and below the image. In HTML5, use the margin CSS property instead. |
Browser Compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
srcset |
34.0 | (Yes) | 32.0 (32.0)[2] | No support | 21 | 7.1 |
referrerpolicy |
46.0[1] | ? | 42.0 (42.0)[3] | ? | ? | ? |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
srcset |
? | 34.0 | No support | No support | 21 | iOS 8 | 34.0 |
referrerpolicy |
? | 46.0 [1] | 42.0 (42.0)[3] | ? | ? | ? | 46.0[1] |
[1] Implemented as referrerpolicy
and behind a flag.
[2] Implemented behind a preference.
[3] Behind the network.http.enablePerElementReferrer
preference, that default to false
. From Firefox 42 to Firefox 44, the attribute was called referrer
.
See Also
Specifications
Specification | Status | Comment |
---|---|---|
Referrer Policy The definition of 'referrer attribute' in that specification. |
Working Draft | Added the referrerpolicy attribute. |
WHATWG 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 |
Supported image formats
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. Gecko supports:
Interaction with CSS
Regarding CSS, <img>
is a replaced element. It 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.
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, have no intrinsic dimensions.
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/html/element/img