Tag HTML <img>
Exemplo
Como inserir uma imagem:
<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">
Mais exemplos de "Experimente você mesmo" abaixo.
Definição e uso
A <img>
tag é usada para incorporar uma imagem em uma página HTML.
As imagens não são tecnicamente inseridas em uma página da web; as imagens estão vinculadas a páginas da web. A <img>
tag cria um espaço de retenção para a imagem referenciada.
A <img>
tag tem dois atributos obrigatórios:
- src - Especifica o caminho para a imagem
- alt - Especifica um texto alternativo para a imagem, se a imagem por algum motivo não puder ser exibida
Nota: Além disso, sempre especifique a largura e a altura de uma imagem. Se a largura e a altura não forem especificadas, a página poderá piscar enquanto a imagem é carregada.
Dica: Para vincular uma imagem a outro documento, simplesmente aninhe a <img>
tag dentro de uma tag <a> (veja o exemplo abaixo).
Suporte ao navegador
Element | |||||
---|---|---|---|---|---|
<img> | Yes | Yes | Yes | Yes | Yes |
Atributos
Attribute | Value | Description |
---|---|---|
alt | text | Specifies an alternate text for an image |
crossorigin | anonymous use-credentials |
Allow images from third-party sites that allow cross-origin access to be used with canvas |
height | pixels | Specifies the height of an image |
ismap | ismap | Specifies an image as a server-side image map |
loading | eager lazy |
Specifies whether a browser should load an image immediately or to defer loading of images until some conditions are met |
longdesc | URL | Specifies a URL to a detailed description of an image |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin unsafe-url |
Specifies which referrer information to use when fetching an image |
sizes | sizes | Specifies image sizes for different page layouts |
src | URL | Specifies the path to the image |
srcset | URL-list | Specifies a list of image files to use in different situations |
usemap | #mapname | Specifies an image as a client-side image map |
width | pixels | Specifies the width of an image |
Atributos Globais
A <img>
tag também suporta os Atributos Globais em HTML .
Atributos do evento
A <img>
tag também suporta os atributos de evento em HTML .
Mais exemplos
Exemplo
Alinhar imagem (com CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:bottom">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:top">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="float:right">
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="float:left">
Exemplo
Adicionar borda de imagem (com CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42"
style="border:5px solid black">
Exemplo
Adicione margens esquerda e direita à imagem (com CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42"
style="vertical-align:middle;margin:0px 50px">
Exemplo
Adicione margens superior e inferior à imagem (com CSS):
<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:50px 0px">
Exemplo
Como inserir imagens de outra pasta ou de outro site:
<img src="/images/stickman.gif" alt="Stickman" width="24" height="39">
<img src="https://www.w3schools.com/images/lamp.jpg" alt="Lamp" width="32"
height="32">
Exemplo
Como adicionar um hiperlink a uma imagem:
<a href="https://www.w3schools.com">
<img src="w3html.gif"
alt="W3Schools.com" width="100" height="132">
</a>
Exemplo
Como criar um mapa de imagem, com regiões clicáveis. Cada região é um hiperlink:
<img src="workplace.jpg" alt="Workplace" usemap="#workmap" width="400"
height="379">
<map name="workmap">
<area shape="rect"
coords="34,44,270,350" alt="Computer" href="computer.htm">
<area
shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
<area shape="circle" coords="337,300,44" alt="Cup of coffee" href="coffee.htm">
</map>
Páginas relacionadas
Tutorial HTML: Imagens HTML
Referência HTML DOM: objeto de imagem
Tutorial CSS: Estilizando Imagens
Configurações padrão de CSS
A maioria dos navegadores exibirá o <img>
elemento com os seguintes valores padrão:
Exemplo
img {
display: inline-block;
}