How to Use Image Tag in HTML

Within the ever-evolving realm of web development, the process of incorporating images into your website plays a crucial role in creating visually appealing and engaging content. HTML, serving as the fundamental structure of web pages, employs the <img> tag to seamlessly integrate images. This comprehensive guide will explore the intricacies of leveraging the <img> tag, offering detailed explanations and diverse examples to aid you in mastering the art of image embedding in HTML.

The structures of the <img> Tag:

Prior to exploring instances, it is crucial to understand the fundamental structure of the <img> tag:

Example

<!DOCTYPE html>
<html lang = " en ">
<body>
    <!-- Example 1: Displaying a Local Image -->
    <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " A beautiful landscape " >

    <!-- Example 2: Adding Dimensions to an Image from a URL -->
    <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " An example image " width = " 300 " height = " 200 " >

    <!-- Example 3: Making an Image Responsive -->
    <style>
        img {
            max-width: 100%;
            height: auto;
        }
    </style>
    <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " Responsive image " >
</body>
</html>
  • src (source): This trait is compulsory and indicates the path to the image file. It tends to be a local file path or a URL.
  • alt (alternative text): Additionally compulsory, this quality gives alternative text to the image. It is significant for accessibility, supporting screen perusers in depicting the image to outwardly debilitated users.
  • width and height: These discretionary attributes characterize the width and height of the image, separately. It is prescribed to set either the width or the height, not both, to keep up with the viewpoint proportion and forestall mutilation.
  • 1. Showing a Local Image

In this instance, replace "images/my-image.jpg" with the actual path to your image file. The alt attribute provides descriptive text for accessibility purposes.

Example

<img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " A beautiful landscape ">

Example:

Example

<!DOCTYPE html>
<html lang = " en ">
<head>
    <meta charset = " UTF ? 8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE = edge " >
    <meta name = " viewport " content = " width = device-width, initial-scale = 1.0 "  >
    <title> Local Image Example </title>
</head>
<body>
    <!-- Displaying a Local Image -->
    <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " A beautiful landscape " >
</body>
</html>

2. Adding Dimensions

The following illustration demonstrates the use of an image sourced from a URL, specifying specific width and height dimensions in pixels.

Example

<img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " An example image " width = " 300 " height = " 200 ">

Example:

Example

<!DOCTYPE html>
<html lang = " en ">
<head>
    <meta charset = " UTF ? 8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE = edge " >
    <meta name = " viewport " content = " width = device-width, initial-scale = 1.0 "  >
    <title> Image with Dimensions Example </title>
</head>
<body>
    <!-- Adding Dimensions to an Image -->
    <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " An example image " width = " 300 " height = " 200 " >
</body>
</html>

3. Responsive Images

In order to ensure that images are responsive, adapt to different screen sizes, and maintain aspect ratio, you can apply the following CSS properties:

By setting the maximum width to 100%, the image can scale down proportionally on smaller screens.

Example

img {
  max-width: 100%;
  height: auto;
}

Example:

Example

<!DOCTYPE html>
<html lang = " en ">
<head>
    <meta charset = " UTF ? 8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE = edge " >
    <meta name = " viewport " content = " width = device-width, initial-scale = 1.0 "  >
    <style>
        /* Responsive Images */
        img {
            max-width: 100%;
            height: auto;
        }
    </style>
    <title> Responsive Image Example </title>
</head>
<body>
    <!-- Responsive Images using CSS -->
    <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " A responsive image ">
</body>
</html>

Additional Examples:

1. Connecting Images

Images can be converted into clickable links by enclosing the <img> tag within an <a> (anchor) tag. This allows for creating an interactive image that directs users to a specified URL.

Example

<a href = " https://logic-practice.com " >
  <img src = "https://placehold.co/400x300/9b59b6/ffffff?text=Sample+Image" alt = " Clickable image ">
</a>

Example:

Example

<!DOCTYPE html>
<html lang = " en ">
<head>
    <meta charset = " UTF ? 8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE = edge " >
    <meta name = " viewport " content = " width = device-width, initial-scale = 1.0 "  >
    <title> Linking Image Example </title>
</head>
<body>
    <!-- Linking an Image -->
    <a href = " https://logic-practice.com ">
        <img src = "https://placehold.co/400x300/3498db/ffffff?text=Sample+Image" alt = " Clickable image ">
    </a>
</body>
</html>

2. Various Images in a Row

Arrange various images horizontally in a row using the provided HTML layout. Enhance the alignment and spacing by applying CSS styles to the .image-row class.

Example

<div class = " image-row ">
  <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " Image 1 ">
  <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " Image 2 ">
  <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " Image 3 ">
</div>

Example:

Example

<!DOCTYPE html>
<html lang = " en ">
<head>
    <meta charset = " UTF ? 8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE = edge " >
    <meta name = " viewport " content = " width = device-width, initial-scale = 1.0 "  >
    <style>
        /* Styling for Image Row */
        .image-row {
            display: flex;
            justify-content: space-between;
        }
    </style>
    <title> Multiple Images Example </title>
</head>
<body>
    <!-- Displaying Multiple Images in a Row -->
    <div class = " image-row ">
        <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " Image 1 ">
        <img src = "https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image" alt = " Image 2 ">
        <img src = "https://placehold.co/300x300/1abc9c/ffffff?text=Sample+Image" alt = " Image 3 " >
    </div>
</body>
</html>

3. Image as a Background

On occasion, it is beneficial to incorporate an image as a background element. This can be achieved by following the provided demonstration. By doing so, you will establish the background image for the specified div, allowing you to superimpose content on top of it.

Example

<div class = " background-image " style = " background-image: url(' background.jpg '); ">
  <h1> Your Content Here </h1>
</div>

Example:

Example

<!DOCTYPE html>
<html lang = " en ">
<head>
    <meta charset = " UTF ? 8 " >
    <meta http-equiv = " X-UA-Compatible " content = " IE = edge " >
    <meta name = " viewport " content = " width = device-width, initial-scale = 1.0 "  >
    <style>
        /* Styling for Background Image */
        .background-image {
            background-size: cover;
            background-position: center;
            height: 300px; /* Adjust the height as needed */
            text-align: center;
            color: white; /* Text color for better visibility */
            padding: 20px;
        }
    </style>
    <title> Background Image Example </title>
</head>
<body>
    <!-- Using an Image as a Background -->
    <div class = " background ? image " style = " background ? image : url(' background.jpg '); ">
        <h1> Your Content Here </h1>
    </div>
</body>
</html>

Extra Tips for Image Optimization:

  • Spellbinding File Names: Pick significant names for your image files to upgrade association and further develop SEO.
  • Image Compression: Pack images to lessen file size or very smaller size without compromising quality, guaranteeing quicker page loading times.
  • Image Accessibility: Consistently give elucidating alternative text to images, guaranteeing inclusivity and easy accessibility.
  • Dynamic Image Loading: Use JavaScript to stack the images dynamically founded on user communications or for the page scrolling for further developed execution.
  • Conclusion

Mastering the <img> element in HTML is essential for creating visually appealing and accessible websites. Understanding its properties and exploring various instances allows for seamless integration of pictures on your site, enhancing the overall user experience. Regardless of your expertise level in development, these insights will empower you to harness the power of images in web design.

In the ever-evolving realm of technology, staying up-to-date on recommended practices, emerging trends, and new possibilities in image processing is crucial. Delve into advanced techniques such as lazy loading and responsive design frameworks to ensure your website stays ahead in user experience standards.

Input Required

This code uses input(). Please provide values below: