Sub topic 2
Aspect Ratios for Web Layouts
Assume you know basic HTML, CSS, responsive layouts, and breakpoints. An aspect ratio describes the relationship between an image’s width and height. A 16:9 image is wide, while a 1:1 image is square.
Aspect ratios and cropping
Choose an aspect ratio that fits the layout and the image’s purpose. A hero banner may use 16:9 or 3:1. A profile card may use 1:1. A portrait feature may use 4:5.
Cropping removes part of an image to fit a shape. It should remove less important background before removing the subject. Before cropping, identify the focal point: the person, object, or action that carries the image’s meaning.
Keep faces, hands, products, and other important details away from edges where small changes can cut them off. If text will sit over the image, leave a low-detail safe area for it. Do not place text over a face or a visually busy section.
Cropping in the browser with CSS
Use aspect-ratio to define the box shape. Use object-fit: cover when the image should fill that box, even though some pixels will be hidden. Use object-position to move the visible area toward the focal point.
<style>
.hero img {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: 30% 40%;
}
</style>
<div class="hero">
<img src="baker.jpg" width="1600" height="900"
alt="A baker lifting a loaf from a stone oven">
</div>The first percentage in object-position controls the horizontal position. The second controls the vertical position. 50% 50% centers the image. Test the crop at each breakpoint because a focal point that works on a desktop banner may fail in a narrow card.
Exercise
What CSS combination makes an image fill a fixed-ratio box while keeping a subject toward the upper-left?
Show answer
Use object-fit: cover with an appropriate aspect-ratio, then set object-position, such as 25% 30%.