HTML & CSS Interview Question #17

How do you display images correctly in HTML?

HTML Fundamentals, Structure, Semantics, Links, Images, Tables & Forms Junior Beginner

Quick Interview Answer

Use the img element with a src attribute for the image resource and an alt attribute for text alternatives. Meaningful images should have useful alternative text that communicates the relevant purpose or content.

Detailed Explanation

Use the img element with a src attribute for the image resource and an alt attribute for text alternatives. Meaningful images should have useful alternative text that communicates the relevant purpose or content.

For purely decorative images, an empty alt attribute is normally appropriate so assistive technologies can ignore them. Width and height attributes can also reserve aspect-ratio space and reduce layout shifts while the image loads.

Code Example

<img src="/images/projector.jpg"
     alt="Portable projector on a desk"
     width="800"
     height="600">