In the ever-evolving landscape of web development, few challenges are as persistent as the tension between legacy HTML structures and the fluid requirements of modern responsive design. As developers strive to satisfy both the aesthetic demands of human users and the rigid technical requirements of search engine algorithms, one specific element has emerged as a focal point of this struggle: the <img> tag.
Recent industry discourse, highlighted by studies on Cumulative Layout Shift (CLS)—a core pillar of Google’s Core Web Vitals—has underscored a critical best practice: declaring explicit width and height attributes in your HTML. However, implementing this advice in a responsive world is not as straightforward as it seems. Without the right CSS strategy, these attributes can lead to distorted imagery and broken layouts. The solution, while simple, requires a fundamental shift in how we approach the relationship between markup and styling.
Main Facts: The Intersection of Performance and Presentation
The central conflict arises from the disparate roles of HTML and CSS. HTML provides the structural blueprint of a document, while CSS provides the visual presentation. When we add width and height attributes to an <img> tag, we are telling the browser how much space to reserve for that image before the image file itself is even downloaded. This is the cornerstone of preventing layout shifts—the jarring "jumping" effect users experience when elements move as a page loads.
However, modern design mandates that images be fluid. We use max-width: 100% or other percentage-based constraints to ensure images scale down on mobile devices. Historically, developers feared that setting an explicit height in the HTML would force the browser to respect that value, even if the width was constrained, resulting in a squashed or stretched image.
The core fact remains: Performance requires explicit dimensions, but responsiveness requires fluidity. Reconciling these two needs is not a matter of choosing one over the other, but of understanding how the browser’s rendering engine interprets the height: auto property.

Chronology: The Evolution of Image Handling
To understand why this is a modern concern, we must look at the history of web layout techniques.
The Era of Fixed Layouts (Pre-2010)
In the early days of the web, "fixed-width" designs were the standard. Developers knew the exact pixel width of their containers. Images were sized precisely to fit those containers, and HTML attributes were treated as authoritative. Layout shifts were an accepted side effect of a slower internet, and search engine optimization (SEO) was less concerned with user experience metrics.
The Responsive Revolution (2010–2015)
Ethan Marcotte’s introduction of "Responsive Web Design" changed the industry. Suddenly, layouts were fluid. The industry-wide adoption of img max-width: 100%; height: auto; became the de facto standard. At this stage, many developers began omitting width and height attributes from HTML entirely, believing that letting the browser "figure it out" was the cleaner approach for fluid design.
The Core Web Vitals Shift (2020–Present)
Google’s introduction of Core Web Vitals changed the stakes. CLS became a direct ranking factor. Search engines began penalizing sites that did not provide aspect-ratio information in the markup. This created a paradoxical situation: developers who had abandoned HTML attributes for the sake of responsive CSS were suddenly being told they were hurting their SEO performance. We have now entered an era where we must provide the metadata (HTML attributes) while maintaining the fluidity (CSS).
Supporting Data: Why Dimensions Matter
The data on user behavior is unambiguous: performance is synonymous with user retention. According to performance studies by organizations like Request Metrics, even a minor delay in page rendering—often caused by layout shifts—leads to a measurable drop in user engagement.

When an image loads without defined dimensions, the browser has no way of knowing the aspect ratio until the first bytes of the image file are parsed. If a browser renders a text block and then suddenly pushes that text down to accommodate an image that just finished loading, the user loses their place. This "layout shift" is more than just an aesthetic annoyance; it is a primary indicator of poor technical health.
By including width="728" and height="90" (as seen in standard banner implementations), the browser can calculate the aspect ratio (in this case, 8.08:1) immediately. It reserves that exact amount of vertical space. Even if the image eventually scales down to 300px wide, the reserved space scales proportionally, maintaining the correct aspect ratio and preventing the layout from jumping.
Official Guidance and Best Practices
Industry experts and browser engine developers (such as the Chromium team) are in lockstep regarding this implementation. The guidance is consistent:
- Always include
widthandheightattributes. These should reflect the "intrinsic" size of the image file. - Use CSS to enforce responsiveness. Do not rely on the HTML attributes to define the final visual size.
- The "Auto" override. By setting
height: autoin your stylesheet, you instruct the browser to ignore the static height attribute in the HTML once the image starts scaling, while still utilizing the aspect ratio derived from the HTML attributes to maintain the element’s container shape.
The Code Implementation
The implementation is elegant in its simplicity. By placing this in your global CSS, you ensure that no image, regardless of its HTML-defined dimensions, will break its container:
img
/* Prevent the image from exceeding the width of its parent container */
max-width: 100%;
/* Reset the height to scale proportionally based on the intrinsic aspect ratio */
height: auto;
/* Ensure the image doesn't overflow its boundaries */
display: block;
This approach allows for a "best of both worlds" scenario. The browser receives the explicit instruction to preserve the layout structure, while the CSS engine retains the freedom to resize the element as the viewport changes.

Implications for Future Development
The implications of this simple technical adjustment are significant for the broader web ecosystem.
SEO and Ranking
Search engine crawlers now prioritize sites that provide a seamless loading experience. By adopting this standard, developers are not just writing "cleaner" code; they are actively optimizing their sites for higher search engine placement. In a competitive digital landscape, these micro-optimizations are the difference between the first and second page of search results.
User Experience (UX)
The primary beneficiary is, of course, the user. A stable, predictable layout reduces cognitive load. Users can begin reading content or interacting with navigation menus immediately, without fear that the page will "shift" under their cursor. This is particularly vital for mobile users on high-latency connections, where the time between "text load" and "image load" can be substantial.
Maintenance and Scalability
For development teams, this methodology simplifies maintenance. Rather than creating complex JavaScript loaders or using aspect-ratio CSS properties (which, while powerful, have their own browser compatibility considerations), the standard width/height + height: auto pattern is robust, universally supported, and easy for new developers to understand.
Conclusion
The "dance" between HTML and CSS is one of the most fundamental aspects of web craftsmanship. While it is easy to view these two technologies as separate silos, the reality is that they are deeply interdependent. The <img> element serves as the perfect case study for this relationship.

By reclaiming the use of HTML dimensions and coupling them with the fluid power of CSS, we can create web experiences that are both performant and aesthetically flexible. It is a reminder that in the world of front-end development, the most effective solutions are often those that respect the original design of the web—using the right tool for the right job, and ensuring that those tools work in harmony rather than in conflict. As we continue to push the boundaries of what is possible on the web, let us not forget that the foundation of a great site is built on the stability of its layout and the responsiveness of its assets.

