Sub topic 10
Cropping in the Browser with CSS
This chapter assumes you know basic HTML (img and its attributes), CSS selectors, width and height, and what a breakpoint is. You will use CSS to crop a photo to a fixed shape without editing the file, and you will keep the subject in view.
Why crop in CSS
A layout often needs a fixed shape, such as a 16:9 hero band, while photos arrive in many shapes. Cropping in CSS lets one source file fill the box. The browser hides the overflow, and the original file is not changed.
Three properties do the work:
aspect-ratiosets the shape of the box.object-fitdecides how the image fills that box.object-positiondecides which part of the image stays visible.
The basic recipe
.hero img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: 30% 40%;
}The image fills a 16:9 box. The browser scales it until the box is covered, then crops the overflow. object-position moves the visible area toward the subject instead of the center.
Choosing an object-fit value
| Value | Behavior | Typical use |
|---|---|---|
| `cover` | Fills the box, crops the overflow, keeps proportions | Heroes, cards, thumbnails |
| `contain` | Shows the whole image, leaves empty bands | Logos, product shots that must not be cut |
| `fill` | Stretches to the box and distorts | Almost never |
| `none` | Keeps the original size and crops | Rare, pixel-exact work |
Use cover for photos. Use contain when losing any edge would change the meaning.
Moving the focal point
object-position takes two values: horizontal, then vertical. The default is 50% 50%, the center. The values describe where the image is anchored in the box.
0% 0%keeps the top-left corner visible.100% 0%keeps the top-right corner visible.30% 40%favors a subject left of center and slightly above the middle.
Keywords work as well, for example object-position: top; for a portrait where the face sits near the top edge.
Common errors
- The subject is cut off. The default center crop missed it. Adjust
object-position, and check the result at every width where the box shape changes. - The photo looks stretched. Something set both
widthandheightwithobject-fitleft at its defaultfill. Addobject-fit: cover. - The box has no height.
aspect-rationeeds a width. Give the image or its container a width, or the ratio has nothing to work from. - A fixed height crops badly on small screens. A height in pixels gives a different shape at each width. Prefer
aspect-ratio.
Check the crop
Resize the browser from wide to narrow, and watch the subject, not the layout. Ask whether the face, product, or key object stays fully visible at each width. If not, change object-position or the aspect-ratio.
Exercise
An image uses width: 100%; aspect-ratio: 1 / 1; object-fit: cover;. The photo is a wide landscape, and a person standing at the far left edge is cut off. Which property do you change, and to what kind of value?
Show answer
Change object-position and set the horizontal value near 0%, for example object-position: 10% 50%;. This anchors the left side of the photo in the box, so the person at the left stays visible.