Close Menu

How to Set an Explicit Width and Height on Image Elements

Learn how to set explicit width and height on images.

Images are often the heaviest assets on a webpage, and improper handling can lead to slow loading times, layout shifts, and a generally poor user experience.

One common mistake that developers make is neglecting to set explicit width and height attributes on image elements.

Although it might seem trivial, defining these dimensions is crucial for maintaining the structure and performance of your site.

In this article, we’ll explore why setting explicit width and height attributes on image elements is essential, how it can prevent common issues like layout shifts, and best practices for implementing this in your web development projects.

Why Setting Explicit Width and Height Is Important

Prevents Layout Shifts

When a webpage loads, the browser progressively renders elements. If you don’t define the width and height of an image, the browser doesn’t know how much space to allocate for it until the image fully loads.

This can cause something called “layout shift”, where the content around the image moves or jumps as the image loads. Layout shifts can be disorienting for users, leading to a frustrating experience, especially on slower networks.

By setting the width and height attributes, you reserve the exact space that the image will occupy, ensuring a stable layout as the page loads.

Improves Loading Performance

Modern browsers can start downloading images as soon as they encounter the <img> tag.

If the browser knows the dimensions of the image beforehand, it can optimise the loading process by allocating the correct space in the layout, reducing the need for reflow and repaint processes.

This makes the page load faster, particularly on mobile devices where performance is often constrained by network conditions.

Enhances User Experience

A smooth and predictable layout is key to a good user experience. When images load seamlessly without causing other elements to shift, users can interact with the content more comfortably.

This is particularly important for e-commerce sites, blogs, and any content-heavy site where users engage with visual content.

Moreover, setting explicit dimensions helps with responsive design, ensuring that images scale appropriately across different devices without causing unexpected layout issues.

SEO Benefits

Google and other search engines take page speed and user experience into account when ranking websites.

A site that has optimised images and avoids layout shifts is likely to perform better in search rankings.

By setting explicit width and height attributes, you contribute to a faster, more stable site, which can indirectly boost your SEO efforts.

How to Set Width and Height in HTML

Setting explicit width and height attributes on image elements is straightforward in HTML. You simply add width and height attributes directly within the <img> tag. Here’s a basic example:

<img src="/path-to-image/image.jpg" width="600" height="400" alt="A person typing at a laptop">

In this example, the image has a width of 600 pixels and a height of 400 pixels. These dimensions should ideally match the actual dimensions of the image file or the aspect ratio you want to maintain.

This way, the browser knows exactly how much space to allocate for the image as soon as it encounters the <img> tag.

Responsive Images

In responsive web design, you often want images to scale based on the size of the viewport while maintaining their aspect ratio.

While you can still set explicit dimensions, you might want to use CSS to control the image’s behavior on different screen sizes. You can either write this inline:

<img src="/path-to-image/image.jpg" width="600" height="400" alt="A person typing at a laptop" style="max-width: 100%; height: auto;">

or use your CSS stylesheet:

<img src="/path-to-image/image.jpg" width="600" height="400" alt="A person typing at a laptop" class="banner-image">
.banner-iamge {
max-width: 100%; 
height: auto;
}

In this case, the width and height attributes provide the browser with the image’s intrinsic dimensions, preventing layout shifts.

Meanwhile, the CSS max-width: 100%; ensures that the image scales down appropriately on smaller screens but still remains full width, and height: auto; maintains the aspect ratio.

Practical Considerations

Aspect Ratio

When setting width and height, it’s important to maintain the aspect ratio of your images to avoid distortion.

The aspect ratio is the proportional relationship between the width and height of an image.

If your image is 600×400 pixels, that’s an aspect ratio of 3:2. To keep this ratio intact across different screen sizes, make sure your CSS respects this relationship (e.g., using height: auto; in combination with a width setting).

Using Modern HTML and CSS Features

With the advent of modern web standards, tools like the aspect-ratio CSS property allow developers to enforce aspect ratios directly in CSS without needing to hard-code specific dimensions in HTML.

This can be particularly useful in responsive designs:

img {
    aspect-ratio: 3 / 2;
    width: 100%;
    height: auto;
}

This approach gives you flexibility while still benefiting from the layout stability that explicit dimensions provide.

Handling Images in Different Contexts

If you’re working with images in responsive containers, such as within flexbox or grid layouts, setting explicit dimensions can still be beneficial.

However, you’ll likely need to rely more on CSS for scaling and positioning. It’s also important to test across different devices and browsers to ensure consistent behavior

Setting explicit width and height attributes on image elements might seem like a minor detail in web development, but its impact on performance and user experience is significant.

By defining these attributes, you prevent layout shifts, speed up loading times and contribute to a more polished, professional website.

HowCodingWorks Logo