HTML

How to Add Images in HTML the Right and Optimal Way

How to use the HTML <img> tag properly: alt text, width and height, modern formats and lazy loading, so your images stay fast, accessible and SEO-friendly.

RazakTerra
Reading time
2 min read
Comments
Discussion
How to Add Images in HTML the Right and Optimal Way
On this page
  1. Basic Image Syntax
  2. Why the alt Attribute Matters
  3. Setting Width and Height
  4. Lazy Loading for Better Performance
  5. Quick Tips

Basic Image Syntax

HTML
<img src="images/photo.jpg" alt="A description of the image" />
  • src → the path or URL of the image (required)
  • alt → alternative text that describes the image (very important)

Why the alt Attribute Matters

The alt text is shown if the image fails to load. It also helps:

  • Screen readers describe the image to visually impaired users
  • Search engines understand what the image is about
HTML
<!-- Good -->
<img src="cat.jpg" alt="Orange cat sleeping on a sofa" />

<!-- Bad -->
<img src="cat.jpg" alt="image" />
<img src="cat.jpg" />

If the image is purely decorative, you can use an empty alt:

HTML
<img src="decoration.png" alt="" />

Setting Width and Height

Always include width and height when possible. This helps the browser reserve space and prevents layout shifts while the image loads.

Playground
<img src="https://i.pinimg.com/736x/cb/5c/4b/cb5c4b83975929d9866454bcc3d7d29e.jpg" 
height="300" 
width="300"
alt="Lazy cat"
></img>
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

You can still control the display size with CSS later. The important thing is telling the browser the original dimensions.

Lazy Loading for Better Performance

Lazy loading tells the browser to only load images when they’re about to enter the viewport. This makes the page load faster.

Playground
<img
  src="https://i.pinimg.com/1200x/90/63/60/906360fe7313197f5a060249ff10a182.jpg"
  alt="Cat Bro"
  width="300"
  height="300"
  loading="lazy"
/>
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

Use loading="lazy" for images that are lower on the page. For important above the fold images (like a hero image), it’s usually better to load them normally.

Quick Tips

Always write meaningful alt text. Include width and height to avoid layout shifts. Use loading=“lazy” for images below the fold. Prefer modern formats like WebP when possible (with fallbacks if needed). Don’t use huge image files, compress them before uploading. Avoid using images for text that should be real HTML text.

By using the <img> tag correctly with alt, dimensions, and lazy loading, your images will look good, load faster, and be friendlier for both users and search engines.

Written by

Razak

Writes about the web, slowly, and builds small tools on the side.

Discussion

Comments

Loading comments…

Keep reading