CSS3 Images
Styling images with CSS means controlling how pictures size, crop, round and blend into your layout. An <img> arrives with its own natural width and height, which rarely fits your design. A handful of CSS properties make images responsive, keep their proportions, crop them neatly into fixed boxes, and apply effects like rounded corners or filters.
Making images fit their container
The first rule almost every stylesheet includes is:
img {
max-width: 100%;
height: auto;
display: block;
}
max-width: 100% stops an image from ever being wider than its parent, so a large photo shrinks on a phone instead of overflowing. height: auto keeps the aspect ratio when the width shrinks. Using max-width rather than width means small images, such as icons, are not stretched up to fill the container.
Adding width and height attributes in the HTML is still a good idea. Browsers use them to reserve the right amount of space before the image loads, which stops the page from jumping as pictures appear. The CSS above then overrides the actual display size.
Cropping without distortion using object-fit
Sometimes every thumbnail in a grid must be exactly the same size, but your photos have different shapes. Setting both a fixed width and height would squash them. object-fit controls how the picture fills its box:
| Value | Effect |
|---|---|
fill | Default. Stretches to fill, ignoring proportions. |
contain | Fits the whole image inside, leaving empty space if shapes differ. |
cover | Fills the box completely and crops the overflow. |
none | Keeps the natural size, cropping if needed. |
scale-down | Uses none or contain, whichever is smaller. |
object-position chooses which part stays visible when cropping, for example object-position: top to keep faces in portrait photos.
.gallery img {
width: 100%;
aspect-ratio: 4 / 3;
object-fit: cover;
border-radius: 8px;
transition: filter 0.2s, transform 0.2s;
}
.gallery img:hover {
filter: brightness(1.1);
transform: scale(1.02);
}
.logo-grey {
filter: grayscale(100%);
opacity: 0.8;
}
The aspect-ratio property gives each image a consistent shape based on its width, so you do not need to calculate heights for every screen size.
Effects: rounding, shadows and filters
border-radius rounds corners, and 50% on a square image makes a circle. box-shadow lifts an image off the page. The filter property applies effects such as grayscale(), blur(), brightness(), contrast() and sepia(), and several can be combined in one declaration. For decorative pictures, a CSS background-image with background-size: cover works similarly, but use a real <img> with alt text when the image carries meaning.
The mystery gap under images
By default, <img> is an inline element and sits on the text baseline. The browser leaves room below the baseline for letters like "g" and "y", which shows up as a few pixels of unexplained space under the image inside a card or link. Setting display: block on the image, or vertical-align: middle, removes it. If you see the gap only in some places, check whether those images are inside inline wrappers.
Key points
max-width: 100%; height: automakes images responsive.object-fit: coverplusaspect-ratiocreates uniform thumbnails without squashing.- Use
display: blockto remove the baseline gap.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.