CSS - Object-fit & Object-position
Overview
Quick links: Exercises • Checks • Print as PDF
Estimated time: 8–12 minutes
Control how images and videos fit inside fixed boxes without distorting aspect ratios.
Learning Objectives
- Use object-fit: cover|containfor media cropping vs letterboxing.
- Adjust framing with object-position.
Details & Examples
img.cover{ width:100%; height:240px; object-fit: cover; object-position: 50% 30%; }
img.contain{ width:100%; height:240px; object-fit: contain; background:#0a0f1a; }
Try it
Common Pitfalls
- object-fitapplies to replaced elements (img, video), not background images.
Checks for Understanding
- Which setting crops to fill the box while preserving aspect ratio?
Show answers
- object-fit: cover.
Exercises
- Demonstrate object-fit: covervscontainon the same image frames.
- Use object-positionto frame the focal point of an image toward the top-left.
Suggested answers
- Create two elements with fixed dimensions and apply the two settings to compare.
- object-position: 20% 10%;or similar to bias the crop.