Object Fit
Controls how replaced elements like images and videos should be resized and positioned within their containers. This is crucial for responsive design, allowing media to adapt beautifully to different container dimensions without distortion. In Tailwind v4, object-fit utilities work seamlessly with aspect-ratio and other modern layout features.
Layout1 variant4 examples
Basic usage:
<div class="w-full h-64 bg-gray-200">
<img class="w-full h-full object-cover" src="example.jpg" alt="Cover example">
</div>
Examples
<div class="w-full h-64 bg-gray-200">
<img class="w-full h-full object-cover" src="example.jpg" alt="Cover example">
</div>
<div class="w-full h-64 bg-gray-200">
<img class="w-full h-full object-contain" src="example.jpg" alt="Contain example">
</div>
<!-- Comparison of different object-fit values -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="aspect-video bg-gray-100">
<img class="w-full h-full object-cover" src="example.jpg" alt="Cover">
<p class="text-center text-sm mt-2">object-cover</p>
</div>
<div class="aspect-video bg-gray-100">
<img class="w-full h-full object-contain" src="example.jpg" alt="Contain">
<p class="text-center text-sm mt-2">object-contain</p>
</div>
<div class="aspect-video bg-gray-100">
<img class="w-full h-full object-fill" src="example.jpg" alt="Fill">
<p class="text-center text-sm mt-2">object-fill</p>
</div>
</div>
<!-- Hero section with background image -->
<section class="relative h-96">
<img class="absolute inset-0 w-full h-full object-cover" src="hero.jpg" alt="Hero image">
<div class="absolute inset-0 bg-black bg-opacity-40"></div>
<div class="relative h-full flex items-center justify-center p-6 text-white">
<div class="text-center">
<h1 class="text-4xl font-bold mb-4">Welcome to Our Site</h1>
<p class="text-xl">Discover amazing content and services</p>
</div>
</div>
</section>
Live Preview
Try the Object Fit utility with different values
Variants
contain: scales to fit entirely within box, maintaining aspect ratio; cover: fills entire box, maintaining aspect ratio (may crop); fill: stretches to fill box, ignoring aspect ratio; none: not resized; scale-down: like contain or none, whichever is smaller
containcoverfillnonescale-down
Tips & Reference
Related Functions
ContainerThe container utility creates a responsive, centered wrapper...
Box Decoration BreakControls how element decorations (like background, border, s...
Box SizingDetermines how the total width and height of an element is c...
DisplayControls how an element is rendered in the document flow. Th...
Aspect RatioSets the preferred aspect ratio for an element, which will b...
View all Layout utilities