iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

HTML Images

The <img> element embeds an image in the page. It's a void element, so no closing tag.

A live SVG figure

You can embed bitmap images (.jpg, .png, .webp) via <img>, or place vector graphics directly inline with <svg>. SVG scales to any size without losing sharpness:

SVG = scalable + small file size
Fig 1. Inline SVG drawn from a handful of shapes — no image file needed.

Common <img> attributes

AttributePurposeRequired?
srcPath or URL of the image.Yes
altText alternative for screen readers and broken images.Yes (use alt="" for purely decorative images).
width / heightDimensions in pixels. Helps the browser reserve space.Recommended
loadinglazy defers off-screen images until the user scrolls near them.Recommended for long pages
srcsetDifferent image files for different screen densities.Optional
Accessibility: Every <img> needs an alt attribute. Describe what the image conveys, not what it looks like.
Tip: Use SVG for icons, logos, and diagrams. Use WebP/JPG for photographs. PNG only when you need transparency on a raster image.

Example

Example
<!DOCTYPE html>
<html>
<body>

<h2>HTML Image</h2>
<img src="https://placehold.co/300x150/04AA6D/FFF?text=iwantcoding.com" alt="Demo image" width="300" height="150">

</body>
</html>
Try it Yourself »

Exercise

Add the attribute that describes the image for screen readers.

<img src="cat.jpg" ="A cat sleeping in a sunbeam">

Test yourself

Q1. Which element embeds an image?
Q2. Which attribute is required for accessibility on images?
Q3. What does loading="lazy" do?

Discussion

Loading…