CSS Images
CSS Images: Responsive Scaling, Cropping, and Filters
CSS image rules keep photos, product thumbnails, and avatars fitting neatly inside cards on every screen size, without stretching or overflowing. Four properties do most of the work: max-width, height: auto, aspect-ratio, and object-fit. This guide shows when to use each one, how to control the crop, and how to add visual effects with CSS filters.
How CSS Images Stay Responsive
For article photos where the original proportions should stay intact, use this:
img.fluid-photo {
display: block;
max-width: 100%;
height: auto;
border-radius: 8px;
}max-width: 100%stops the image from growing wider than its container, and lets it shrink on small screens.height: autoKeeps the proportions as the width changes.display: blockRemoves the small gap that appears under inline images, because inline images sit on the text baseline. The gap is usually a few pixels, depending on the font.
Also set width and height attributes in the HTML:
<img class="fluid-photo" src="photo.jpg" alt="Team at the launch event" width="1200" height="800">The browser then knows the image's shape before it loads and reserves space for it, which reduces layout shift. The CSS above still makes it fluid. And always write meaningful alt text.
Uniform Thumbnails with aspect-ratio and object-fit
In card grids, every thumbnail should share the same shape, even when the source photos differ. Combine a fixed ratio with object-fit: cover:
img.card-cover {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: center;
}aspect-ratio to set the box shape from the width. It only works if you don't also set a fixed height.
object-fit: cover scales the image to fill the box and crops the overflow, so nothing is squished.
This pairs well with a CSS Grid card layout, where each card holds one cover image.
CSS Image Filters
The filter property changes how an image is painted, with effects such as grayscale(), blur(), and opacity():
img.muted-logo {
filter: grayscale(100%) opacity(0.75);
transition: filter 0.2s ease;
}
img.muted-logo:hover {
filter: none;
}This mutes a logo until the visitor hovers over it. Filters only affect how the browser draws the image on screen. They don't change the image file. If you need to overlay a badge on an image, see our CSS position lesson.
Common Mistakes to Avoid
Setting both a fixed width and height without
object-fit. If the ratios don't match, the image stretches and warps. Addobject-fit:cover orcontain.Using
width: 100%on small icons or badges. It scales them up to the container's width and makes them blurry. Usemax-width: 100%instead.Leaving images as
display: inlinein tight frames. The unexplained gap under the image comes from baseline alignment. Usedisplay: block.Setting
heightalongsideaspect-ratio. A fixed height overrides the ratio.
Conclusion
For CSS images, use max-width: 100% and height: auto for fluid photos, aspect-ratio with object-fit: cover for uniform thumbnails, object-position to protect faces, and filter for visual effects. Add width and height attributes in the HTML to reduce layout shift, and explore more in our free CSS tutorial.
Frequently Asked Questions
What is the difference between object-fit: cover and contain?
cover crops excess edges to fill the entire box with zero empty bands, whereas contain fits the entire uncropped photo inside the box, leaving letterbox space if ratios differ.
Do CSS filter effects like blur() or grayscale() alter the image file?
No. CSS filter modifies only how the browser paints the element visually on screen without touching the underlying file.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.