Etiqueta HTML <img>


Ejemplo

Cómo insertar una imagen:

<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">

Más ejemplos de "Pruébelo usted mismo" a continuación.


Definición y uso

La <img>etiqueta se utiliza para incrustar una imagen en una página HTML.

Técnicamente, las imágenes no se insertan en una página web; las imágenes están vinculadas a páginas web. La <img>etiqueta crea un espacio de espera para la imagen de referencia.

La <img>etiqueta tiene dos atributos obligatorios:

  • src: especifica la ruta a la imagen
  • alt: especifica un texto alternativo para la imagen, si la imagen por algún motivo no se puede mostrar

Nota: Además, especifique siempre el ancho y el alto de una imagen. Si no se especifican el ancho y el alto, la página puede parpadear mientras se carga la imagen.

Sugerencia: para vincular una imagen a otro documento, simplemente anide la <img>etiqueta dentro de una etiqueta <a> (vea el ejemplo a continuación).


Compatibilidad con 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 globales

La <img>etiqueta también es compatible con los atributos globales en HTML .


Atributos de eventos

La <img>etiqueta también es compatible con los atributos de eventos en HTML .


Más ejemplos

Ejemplo

Alinear imagen (con 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">

Ejemplo

Agregar borde de imagen (con CSS):

<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="border:5px solid black">

Ejemplo

Agregue márgenes izquierdo y derecho a la imagen (con CSS):

<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:0px 50px">

Ejemplo

Agregue márgenes superior e inferior a la imagen (con CSS):

<img src="smiley.gif" alt="Smiley face" width="42" height="42" style="vertical-align:middle;margin:50px 0px">

Ejemplo

Cómo insertar imágenes de otra carpeta o de otro sitio web:

<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">

Ejemplo

Cómo agregar un hipervínculo a una imagen:

<a href="https://www.w3schools.com">
<img src="w3html.gif" alt="W3Schools.com" width="100" height="132">
</a>

Ejemplo

Cómo crear un mapa de imagen, con regiones en las que se puede hacer clic. Cada región es un hipervínculo:

<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 de HTML: imágenes HTML

Referencia HTML DOM: Objeto de imagen

Tutorial de CSS: Dar estilo a las imágenes


Configuración predeterminada de CSS

La mayoría de los navegadores mostrarán el <img>elemento con los siguientes valores predeterminados:

Ejemplo

img {
  display: inline-block;
}