HTML Images

HTML Images: Embedding Visuals

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:

Basic Image Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML Images</title>
</head>
<body>
    <img src="https://via.placeholder.com/300" alt="Placeholder image" />
</body>
</html>

HTML img Tag Attributes

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.

1. The alt Attribute

The alt attribute in the <img> tag provides alternative text when an image cannot be displayed and improves accessibility.

alt Attribute Example:

<!DOCTYPE html>
<html lang="en">
<body>
    <img src="broken-link.png" alt="This is the IntricateDevo logo" />
</body>
</html>

2. Set Image Size - Width and Height Attribute

The width and height attributes in the <img> tag define the size of an image, with values specified in pixels by default.

Width and Height Example:

<!DOCTYPE html>
<html lang="en">
<body>
    <img src="https://via.placeholder.com/300" 
         alt="IntricateDevo logo" 
         width="300" 
         height="300" />
</body>
</html>

3. Adding Titles to an Image

The title attribute adds a tooltip to an image that appears when a user hovers over it.

Title Attribute Example:

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

4. Setting Style of an Image

The border attribute is used to control the border appearance of an image.

Border Attribute Example:

<!DOCTYPE html>
<html lang="en">
<body>
    <img src="https://via.placeholder.com/200" 
         alt="IntricateDevo logo" 
         width="200" 
         height="200" 
         border="5" />
</body>
</html>

5. Set Image Alignment

Image alignment in HTML controls how an image is positioned within a webpage layout.

Align Attribute Example:

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

6. Adding Image as a Link

Wrap the <img> tag inside an <a> tag to make the image clickable and link it to another page or resource.

Image Link Example:

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

7. Adding Animated Image

Animated images in HTML are added using GIF files to create motion effects on webpages.

Animated Image Example:

<!DOCTYPE html>
<html lang="en">
<body>
    <img src="animated-smiley.gif" 
         alt="Animated smiley" 
         style="width: 200px; height: 200px;" /> 
</body>
</html>

Image Formats

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

Tips for Using HTML Images Effectively

Use images wisely in HTML to improve performance, accessibility, and overall user experience.


Exercise

?

Which attribute in the <img> tag provides alternative text when an image cannot be displayed?