Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to HTML Notes
Topic #404

Image width & height

The width and height attributes tell the browser an image's dimensions before it finishes downloading — letting it reserve the correct amount of space in the layout up front.

Syntax

<img src="photo.jpg" alt="..." width="800" height="600">

Basic Example

<img src="/assets/images/hero.jpg" alt="CodingNow campus" width="1200" height="600">

Browser output: the browser immediately reserves a 1200×600 box for this image in the page layout — even before a single byte of the actual file has arrived — so surrounding text and elements don't shift once it loads.

How It Works

  1. Without width/height, the browser doesn't know an image's size until the file has downloaded enough to read its dimensions — everything below it visually "jumps" into place once it does.
  2. With width/height set, the browser calculates the image's aspect ratio immediately and reserves exactly that much space, regardless of download speed.
  3. CSS can still resize the image visually (e.g. width: 100% in a responsive layout) — the HTML attributes are used to compute the correct aspect ratio, not to lock the final display size.

Important Attributes

AttributePurpose
widthThe image's intrinsic width, in pixels (a number, no unit)
heightThe image's intrinsic height, in pixels (a number, no unit)

Real-World Example

<style>
    img { max-width: 100%; height: auto; }
</style>
<img src="/assets/images/banner.jpg" alt="..." width="1600" height="900">

This is the modern recommended pattern: set the real width/height attributes in HTML for the correct aspect ratio, then let CSS's max-width: 100%; height: auto; handle actual responsive scaling — the browser combines both correctly.

Common Mistakes

  • Leaving out width/height entirely, causing visible layout shift as images load — a real, measurable problem tracked by Google's Core Web Vitals as "Cumulative Layout Shift" (CLS)
  • Setting width/height to a different aspect ratio than the actual image file, causing visible stretching or squashing

Best Practices

  • Always include width and height matching the image's real aspect ratio, even in a fully responsive layout
  • Let CSS handle the final display size — HTML's job here is just establishing the correct ratio

Accessibility Considerations

Layout shift caused by missing dimensions is especially disruptive for users with motor impairments or cognitive differences, who may click a button just as the page jumps and end up clicking something else entirely.

SEO Considerations

Cumulative Layout Shift is one of Google's three Core Web Vitals — a documented ranking signal — and missing image dimensions is one of the most common causes of a poor CLS score.

Browser & Modern HTML Notes

Modern browsers use the width/height attributes specifically to compute an image's aspect ratio ahead of load, even when CSS later overrides the rendered size — this browser behavior is a relatively recent (post-2020), genuinely useful convergence of HTML and CSS working together.

Mini Practice

  1. Add correct width/height attributes to an image, matching its real aspect ratio.
  2. Combine them with a CSS rule making the image fully responsive (max-width: 100%; height: auto;).

Related HTML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →