Live Preview
Standalone image embedded with the img element:


By the end of this tutorial, you’ll embed images in HTML using the <img> element with proper attributes, responsive techniques, and accessibility.
The <img> tag is a fundamental HTML element for displaying photos, icons, logos, and illustrations. This guide covers syntax, attributes, common patterns, and best practices for beginners and web developers alike.
Use src and alt on every image.
Understand that img has no closing tag in HTML.
Set width and height to prevent layout shift.
Wrap img in a for clickable thumbnails.
Scale across devices with srcset and CSS.
Write meaningful alt text for all users.
<img> Tag?The image element (<img>) embeds an image into an HTML document. It is a void (empty) element — it does not wrap content and has no closing tag in HTML syntax.
Beginners sometimes write <image>, which is not a valid HTML tag. Always use <img>. See the image vs img guide for details.
Attributes such as src, alt, width, and height tell the browser where to fetch the file, how to describe it for accessibility, and how much space to reserve on the page.
Include an img element with src pointing to the image file and alt describing its content:
<img src="photo.jpg" alt="Description of the image">img is a void element — no </img> closing tag in HTML.src is required (unless using lazy-loading placeholders with data-src patterns).alt is required for accessibility — use alt="" only for purely decorative images.width and height to reduce cumulative layout shift (CLS).picture when you need fallbacks.| Use Case | Code Snippet | Notes |
|---|---|---|
| Basic image | <img src="photo.jpg" alt="..."> | Minimum viable markup |
| With dimensions | width="300" height="200" | Prevents layout jump |
| Lazy loading | loading="lazy" | Defers off-screen images |
| Responsive | srcset="..." sizes="..." | Multiple resolutions |
| Image link | <a><img ...></a> | Clickable thumbnail |
| Decorative | alt="" | Screen readers skip it |
<img> vs <image>Only one of these is valid for HTML page content:
| Feature | <img> | <image> |
|---|---|---|
| Valid in HTML | Yes | No |
| Renders pictures | Yes | No |
| Standard element | Core HTML since early versions | Common beginner mistake |
| Learn more | This tutorial | image misconception guide |
<img> vs CSS background-imageChoose based on whether the image carries semantic meaning:
| Approach | Best for | Accessibility |
|---|---|---|
<img> | Content images, logos, diagrams | alt text |
| CSS background | Pure decoration, textures, hero overlays | No native alt text |
<figure> + img | Images with captions | figcaption adds context |
<picture> | Art direction, format fallbacks | Uses img with alt inside |
Essential <img> attributes plus global attributes such as class and id:
src RequiredURL or path to the image file (JPEG, PNG, WebP, SVG, GIF, etc.).
src="photo.jpg"alt RequiredAlternative text for accessibility and when the image fails to load.
alt="Sunset over the ocean"width / height LayoutIntrinsic dimensions in pixels to reserve space and reduce layout shift.
width="300" height="200"srcset / sizes ResponsiveOffer multiple image resolutions for different viewports and pixel densities.
srcset="large.jpg 2x"loading PerformanceDefer off-screen images with loading="lazy".
loading="lazy"decoding PerformanceHint decode timing: async, sync, or auto.
decoding="async"<img
src="photo.jpg"
alt="Mountain landscape at sunrise"
width="300"
height="200"
loading="lazy"
decoding="async"
>Do not use the filename as alt text. Describe what the image communicates.
Single images, clickable thumbnails, and responsive patterns with copy-ready code and live previews.
Standalone image embedded with the img element:

The primary use of <img> is to display standalone images within page content.
<p>Check out this logo:</p>
<img src="/images/javascript.png" alt="JavaScript logo" width="128" height="128">Use <img> for logos, product photos, avatars, diagrams, and icons. Wrap in <a> for clickable thumbnails, or in <figure> when a caption is needed.
Turn an image into a clickable link by wrapping the img element with an a anchor tag.
<a href="/html">
<img src="thumbnail.jpg" alt="Go to HTML tutorials">
</a>Ensure images scale properly across devices with srcset, sizes, and CSS.
<img
src="small.jpg"
srcset="large.jpg 1024w, medium.jpg 640w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="Responsive image"
loading="lazy"
>Control sizing, cropping, and layout of images with CSS while keeping semantic img markup:
max-width: 100% Fluid responsive imagesobject-fit: cover Crop to fixed boxborder-radius Rounded avatarsaspect-ratio Consistent proportions/* Responsive by default */
img {
max-width: 100%;
height: auto;
display: block;
}
img.avatar {
width: 64px;
height: 64px;
border-radius: 50%;
object-fit: cover;
}
img.hero {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
border-radius: 12px;
}Rounded img preview

Images must be usable for everyone, including assistive technology users:
alt="" so screen readers skip purely visual decoration.alt should describe the link destination, not just the picture.Point src at the image file and describe it with alt.
width and height prevent layout from jumping while the file downloads.
The browser decodes and paints the image in the document flow at its natural or styled size.
Users see the picture; assistive tech reads the alt text when needed.
The <img> element is fully supported in all browsers, including legacy Internet Explorer.
From legacy Internet Explorer to the latest mobile browsers — embed images with confidence using img.
Bottom line: The <img> element is one of the most universally supported HTML tags in production use today.
Understanding the HTML <img> tag is essential for integrating images into web pages. Whether displaying single images, creating clickable thumbnails, or building responsive layouts, img is the standard, versatile tool for visual content.
Always include meaningful alt text, set dimensions to prevent layout shift, optimize file sizes, and choose the right format for each use case.
alt text on every content imagewidth and height to reduce layout shiftloading="lazy" for below-the-fold images<image> instead of <img>alt or use filenames as alt text<img>Bookmark these before you ship — they’ll keep your images fast and accessible.
<img> is the correct HTML element for pictures.
No closing tag required in HTML syntax.
SyntaxDescribe images for accessibility and SEO.
AccessibilityWidth and height prevent layout shift.
PerformanceWrap img in a for clickable thumbnails.
Works in every browser, including legacy IE.
Compatibilitysrc for the file URL and alt for alternative text.img is a void element. In HTML you write it without a closing tag. A trailing slash is optional in XHTML-style syntax.srcset and sizes, CSS max-width: 100%, and optionally the picture element for art direction.Try single images, clickable thumbnails, and responsive srcset in the interactive editor.
6 people found this page helpful