Question
I am working with images and ran into a problem related to aspect ratios.
<img src="big_image.jpg" width="900" height="600" alt="">
The image already has width and height attributes. I then added this CSS:
img {
max-width: 500px;
}
For big_image.jpg, the displayed size becomes width: 500px, but the height stays 600px. How can I resize images so they scale correctly while keeping their original aspect ratio?
Short Answer
By the end of this page, you will understand how browsers size images, why an image can become distorted when only one dimension changes, and how to use CSS such as max-width: 100% and height: auto to resize images while preserving aspect ratio.
Concept
When an image is resized, the browser needs to know how to calculate its width and height.
An image has an intrinsic aspect ratio, which is the relationship between its width and height. In this example:
- width =
900 - height =
600 - aspect ratio =
900 / 600 = 1.5
If you shrink the width, the height should shrink proportionally too. Otherwise, the image becomes stretched or squashed.
In HTML, the width and height attributes provide the browser with dimensions. In CSS, if you set only max-width, the browser may limit the width but still keep the explicit height from the attributes unless you also allow the height to adjust automatically.
The usual fix is:
img {
max-width: 500px;
height: auto;
}
height: auto tells the browser: “calculate the height based on the current width and the image’s aspect ratio.”
This matters in real programming because responsive layouts often need images to scale to fit containers, cards, articles, product pages, and mobile screens without distortion.
Mental Model
Think of an image like a printed photo in a frame.
- If you make the frame narrower but keep the old height, the photo gets squeezed.
- If you resize both dimensions proportionally, the photo keeps its original shape.
So:
widthcontrols horizontal sizeheightcontrols vertical sizeheight: automeans “adjust the height naturally to match the width”
Syntax and Examples
The most common pattern for preserving aspect ratio is:
img {
max-width: 500px;
height: auto;
}
Example 1: Fixed maximum width
<img src="big_image.jpg" width="900" height="600" alt="Landscape photo">
img {
max-width: 500px;
height: auto;
}
If the image is larger than 500px wide, it will shrink down to 500px and the height will be calculated automatically.
For a 900x600 image:
- new width =
500px - new height =
600 × (500 / 900) - new height ≈
333px
Example 2: Responsive images inside a container
Step by Step Execution
Consider this HTML and CSS:
<img src="big_image.jpg" width="900" height="600" alt="Example image">
img {
max-width: 500px;
height: auto;
}
Here is what happens step by step:
- The browser reads the image element.
- It sees the original dimensions:
900x600. - It calculates the image’s aspect ratio:
900 / 600 = 1.5. - The CSS says the image cannot be wider than
500px. - Since
900pxis larger than500px, the browser scales the width down to500px. - Because
height: autois set, the browser calculates the matching height using the original ratio. - The new height becomes approximately
333px. - The image is displayed at
500x333, keeping the original shape.
Real World Use Cases
Image resizing with preserved aspect ratio is used in many common situations:
- Responsive websites: article images scale down on phones and tablets
- Product cards: store listings show smaller versions of large product photos
- User uploads: profile pictures or gallery images must fit within layout limits
- Blog content: large images should not overflow the content column
- Admin dashboards: thumbnails need to fit containers without looking stretched
- Email templates: images often need to shrink to smaller screens safely
Example in a content area:
.article-content img {
max-width: 100%;
height: auto;
}
This ensures inserted images never break the article layout.
Real Codebase Usage
In real projects, developers usually do more than just resize a single image.
A common pattern is:
img {
max-width: 100%;
height: auto;
display: block;
}
Why this is common:
max-width: 100%prevents overflowheight: autopreserves aspect ratiodisplay: blockremoves extra inline spacing issues in some layouts
Common usage patterns
1. Responsive content images
.content img {
max-width: 100%;
height: auto;
}
2. Constrained thumbnails
.thumbnail img {
max-width: 200px;
height: auto;
}
3. Utility class approach
.img-fluid {
: ;
: auto;
}
Common Mistakes
1. Setting only max-width and forgetting height: auto
img {
max-width: 500px;
}
This may not be enough if another height value is still being applied.
Fix:
img {
max-width: 500px;
height: auto;
}
2. Forcing both width and height
Broken code:
img {
width: 500px;
height: 600px;
}
This ignores the original proportions and can distort the image.
Fix:
img {
width: 500px;
height: auto;
}
3. Using max-width: 100% without understanding the parent width
{
: ;
: auto;
}
Comparisons
| Approach | What it does | Keeps aspect ratio? | Common use |
|---|---|---|---|
width: 500px; height: auto; | Sets exact width and calculates height | Yes | When you want a fixed width |
max-width: 500px; height: auto; | Shrinks large images but does not force smaller ones to grow | Yes | When images should have an upper size limit |
max-width: 100%; height: auto; | Makes image responsive to parent width | Yes | Responsive layouts |
width: 500px; height: 500px; | Forces both dimensions | No | Only when intentional cropping/stretching is acceptable |
object-fit: cover; |
Cheat Sheet
/* Safe responsive image pattern */
img {
max-width: 100%;
height: auto;
}
/* Limit image to a maximum width */
img {
max-width: 500px;
height: auto;
}
/* Fixed width with preserved ratio */
img {
width: 300px;
height: auto;
}
Rules to remember
- Use
height: autoto preserve aspect ratio. - Use
max-widthwhen you want a size limit. - Use
max-width: 100%for responsive layouts. - Avoid setting both width and height unless you intentionally want a fixed box.
- HTML
widthandheightattributes do not prevent CSS from resizing the image.
Common formula
new height = original height × (new width / original width)
Good default for many projects
FAQ
Why does my image stretch when I change only the width?
Because the height may still be fixed by HTML attributes or another CSS rule. Set height: auto so the browser recalculates the height proportionally.
Should I use width or max-width for images?
Use max-width when you want the image to shrink only if needed. Use width when you want a specific width every time.
Is max-width: 100% the best option for responsive images?
Yes, it is one of the most common and useful patterns. Pair it with height: auto.
Do HTML width and height attributes break responsive images?
No. CSS can still resize the image. In fact, keeping those attributes can help reduce layout shift while loading.
What if I want all images to fit a fixed box?
Use a set width and height with object-fit, for example:
img {
width: 200px;
height: 200px;
object-fit: cover;
}
Mini Project
Description
Build a simple responsive photo gallery where large images never overflow their container and always keep their original proportions. This project demonstrates the most common real-world image resizing pattern used in articles, dashboards, and gallery layouts.
Goal
Create a small gallery in which images scale down neatly inside cards while preserving aspect ratio.
Requirements
- Create a page with at least three images of different original sizes.
- Place each image inside a card-style container.
- Make sure images never overflow their container width.
- Preserve aspect ratio for every image.
- Add simple styling so the gallery is easy to inspect visually.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.