The HTML <img> tag is used to embed images like photos, illustrations, or icons in a webpage. It is a self-closing tag (no separate closing tag) and can display images from your local project folders or from online URLs.
In addition to the basic <img> tag, you can also use CSS to apply images as backgrounds on elements, and use extra attributes like loading and decoding to improve performance.
images/photo.jpg) or an external URL.A basic <img> element must include at least the src and alt attributes. Width and height are recommended to help the browser reserve layout space.
<img src="path-or-url" alt="Short description of the image" width="300" height="200" />
To use images as decoration, you can apply them via CSS using background-image. This is useful for cards, banners, or sections where the image is not core content.
<div style="
width:300px;
height:200px;
background-image: url('background.jpg');
background-size: cover;
background-position: center;
">
</div>
<!-- Embedding an image with source and alt text -->
<img
src="https://via.placeholder.com/300" <span class="comment"><!-- Image URL --></span>
alt="Placeholder Image" <span class="comment"><!-- Alt text --></span>
width="300" <span class="comment"><!-- Width (optional) --></span>
height="200" <span class="comment"><!-- Height (optional) --></span>
title="Sample Image" <span class="comment"><!-- Tooltip text --></span>
/>
<!-- Div with a background image -->
<div
style="width:300px; height:200px;
background-image: url('https://via.placeholder.com/300x200');
background-size: cover;
background-position: center;
border-radius: 8px;">
</div>
<img
src="https://via.placeholder.com/300"
alt="Placeholder Image"
loading="lazy" <span class="comment"><!-- Loads when needed --></span>
decoding="async" <span class="comment"><!-- Decodes asynchronously --></span>
width="300"
height="200"
/>
The following image is loaded from an external URL. The browser knows its width and height in advance, and hovering shows the title as a tooltip.

Here, the image is purely decorative. It is set as a background of a <div>, stretched to cover the full area and centered.
This image uses loading="lazy" and decoding="async". On long pages, it will only start loading when you scroll near it, improving performance.

alt text for accessibility and SEO.width and height to reduce layout shifts while the image loads.images/logo.png for project images.background-image for decorative visuals, not for important content.loading="lazy" for below-the-fold images to boost performance.alt, width, and height.background-image on a <div>.loading="lazy". Inspect network requests to see when they load.background-size values: cover, contain, and specific pixel sizes.<img> tag embeds images directly into your HTML.src, alt, width, height, and title.background-image for decorative images and visual styling.loading="lazy" and decoding="async" help optimize page speed.