How to Resize an Image in CSS
Use CSS width and height properties when you need an image to fit a card, hero section, avatar, gallery, or responsive layout. The safest default is to control the width and let the height scale automatically.
What you’ll build or solve
You’ll learn how to resize an image in CSS without stretching it. You’ll also know how to keep the aspect ratio correct across different layouts and screen sizes.
Learn CSS on Mimo
When this approach works best
This approach is the right choice when images appear too large, too small, or inconsistent across a layout.
Common real-world scenarios include:
- Hero banners
- Card thumbnails
- Profile images
- Product galleries
- Blog cover images
This is a bad idea when the image file itself needs permanent editing. In that case, resize the asset before uploading it.
Prerequisites
You only need:
- A basic HTML file with an image
- A text editor
- Basic HTML and CSS knowledge
Step-by-step instructions
Step 1: Set width and keep height automatic
Use width to control the size and height: auto to preserve the aspect ratio.
HTML
<img class="hero-image" src="usa-team.jpg" alt="USA team">
<style>
.hero-image {
width: 400px;
height: auto;
}
</style>
For responsive layouts, use percentage width instead.
HTML
<img class="cover-image" src="france-product.jpg" alt="France product">
<style>
.cover-image {
width: 100%;
max-width: 600px;
height: auto;
}
</style>
What to look for:
height: autoprevents stretching- Fixed pixel width works for controlled components
- Percentage width works better for responsive layouts
max-widthprevents oversized images- Resize the container too if the layout still feels off
Examples you can copy
Card thumbnail
HTML
<img class="card-image" src="uk-course.jpg" alt="UK course">
<style>
.card-image {
width: 300px;
height: auto;
}
</style>
Responsive hero image
HTML
<img class="hero" src="usa-hero.jpg" alt="USA pricing">
<style>
.hero {
width: 100%;
max-width: 900px;
height: auto;
}
</style>
Avatar image
HTML
<img class="avatar" src="mentor.jpg" alt="France mentor">
<style>
.avatar {
width: 80px;
height: 80px;
}
</style>
Common mistakes and how to fix them
Mistake 1: Setting both width and height with unrelated values
What the reader might do:
HTML
.image {
width: 400px;
height: 100px;
}
Why it breaks: the image stretches because the proportions no longer match the original file.
Corrected approach:
HTML
.image {
width: 400px;
height: auto;
}
Mistake 2: Using a fixed width on responsive layouts
What the reader might do:
HTML
.hero {
width: 1200px;
}
Why it breaks: the image can overflow smaller screens.
Corrected approach:
HTML
.hero {
width: 100%;
max-width: 1200px;
height: auto;
}
Mistake 3: Forgetting the container width
What the reader might do:
HTML
.image {
width: 100%;
}
Why it breaks: the image may become much larger than intended if the parent container is too wide.
Corrected approach:
HTML
.container {
max-width: 600px;
}
.image {
width: 100%;
height: auto;
}
Troubleshooting
If the image looks stretched, switch height to auto.
If the image overflows mobile screens, replace fixed widths with percentages and max-width.
If the image still feels too large, reduce the parent container width.
If avatars need exact squares, set equal width and height.
Quick recap
- Use
widthto resize images - Keep
height: autofor correct proportions - Use
%andmax-widthfor responsive layouts - Avoid unrelated width and height values
- Resize the container when needed
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot