CSS image rules ensure photographs, product thumbnails, and user avatars fit neatly inside cards across every screen size without stretching or overflowing. Four core properties—max-width, height: auto, aspect-ratio, and object-fit—eliminate distorted graphics across modern web layouts.
For editorial article photos, keep original proportions intact with max-width: 100%; height: auto; display: block;. Setting display: block; also removes the tiny 4px baseline descender gap that appears underneath inline images inside bordered cards.
When building uniform card grids or circular avatars where every thumbnail must share identical dimensions, combine width: 100%; aspect-ratio: 16 / 9; with object-fit: cover; so the image fills the frame neatly without squishing faces or text.
img.fluid-photo {
display: block;
max-width: 100%;
height: auto;
border-radius: 8px;
}
img.card-cover {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: center;
}
img.muted-logo {
filter: grayscale(100%) opacity(0.75);
transition: filter 0.2s ease;
}
img.muted-logo:hover {
filter: none;
}
By default, object-fit: cover crops equally from all edges (50% 50%). For portrait headshots, add object-position: center 20%; so tall photos never slice off the top of a person's head.
width and height pixels without object-fit: Mixing mismatched aspect ratios stretches and warps people and product photos.width: 100% on tiny icons or badges: width: 100% blows a 40px badge up to 600px blurry pixels; use max-width: 100% instead.img as default display: inline inside tight card frames: Unexplained 4px gaps below images come from inline baseline alignment—fix with display: block.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.
No. CSS filter modifies only how the browser paints the element visually on screen without touching the underlying file.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.