Skip to content
About Blog Learn Explore Patterns Case studies
  • Home
  • All patterns
  • Images

<img> tag

Aug 17, 2021 — Updated Aug 17, 2021

Setting the width and height attributes on <img> tags helps prevent layout shifts. This information allows the browser to reserve the correct amount of space for the image.

  • Set the width and height attributes: The values of these attributes should be set to match the dimensions of the image itself (that is, its intrinsic size) - rather than dimensions that the image will be displayed at.

  • Adjust image styling as needed: Depending on the image's existing styling, adding width and height attributes may cause the image to render differently. In many cases, this can be fixed by adding height: auto or width: auto to the existing styling.

Previous CSS stylingChange to
img { width: 100%; }img { width: 100%; height: auto; }
img { max-width: 100%; }img { max-width: 100% height: auto; }
img { height: 100%; }img { height: 100%; width: auto }
<img width="267" height="400" src="dog.jpg">
Open demo
Last updated: Aug 17, 2021 — Improve article
Share
subscribe

Contribute

  • File a bug
  • View source

Related content

  • developer.chrome.com
  • Chrome updates
  • Case studies
  • Podcasts
  • Shows

Connect

  • Twitter
  • YouTube
  • Google Developers
  • Chrome
  • Firebase
  • Google Cloud Platform
  • All products
  • Terms & Privacy
  • Community Guidelines

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies.