Sub topic 8
Protecting the focal point
This chapter assumes you know the img element, basic CSS sizing, and how breakpoints work. It also assumes you have read the earlier chapters on aspect ratios and the principles of cropping.
What a focal point is
The focal point is the part of a photo that must stay visible: a face, a product, a loaf of bread. Everything else is context that you can lose if you have to.
When the browser fits an image into a box with a different shape, it crops the image. By default it crops evenly around the center. If your subject sits off-center, a centered crop can cut off the head, the product, or the hands.
Find the focal point before you crop
Do this for each photo before you write any CSS:
- Look at the photo and name the one thing that must stay visible.
- Estimate where that thing sits, as a percentage across and down the image.
- Check which crops you will need, such as 16:9 on desktop and 1:1 on phones.
- Ask whether the subject survives the tightest of those crops.
If the subject does not survive the tightest crop, moving the focal point will not help. You need a different crop file, which is covered in the art direction chapter.
Moving the visible area with object-position
object-fit: cover fills the box and crops the overflow. object-position decides which part of the image stays visible. Its two values are the horizontal and vertical position, as percentages.
.hero img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: 30% 40%;
}The image fills a 16:9 box and the browser crops the overflow. The 30% 40% value moves the visible area toward the subject, which sits left of center and slightly above it, instead of cropping around the center.
A percentage lines up the same percentage point of the image with that point of the box. So 0% shows the left or top edge, 50% is the center, and 100% shows the right or bottom edge.
Common errors
- Setting the position once and never testing it. A value that works at 16:9 can fail at 1:1, because the box shape changes which edges are cropped.
- Moving the wrong axis. If the image is wider than the box, only the horizontal value matters. If it is taller, only the vertical value matters. Changing the other value does nothing.
- Guessing the percentages. Resize the browser window and watch the subject, or use developer tools to edit the value live.
- Relying on
object-positionfor extreme crops. A wide landscape shown in a tall box leaves little room to move.
Check several widths
Test the page at three widths: a narrow phone, a tablet, and a wide desktop. At each width, confirm that the subject is fully visible and that no key detail touches the edge of the box.
Exercise
A photo shows a person standing in the right third of the frame. You display it with object-fit: cover in a square box, and the person's face is cut off. Which CSS property do you adjust, and in which direction do you move the horizontal value?
Show answer
Adjust object-position. The photo is wider than the square box, so the horizontal value controls the crop. Increase it, for example to 80% 30%, so the visible area moves toward the right side where the person stands.