<img>

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>

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

Atributos

Este Elemeto inlcui o atributo global.

align Deprecated

The alignment of the image with respect to its surrounding context. Use the vertical-align CSS property.

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 .

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

border Deprecated

The width of a border around the image.

crossorigin

This enumerated attribute indicates if the fetching of the related image must be done using CORS or not. CORS-enabled images (en-US) 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) 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 <canvas> elements. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes (en-US) for additional information.

height

The height of the image in HTML 5 CSS pixels, or HTML 4 in pixels or as a percentage.

hspace Deprecated

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.

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

longdesc Deprecated

A link to a more detailed description of the image. Possible values are a URL or an element id.

name Deprecated

A name for the element. 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.

srcset

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.

width

The width of the image in pixels or percent.

usemap

The partial URL (starting with '#') of an image map associated with the element.

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

vspace Deprecated

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 (en-US). 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.

Exemplos

Usando o texto alternativo

O exemplo a seguir insere uma imagem na página e inclui o texto alternativo para acessibilidade, de forma que ele possa ser lido por programas leitores de tela ou exibido caso a imagem não carregue.

html
<img
  src="https://developer.mozilla.org/static/img/favicon144.png"
  alt="MDN logo" />

Esse exemplo mostra como transformar uma imagem em um link. Para isso, insira a tag <img> dentro da tag do link <a>. Nesse caso, é interessante fazer o texto alternativo descrever o site para o qual o link aponta, como se fosse o texto usado dentro da tag <a>.

html
<a href="https://developer.mozilla.org">
  <img
    src="https://developer.mozilla.org/static/img/favicon144.png"
    alt="Visit the MDN site" />
</a>

Usando o atributo srcset

Nesse exemplo, o atributo srcset inclui uma versão do logotipo com maior resolução, a qual vai ser carregada no lugar de src em aparelhos de alta resolução e cujo navegador tenha suporte à srcset.

A imagem presente no atributo src conta como um candidato 1x em navegadores com suporte à srcset.

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.

html
<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:

  1. Um nome do arquivo da imagem (mdn-logo-HD.png).
  2. Um espaço.
  3. A largura da imagem ou sua densidade de pixels.

Acessibilidade

Utilizando textos alternativos relevantes

O valor do atributo alt deve descrever de maneira clara e concisa o conteúdo da imagem. Evite descrever a própria presença da imagem ou o nome de seu arquivo. Se o atributo alt for propositalmente deixado vazio porque a imagem não possui nenhum equivalente em texto, considere métodos alternativos para indicar o que a imagem deseja comunicar.

Evite

html
<img alt="imagem" src="penguin.jpg" />

Prefira

html
<img alt="Um pinguim-saltador-da-rocha em pé numa praia." src="penguin.jpg" />

Quando o atributo alt não estiver presente em uma imagem, alguns programas leitores de tela vão narrar o nome do arquivo da imagem em seu lugar, o que pode ser confuso caso o nome do arquivo não seja representativo do conteúdo da imagem.

O atributo title

O atributo title não é um substituto aceitável para o atributo alt. Além disso, evite duplicar o valor do atributo alt no atributo title para uma mesma imagem. Isso pode fazer com que alguns programas leitores de tela narrem duas vezes a descrição, o que pode criar uma experiência confusa para usuários.

Evite usar o atributo title como uma forma suplementar de legenda para a descrição do alt. Caso a imagem precise de uma legenda, prefisa os elementos figure e figcaption.

O valor do atributo title é geralmente mostrado ao usuário como uma dica, que aparece após o usuário parar o cursor sobre a imagem. Apesar de poder prover informações adicionais ao usuário, não se deve assumir todos os usuários vão vê-lo, pois o mesmo pode possuir apenas um teclado ou uma tela de toque (touchscreen). Se você considera a informação particularmente importante para o usuário, prefira o uso de elementos inline.

Especificações

Specification
HTML Standard
# the-img-element

Compatibilidade dos navegadores

BCD tables only load in the browser

Veja também