Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 926851 of <img>

  • Slug da revisão: Web/HTML/Element/img
  • Título da revisão: <img>
  • ID da revisão: 926851
  • Criado:
  • Criador: jorgeclesio
  • É a revisão atual? Não
  • Comentar

Conteúdo da revisão

Resumo

O Elementos HTML <img>  (or HTML Image Element) representa a inseração de imagem no documento, sedo implemente dado também pelo HTML5.

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 {{htmlattrxref("usemap", "img")}} attribute, it also is a part of the interactive content category.
  • Permitted content None, it is an {{Glossary("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 {{domxref("HTMLImageElement")}}

Atributos

This element includes the global attributes.

{{htmlattrdef("align")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}} Use the {{cssxref('vertical-align')}} CSS property
The alignment of the image with respect to its surrounding context.
{{htmlattrdef("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 rendering.

{{htmlattrdef("border")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
The width of a border around the image.
{{htmlattrdef("crossorigin")}} {{HTMLVersionInline(5)}}
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 {{HTMLElement("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) 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 usage in {{HTMLElement('canvas')}} elements. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes for additional information.
{{htmlattrdef("height")}}
The height of the image in {{HTMLVersionInline(5)}} CSS pixels, or {{HTMLVersionInline(4)}} in pixels or as a percentage.
{{htmlattrdef("hspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
The number of pixels of white space to insert to the left and right of the image.
{{htmlattrdef("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 {{htmlelement("a")}} element with a valid {{htmlattrxref("href","a")}} attribute.

{{htmlattrdef("longdesc")}}{{HTMLVersionInline(4)}} only
The URL of a description of the image to be displayed, which supplements the alt text. In {{HTMLVersionInline(5)}}, use a regular {{HTMLElement("a")}} element to link to the description.
{{htmlattrdef("name")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
A name for the element. It is supported in {{HTMLVersionInline(4)}} only for backward compatibility. Use the id attribute instead.
{{htmlattrdef("src")}}
Image URL, this attribute is obligatory for the <img> element. On browsers supporting srcset, src is ignored if this one is provided.
{{htmlattrdef("srcset")}}{{HTMLVersionInline(5)}}
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, that is a positive integer directly followed by 'w'. The default value, if missing, is the infinity.
  3. a pixel density descriptor, that is a positive floating number directly followed by 'x'. 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 chooses the most adequate image to display at a given point of time.

{{htmlattrdef("width")}}
The width of the image in pixels or percent.
{{htmlattrdef("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 {{htmlelement("a")}} or {{HTMLElement("button")}} element.

{{htmlattrdef("vspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
The number of pixels of white space to insert above and below the image.

Formato de imagens suportadas

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:

  • JPEG
  • GIF, including animated GIFs
  • PNG
  • APNG {{gecko_minversion_inline("1.9.2")}}
  • SVG {{gecko_minversion_inline("2.0")}}
  • BMP
  • BMP ICO
  • PNG ICO {{gecko_minversion_inline("9.0")}}

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

Interação com CSS

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

Depending of its type, an image may have an intrinsic dimension, but this is not a necessary condition: a SVG image has no intrinsic dimension, a raster image has one.

Exemplo 1

<img src="mdn-logo-sm.png" alt="MD Logo" />

MD Logo

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

MDN Logo

Example 3: Usando o atributo srcset

The src attribute is ignored on browsers supporting srcset when this one is provided. The displayed images will depend of the type of screen.

 <img src="mdn-logo-sm.png" 
      alt="MD Logo" 
      srcset="mdn-logo-HD.png 2x, mdn-logo-small.png 15w, mdn-banner-HD.png 100w 2x" />

Especificações

Specification Status Comment
{{SpecName('HTML WHATWG', 'embedded-content-1.html#the-img-element', '<img>')}} {{Spec2('HTML WHATWG')}}  
{{SpecName('HTML5 W3C', 'embedded-content-0.html#the-img-element', '<img>')}} {{Spec2('HTML5 W3C')}}  
{{SpecName('HTML4.01', 'struct/objects.html#h-13.2', '<img>')}} {{Spec2('HTML4.01')}}  

Compatibilidade dos navegadores

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
srcset attribute {{CompatChrome(34.0)}} {{CompatGeckoDesktop("32.0")}} (behind a pref) {{CompatNo}} {{CompatOpera(21)}} {{CompatSafari(7.1)}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
srcset attribute {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} iOS 8

Veja também

  • {{HTMLElement("object")}} and {{HTMLElement("embed")}} elements
{{HTMLRef}}

Fonte da revisão

<h2 id="Resumo">Resumo</h2>

<p>O Elementos&nbsp;<strong>HTML <code>&lt;img&gt;</code> </strong>&nbsp;(or <em>HTML Image Element</em>) representa a inseração de imagem no documento, sedo implemente dado também pelo HTML5.</p>

<div class="note">
<p><strong>Usage note:</strong><br />
 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 <a href="#Supported_image_formats" title="HTML/Element/Img#Supported_Image_Formats">unsupported type</a>. In these cases, the browser may replace the image with the text defined in this element's <strong>alt</strong> attribute.</p>
</div>

<ul class="htmlelt">
 <li><dfn><a href="/en-US/docs/HTML/Content_categories" title="HTML/Content_categories">Content categories</a></dfn> <a href="/en-US/docs/HTML/Content_categories#Flow_content" title="HTML/Content categories#Flow content">Flow content</a>, <a href="/en-US/docs/HTML/Content_categories#Phrasing_content" title="HTML/Content categories#Phrasing content">phrasing content</a>, embedded content, palpable content. If the element has a {{htmlattrxref("usemap", "img")}} attribute, it also is a part of the interactive content category.</li>
 <li><dfn>Permitted content</dfn> None, it is an {{Glossary("empty element")}}.</li>
 <li><dfn>Tag omission</dfn> Must have a start tag and must not have an end tag.</li>
 <li><dfn>Permitted parent elements</dfn> Any element that accepts embedded content.</li>
 <li><dfn>DOM interface</dfn> {{domxref("HTMLImageElement")}}</li>
</ul>

<h2 id="Atributos">Atributos</h2>

<p><span style="line-height:21px">This element includes the&nbsp;</span><a href="https://developer.mozilla.org/en-US/docs/HTML/Global_attributes" style="line-height: 21px;" title="HTML/Global attributes">global attributes</a><span style="line-height:21px">.</span></p>

<dl>
 <dt>{{htmlattrdef("align")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}} Use the {{cssxref('vertical-align')}} CSS property</dt>
 <dd>The alignment of the image with respect to its surrounding context.</dd>
 <dt>{{htmlattrdef("alt")}}</dt>
 <dd>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 <a href="#Supported_image_formats" title="HTML/Element/Img#Supported image formats">supported formats</a>, or until the image is downloaded.
 <div class="note">
 <p><strong>Usage note: </strong>Omitting this attribute indicates that the image <em>is</em> a key part of the content, but no textual equivalent is available. Setting this attribute to the empty string indicates that this image is <em>not</em> a key part of the content; non-visual browsers may omit it from rendering.</p>
 </div>
 </dd>
 <dt>{{htmlattrdef("border")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>The width of a border around the image.</dd>
 <dt>{{htmlattrdef("crossorigin")}} {{HTMLVersionInline(5)}}</dt>
 <dd>This enumerated attribute indicates if the fetching of the related image must be done using CORS or not. <a href="/en-US/docs/CORS_Enabled_Image" title="CORS_Enabled_Image">CORS-enabled images</a> can be reused in the {{HTMLElement("canvas")}} element without being <em>tainted</em>. The allowed values are:
 <dl>
  <dt>anonymous</dt>
  <dd>A cross-origin request (i.e. with <code>Origin:</code> 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 <code>Access-Control-Allow-Origin:</code> HTTP header), the image will be <em>tainted</em> and its usage restricted..</dd>
  <dt>use-credentials</dt>
  <dd>A cross-origin request (i.e. with <code>Origin:</code> HTTP header) 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 <code>Access-Control-Allow-Credentials:</code> HTTP header), the image will be <em>tainted</em> and its usage restricted.</dd>
 </dl>
 When not present, the resource is fetched without a CORS request (i.e. without sending the <code>Origin:</code> HTTP header), preventing its non-tainted usage in {{HTMLElement('canvas')}} elements. If invalid, it is handled as if the enumerated keyword <strong>anonymous</strong> was used. See <a href="/en-US/docs/HTML/CORS_settings_attributes" title="CORS settings attributes">CORS settings attributes</a> for additional information.</dd>
 <dt>{{htmlattrdef("height")}}</dt>
 <dd>The height of the image in {{HTMLVersionInline(5)}} CSS pixels, or {{HTMLVersionInline(4)}} in pixels or as a percentage.</dd>
 <dt>{{htmlattrdef("hspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>The number of pixels of white space to insert to the left and right of the image.</dd>
 <dt>{{htmlattrdef("ismap")}}</dt>
 <dd>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.
 <div class="note">
 <p><strong>Usage note: </strong>This attribute is allowed only if the <code>&lt;img&gt;</code> element is a descendant of an {{htmlelement("a")}} element with a valid {{htmlattrxref("href","a")}} attribute.</p>
 </div>
 </dd>
 <dt>{{htmlattrdef("longdesc")}}{{HTMLVersionInline(4)}} only</dt>
 <dd>The URL of a description of the image to be displayed, which supplements the <strong>alt</strong> text. In {{HTMLVersionInline(5)}}, use a regular {{HTMLElement("a")}} element to link to the description.</dd>
 <dt>{{htmlattrdef("name")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>A name for the element. It is supported in {{HTMLVersionInline(4)}} only for backward compatibility. Use the <strong>id</strong> attribute instead.</dd>
 <dt>{{htmlattrdef("src")}}</dt>
 <dd>Image URL, this attribute is obligatory for the <code>&lt;img&gt;</code> element. On browsers supporting <strong>srcset</strong>, <strong>src</strong> is ignored if this one is provided.</dd>
 <dt>{{htmlattrdef("srcset")}}{{HTMLVersionInline(5)}}</dt>
 <dd>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:
 <ol>
  <li>one URL to an image,</li>
  <li>a width descriptor, that is a positive integer directly followed by <code>'w'</code>. The default value, if missing, is the infinity.</li>
  <li>a pixel density descriptor, that is a positive floating number directly followed by <code>'x'</code>. The default value, if missing, is <code>1x</code>.</li>
 </ol>

 <p>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.<br />
  The browser chooses the most adequate image to display at a given point of time.</p>
 </dd>
 <dt>{{htmlattrdef("width")}}</dt>
 <dd>The width of the image in pixels or percent.</dd>
 <dt>{{htmlattrdef("usemap")}}</dt>
 <dd>The partial URL (starting with '#') of an <a href="/en-US/docs/HTML/Element/map" title="HTML/Element/Map">image map</a> associated with the element.
 <div class="note">
 <p><strong>Usage note: </strong>You cannot use this attribute if the <code>&lt;img&gt;</code> element is a descendant of an {{htmlelement("a")}} or {{HTMLElement("button")}} element.</p>
 </div>
 </dd>
 <dt>{{htmlattrdef("vspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>The number of pixels of white space to insert above and below the image.</dd>
</dl>

<h2 id="Formato_de_imagens_suportadas">Formato de imagens suportadas</h2>

<p>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:</p>

<ul>
 <li><a class="external" href="https://en.wikipedia.org/wiki/JPEG" title="https://en.wikipedia.org/wiki/JPEG">JPEG</a></li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/Graphics_Interchange_Format" title="https://en.wikipedia.org/wiki/Graphics_Interchange_Format">GIF</a>, including animated GIFs</li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/Portable_Network_Graphics" title="https://en.wikipedia.org/wiki/Portable_Network_Graphics">PNG</a></li>
 <li><a href="/en-US/docs/Animated_PNG_graphics" title="Animated PNG graphics">APNG</a> {{gecko_minversion_inline("1.9.2")}}</li>
 <li><a href="/en-US/docs/SVG" title="SVG">SVG</a> {{gecko_minversion_inline("2.0")}}</li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/BMP_file_format" title="https://en.wikipedia.org/wiki/BMP_file_format">BMP</a></li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/ICO_%28file_format%29" title="https://en.wikipedia.org/wiki/ICO_%28file_format%29">BMP ICO</a></li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/ICO_%28file_format%29" title="https://en.wikipedia.org/wiki/ICO_%28file_format%29">PNG ICO</a> {{gecko_minversion_inline("9.0")}}</li>
</ul>

<div class="note">
<p><strong>Note:</strong><br />
 Support for the <a class="external" href="https://en.wikipedia.org/wiki/X_BitMap" title="https://en.wikipedia.org/wiki/X_BitMap">XBM</a> format has been removed in Gecko 1.9.2.</p>
</div>

<h2 id="Interação_com_CSS">Interação com CSS</h2>

<p>Regarding CSS, an <code>&lt;img&gt;</code> is a <a href="/en-US/docs/CSS/Replaced_element" title="CSS/Replaced_element">replaced element</a>. It has no baseline, meaning, when used in an inline formatting context with {{cssxref("vertical-align")}}<code>: baseline</code>, the bottom of the image will be posed on the container's baseline.</p>

<p>Depending of its type, an <em>image</em> may have an intrinsic dimension, but this is not a necessary condition: a SVG image has no intrinsic dimension, a raster image has one.</p>

<h2 id="Exemplo_1">Exemplo 1</h2>

<pre class="brush: html">
&lt;img src="mdn-logo-sm.png" alt="MD Logo" /&gt;
</pre>

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

<h2 id="Exemplo_2_Link_em_imagem">Exemplo 2: Link em imagem</h2>

<pre class="brush: html">
&lt;a href="https://developer.mozilla.org/"&gt;&lt;img src="mdn-logo-sm.png" alt="MDN Logo" /&gt; &lt;/a&gt;
</pre>

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

<h2 id="Specifications" name="Specifications">Example 3: Usando o atributo <code>srcset</code></h2>

<p>The <code>src</code> attribute is ignored on browsers supporting <code>srcset</code> when this one is provided. The displayed images will depend of the type of screen.</p>

<pre class="brush: html">
 &lt;img src="mdn-logo-sm.png" 
      alt="MD Logo" 
      srcset="mdn-logo-HD.png 2x, mdn-logo-small.png 15w, mdn-banner-HD.png 100w 2x" /&gt;</pre>

<h2 id="Specifications" name="Specifications">Especificações</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'embedded-content-1.html#the-img-element', '&lt;img&gt;')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', 'embedded-content-0.html#the-img-element', '&lt;img&gt;')}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML4.01', 'struct/objects.html#h-13.2', '&lt;img&gt;')}}</td>
   <td>{{Spec2('HTML4.01')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Compatibilidade dos navegadores</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>srcset attribute</td>
   <td>{{CompatChrome(34.0)}}</td>
   <td>{{CompatGeckoDesktop("32.0")}} (behind a pref)</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera(21)}}</td>
   <td>{{CompatSafari(7.1)}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>srcset attribute</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>iOS 8</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Veja_também">Veja também</h2>

<ul>
 <li>{{HTMLElement("object")}} and {{HTMLElement("embed")}} elements</li>
</ul>

<div>{{HTMLRef}}</div>
Reverter para esta revisão