Skip to content

Adding Images

To display an image in an HTML page, we use the self-closing <img /> tag.

<img src="path/to/image.jpg" alt="image description" />

To define the source of the image, you can use:

<img src="https://placekitten.com/408/287" alt="A cute gray cat" />
<img src="/images/my-favorite-cat.jpeg" alt="My favorite cat" />
<img src="./pictures/my-avatar.png" alt="Profile avatar" />

Essential for:

  • Accessibility (screen readers)
  • SEO (search engines)
  • When the image fails to load
  • Be clear and precise
  • Describe the content, not the type (“image of…” → avoid)
  • Don’t overload with SEO keywords
  • If the image is purely decorative: alt=""
<img src="machu.jpg" alt="Machu Picchu in Peru at sunrise" />

You can define the display size of the image with width and height.

<img src="photo.jpg" width="300" height="400" />
Warning

If you specify only one attribute, the other will be calculated automatically. Don’t change the original aspect ratio, otherwise the image will be distorted.

Don’t load a 4000×3000 px image to display it at 300×200 px.
Optimize your images to the right size before inserting them!


  • If it’s purely decorative, use CSS (e.g., background-image) instead of an <img /> tag.
  • HTML is meant for structured information.

<img
src="https://placekitten.com/300/300"
alt="Gray cat sitting in a basket"
width="300"
/>

ElementRole
<img />Display an image
srcDefine the image source
altDescribe the image (accessibility + SEO)
width / heightDefine display size
alt=""For decorative images

Bonus tip

To test easily, use services like https://placekitten.com/ or https://picsum.photos/ to generate temporary placeholder images.