Your Search Results

    <img>

    This article is in need of an editorial review.

    Summary

    The HTML <img> Element (or HTML Image Element) represents an image of 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), or if the user chooses not to display images, or if the browser is unable to 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 part of the interactive content category.
    • Permitted content None, this is a void 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

    This element includes the global attributes.

    align Deprecated since HTML4.01, Obsolete since HTML5 Use the vertical-align CSS property
    The alignment of the image with respect to its surrounding context.
    alt
    This attribute defines the alternative text describing the image. Users will see this displayed if the image URL is wrong, the image is not in one of the supported formats, or until the image is downloaded.

    Usage note: Omitting this attribute indicates that the image is a key part of the content, but no textual equivalent is available. Setting this attribute to the empty string indicates that this image is not a key part of the content; non-visual browsers may omit it from the rendering.

    border Deprecated since HTML4.01, Obsolete since HTML5
    The width of a border around the image.
    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:
    anonymous
    A cross-origin request (i.e. with Origin: HTTP header) is performed. But no credential is sent (i.e. no cookie, no X.509 certificate and no HTTP Basic authentication is sent). 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
    A cross-origin request (i.e. with Origin: HTTP header) is performed with credential is sent (i.e. a cookie, a certificate and HTTP Basic authentication is performed). If the server does not give credentials to the origin site (through Access-Control-Allow-Credentials: HTTP header), the image will be tainted and its usage restricted.
    When not present, the resource is fetched without a CORS request (i.e. without sending the Origin: HTTP header), preventing its non-tainted used 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 height of the image in HTML5 CSS pixels, or HTML 4 in pixels or as a percentage.
    hspace Deprecated since HTML4.01, Obsolete since HTML5
    The number of pixels of white space to insert to the left and right of the image.
    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.

    Usage note: This attribute is allowed only if the <img> element is a descendant of an <a> element with a valid href attribute.

    longdescHTML 4 only
    The URL of a description of the image to be displayed, which supplements the alt text. In HTML5, use a regular <a> element to link to the description.
    name Deprecated since HTML4.01, Obsolete since HTML5
    A name for the element. It is supported in HTML 4 only for backward compatibility. Use the id attribute instead.
    src
    Image URL, this attribute is obligatory for the <img> element.
    srcsetHTML5
    A list of one or more strings separated by commas indicating a set of possible images for the user agent to use. Each string is composed of:
    1. one URL to an image,
    2. a width descriptor (a positive integer directly followed by '). The default value, if missing, is the infinity.
    3. an pixel density descriptor (a positive floating number directly followed by '). The default value, if missing, is 1X.

    Each string in the list must have at least a width descriptor or a pixel density descriptor to be valid. Among the list, there must be only one string containing the same tuple of width descriptor and pixel density descriptor.
    The browser choose the most adequate image to display at a given point of time.

    width
    The width of the image in pixels or percent.
    usemap
    The partial URL (starting with '#') of an image map associated with the element.

    Usage note: You cannot use this attribute if the <img> element is a descendant of an <a> or <button> element.

    vspace Deprecated since HTML4.01, Obsolete since HTML5
    The number of pixels of white space to insert to above and below the image.

    Supported image formats

    The HTML standard doesn't give a list of image format that must be supported, so each user agent supports a different set of formats. Gecko supports:

    Note:
    Support for the XBM format has been removed in Gecko 1.9.2.

    Interaction with CSS

    Regarding CSS, an <img> is a replaced element. It has no baseline, meaning that, when used in a inline formatting context with vertical-align: baseline, the bottom of the image will be posed on the container's baseline.

    An image may have an intrinsic dimension, but not necessarily, depending of its type. A SVG image has no intrinsic dimension, a raster image has one.

    Example 1

    <img src="https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png" alt="MD Logo" />
    

    MD Logo

    Example 2: Image link 

    <a href="https://developer.mozilla.org/" target="_blank"> <img src="https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png" alt="MDN Logo" /> </a>
    

    MDN Logo

    Example3: Using srcset attribute

     <img src="https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png" alt="MD Logo" srcset="mdn-logo-HD.png 2x, mdn-logo-small.png 15w, mdn-banner-HD.png 100w 2x"> />

    Specifications

    Specification Status Comment
    WHATWG HTML Living Standard
    The definition of '<img>' in that specification.
    Living Standard  
    HTML5
    The definition of '<img>' in that specification.
    Candidate Recommendation  
    HTML 4.01 Specification
    The definition of '<img>' in that specification.
    Recommendation  

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
    Basic support (Yes) (Yes) (Yes) (Yes)
    srcset attribute 34.0 32.0 (32.0) (behind a pref) Not supported 21 8
    Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
    srcset attribute Not supported Not supported Not supported Not supported 8 (Yes)

    See also