Esta tradução está incompleta. Ajude a traduzir este artigo em inglês
Resumo
O elemento HTML <img> (or HTML Image Element) representa a inserção de imagem no documento, sendo implementado também pelo HTML5 para uma melhor experiência com o elemento <figure> e <figcaption>.
Exemplo da implementação do HTML5
<figure>
<img src="imagem.jpg" alt="Minha Figura">
<figcaption>Informações da Figura</figcaption>
</figure>
Usage note:
Navegadores nem sempre exibem a imagem referenciada pelo elemento. Este é o caso para navegadores não gráficos (incluindo aqueles usados por pessoas com deficiência de visão), ou se o usuário optar por não exibir imagens ou se o navegador não conseguir exibir a imagem porque é inválido ou um tipo não suportado. Nesses casos, o navegador pode substituir a imagem pelo texto definido no atributo alt deste elemento.
- Content categories Flow content, phrasing content, embedded content, palpable content. If the element has a
usemapattribute, 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
Atributos
Este Elemeto inlcui o atributo global.
alignDeprecated since HTML4.01, Obsolete since HTML5 Use thevertical-alignCSS property- The alignment of the image with respect to its surrounding context.
alt- Este atributo define um texto alternativo que descreve a imagem. Os Usuários irão ver o texto se a URL da imagem estiver errado, a imagem não está em um dos formatos suportados ou até a imagem ser baixada .
-
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.
borderDeprecated since HTML4.01, Obsolete since HTML5- The width of a border around the image.
crossoriginHTML5- 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 theAccess-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 (throughAccess-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 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.
hspaceDeprecated 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.
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. nameDeprecated 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. On browsers supporting srcset, src is ignored if this one is provided. 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:
- one URL to an image,
- a width descriptor, that is a positive integer directly followed by
'w'. The default value, if missing, is the infinity. - a pixel density descriptor, that is a positive floating number directly followed by
'x'. The default value, if missing, is1x.
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. width- The width of the image in pixels or percent.
usemap- The partial URL (starting with '#') of an image map associated with the element.
vspaceDeprecated since HTML4.01, Obsolete since HTML5- The number of pixels of white space to insert above and below the image.
Formato de imagens suportadas
O padrão HTML não fornece uma lista de formatos de imagem que devem ser suportados, portanto, cada agente de usuário oferece suporte a um conjunto diferente de formatos. Gecko suporta:
Interação com CSS
Em relação ao CSS, uma <img> é um elemento substituído. Ele não tem linha de base, ou seja, quando usado em um contexto de formatação em linha (inline) com vertical-align: baseline, a base inferior da imagem será colocada na linha de base do contêiner.
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="MDN Logo" />

Exemplo 2: Link em imagem
<a href="https://developer.mozilla.org/"><img src="mdn-logo-sm.png" alt="MDN Logo" /> </a>
Example 3: Usando o atributo srcset
O atributo src é ignorado em navegadores que possuem suporte à srcset quando este é fornecido. As imagens exibidas dependerão do tipo de tela.
srcset define o conjunto de imagens que nós iremos permitir ao navegador escolher, e qual tamanho cada imagem tem.
<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" />
Antes de cada vírgula nós escrevemos:
- Um nome do arquivo da imagem (
mdn-logo-HD.png). - Um espaço.
- A largura da imagem
Especificações
| Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of '<img>' in that specification. |
Padrão em tempo real | |
| HTML5 The definition of '<img>' in that specification. |
Recomendação | |
| HTML 4.01 Specification The definition of '<img>' in that specification. |
Recomendação |
Compatibilidade dos navegadores
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| srcset attribute | 34.0 | 32.0 (32.0) (behind a pref) | Não suportado | 21 | 7.1 |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| srcset attribute | Não suportado | Não suportado | Não suportado | Não suportado | iOS 8 |