Everything You Should Know about <img> Tag in HTML

Everything You Should Know about <img> Tag in HTML

October 28, 2024·Tarık Korucuoğlu
Tarık Korucuoğlu

The <img> tag is an essential element in HTML that allows you to embed images into web pages. This tag is integral for creating visually appealing websites, enhancing user engagement, and conveying information effectively. In this post, we'll explore the various attributes of the <img> tag, best practices for using it, and some tips for optimizing images for the web.

What is the <img> Tag?

The <img> tag is a self-closing HTML element used to display images on a web page. Unlike other HTML tags, it does not have a closing tag. Instead, it contains attributes that define the source of the image, its size, and alternative text, among other properties.

Basic Syntax

Here’s the basic syntax of the <img> tag:

<img src="image-url" alt="description" />
    - ***src*** : This attribute specifies the URL of the image you want to display. It can be a relative or absolute path.
    • alt : This attribute provides alternative text for the image, which is displayed if the image cannot be loaded. It’s also essential for accessibility, helping screen readers convey information to visually impaired users.

    Example of the <img> Tag

    Here’s a simple example of how to use the <img> tag:

    <img src="https://example.com/image.jpg" alt="A beautiful landscape" />

    In this example, the image located at the provided URL will be displayed, and if it fails to load, the text "A beautiful landscape" will appear in its place.

    Important Attributes of the <img> Tag

    Besides the src and alt attributes, the <img> tag includes several other attributes that can help customize how images are displayed on a webpage:

      - ***width and height*** : These attributes specify the dimensions of the image in pixels. Setting these values can help maintain the layout of your page as the image loads.
        <img src="image.jpg" alt="Sample Image" width="300" height="200" />
        - ***title*** : This attribute provides additional information about the image. When a user hovers over the image, the text in the title attribute will appear as a tooltip.
          <img src="image.jpg" alt="Sample Image" title="This is a sample image." />
          - ***loading*** : This attribute allows you to control how the image is loaded. The options are lazy (for lazy loading, which improves performance by loading images only when they are in the viewport) and eager (to load images immediately).
            <img src="image.jpg" alt="Sample Image" loading="lazy" />
            - ***class and id*** : These attributes can be used to apply CSS styles or JavaScript functions to the image.
              <img src="image.jpg" alt="Sample Image" class="responsive" id="featured-image" />

            Best Practices for Using the <img> Tag

              - ***Use Descriptive Alt Text*** : Always provide meaningful alt text for your images. This not only improves accessibility but also enhances SEO by allowing search engines to understand the content of the image.
              • Optimize Image Size : Large image files can slow down your website. Optimize images for the web by compressing them and using appropriate file formats (e.g., JPEG for photographs, PNG for graphics with transparency).
              • Use Responsive Images : To ensure images look good on all devices, consider using the srcset attribute or CSS for responsive design. The srcset attribute allows you to define multiple image sources for different screen sizes.
                 <img src="small.jpg" srcset="medium.jpg 640w, large.jpg 1280w" alt="A responsive image" />
                - ***Use Appropriate File Formats*** : Choose the right file format for your images based on their content. For example:
                  - ***JPEG*** is ideal for photographs and images with many colors.
                  • PNG is better for images with transparency or when you need lossless compression.
                  • SVG is perfect for logos and graphics as it scales without losing quality.
                    - ***Lazy Loading*** : Implement lazy loading for images to enhance performance, especially on pages with many images. This technique delays loading images until they are in the viewport.
                       <img src="image.jpg" alt="Sample Image" loading="lazy" />

Last updated on