The HTML <img> tag is used to embed an image in web pages by linking them. It creates a placeholder for the image, defined by attributes like src, width, height, and alt, and does not require a closing tag.
There are two ways to insert images into a webpage:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Images</title>
</head>
<body>
<img src="https://via.placeholder.com/300" alt="Placeholder image" />
</body>
</html>
<img> tag is used to embed an image in a webpage.src specifies the image source URL.alt provides descriptive text if the image cannot be displayed.| Attribute | Description |
|---|---|
src |
Specifies the path to the image file. |
alt |
Provides alternate text for the image, useful for accessibility and when the image cannot be displayed. |
crossorigin |
Allows importing images from third-party sites with cross-origin access, typically used with canvas. |
height |
Specifies the height of the image. |
width |
Specifies the width of the image. |
ismap |
Specifies an image as a server-side image map. |
loading |
Specifies whether the browser should defer image loading or load it immediately. |
longdesc |
Specifies a URL to a detailed description of the image. |
referrerpolicy |
Specifies which referrer information to use when fetching the image. |
sizes |
Specifies image sizes for different page layouts. |
srcset |
Specifies a list of image files to use in different situations, allowing for responsive images. |
usemap |
Specifies an image as a client-side image map. |
The alt attribute in the <img> tag provides alternative text when an image cannot be displayed and improves accessibility.
<!DOCTYPE html>
<html lang="en">
<body>
<img src="broken-link.png" alt="This is the IntricateDevo logo" />
</body>
</html>
The width and height attributes in the <img> tag define the size of an image, with values specified in pixels by default.
<!DOCTYPE html>
<html lang="en">
<body>
<img src="https://via.placeholder.com/300"
alt="IntricateDevo logo"
width="300"
height="300" />
</body>
</html>
The title attribute adds a tooltip to an image that appears when a user hovers over it.
title attribute in the <img> tag.
<!DOCTYPE html>
<html lang="en">
<body>
<img src="https://via.placeholder.com/200"
alt="IntricateDevo logo"
width="200"
height="200"
title="Logo of IntricateDevo" />
</body>
</html>
The border attribute is used to control the border appearance of an image.
border="0" removes the border completely.
<!DOCTYPE html>
<html lang="en">
<body>
<img src="https://via.placeholder.com/200"
alt="IntricateDevo logo"
width="200"
height="200"
border="5" />
</body>
</html>
Image alignment in HTML controls how an image is positioned within a webpage layout.
align attribute in the <img> tag.left, right, and center.align attribute positions the image on the right side of the webpage. It shows how image alignment worked in older HTML, though CSS is now the recommended method.
<!DOCTYPE html>
<html lang="en">
<body>
<img src="https://via.placeholder.com/150"
alt="IntricateDevo logo"
align="right" />
<p>This text will wrap around the image that is aligned to the right.</p>
</body>
</html>
Wrap the <img> tag inside an <a> tag to make the image clickable and link it to another page or resource.
<!DOCTYPE html>
<html lang="en">
<body>
<a href="https://www.youtube.com/">
<img src="https://via.placeholder.com/200"
alt="IntricateDevo link logo" />
</a>
</body>
</html>
Animated images in HTML are added using GIF files to create motion effects on webpages.
<img> tag with a GIF file as the source.
<!DOCTYPE html>
<html lang="en">
<body>
<img src="animated-smiley.gif"
alt="Animated smiley"
style="width: 200px; height: 200px;" />
</body>
</html>
Here is the commonly used image file format that is supported by all the browsers.
| Abbreviation | File Type | Extension |
|---|---|---|
| PNG | Portable Network Graphics | .png |
| JPEG | Joint Photographic Expert Group image | .jpg, .jpeg, .jfif, .pjpeg, .pjp |
| SVG | Scalable Vector Graphics | .svg |
| GIF | Graphics Interchange Format | .gif |
| ICO | Microsoft Icon | .ico, .cur |
| APNG | Animated Portable Network Graphics | .apng |
Use images wisely in HTML to improve performance, accessibility, and overall user experience.
srcset to serve different images for different devices and resolutions.Which attribute in the <img> tag provides alternative text when an image cannot be displayed?