Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use an <img> element with a valid image source, alternative text suited to the image’s purpose, and intrinsic dimensions. Add responsive sources or deferred loading only when they fit the image and page. The <img> element is a void element: it has no closing tag.

A correct basic image tag

A typical image that conveys information can be marked up like this:

<img
  src="/images/field-guide.jpg"
  alt="Red fox standing in a snowy field"
  width="1200"
  height="800"
  loading="eager"
/>

This is an illustrative example, not a tested implementation. Use the alt text only if it accurately describes the actual image and its role in the surrounding content. The width and height describe the image’s intrinsic dimensions, not necessarily its displayed size. See MDN’s <img> reference and HTML images guide.

Write alt text for the image’s purpose

The alt attribute provides a text replacement for an image’s content or purpose. Keep it concise and useful in context; don’t substitute a filename or generic wording such as “image.” An image that links somewhere should have alternative text that identifies the linked resource or destination.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
  • Informative image: Describe the relevant information the image contributes.
  • Decorative, supplemental, or redundant image: Use alt="" when the image adds no distinct information and would otherwise repeat nearby content.
  • Image used as a link: Make the alternative identify where the link goes or what resource it opens.

MDN recommends that an alt value provide “a clear and concise text replacement for the image’s content.” The right wording depends on context, not just what objects appear in the picture. See the WHATWG HTML Standard.

Set dimensions without distorting the image

Include both intrinsic width and height when known. They let the browser reserve space using the image’s aspect ratio before the file loads, helping reduce layout shifts. Control the rendered size with CSS and preserve the aspect ratio; don’t use the HTML attributes to stretch an image or as a substitute for appropriately sized image files.

MDN advises using both dimensions to let the image take up space before it loads, and cautions against using HTML attributes to resize images. See its <img> reference and HTML images guide.

Choose responsive images when they solve a real need

Use responsive image markup when different layouts or display densities benefit from different image files. The main options serve different purposes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Need Approach Key consideration
Basic embedded image src and contextual alt Communicate the image’s meaning or purpose in context.
Different file widths for responsive layouts srcset with w descriptors and sizes sizes describes the expected rendered slot so the browser can choose a candidate.
Same rendered size, different display pixel densities srcset with x descriptors This density-selection pattern does not need sizes.
Different crop or art direction by viewport <picture> with conditional <source> elements Keep an <img> fallback with appropriate alt text.

Width candidates for responsive layouts

Use width descriptors when you provide files at different pixel widths. Pair them with sizes, which tells the browser how wide the image is expected to appear at different layout conditions. For example, a width-based srcset without a useful sizes description does not fully describe the intended slot.

Density candidates for a fixed rendered size

Use density descriptors such as 1x and 2x when the image occupies the same layout size but you provide files for different display pixel densities. Don’t mix width (w) and density (x) descriptors in the same srcset.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Art direction or alternate formats

Use <picture> with conditional <source> elements when the image should change by viewport, such as to use a different crop, or when offering format choices. Keep the fallback <img> and give it alt text appropriate to the image’s meaning. MDN’s responsive images guide explains these patterns.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use lazy loading selectively

loading="lazy" can defer images that are outside the initial viewport, so they need not be loaded immediately alongside nearby content. Keep intrinsic dimensions so the browser can reserve their space. Don’t apply lazy loading indiscriminately: an image expected to be visible as the page opens should not be deferred automatically. See MDN’s <img> reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Avoid relying on the title attribute

Don’t put essential descriptions or instructions only in title. Support for the attribute is unpredictable, and it may appear only on mouse hover. Put important information in surrounding text or use appropriate alternative text for the image instead. See MDN’s <img> reference.

Check the image asset as well as the markup

Correct markup does not establish that you have permission to use an image. MDN’s HTML images guide advises authors to use images they own, have permission to use, or can use under the applicable license.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.