Aspect Ratio
Sets the preferred aspect ratio for an element, which will be maintained as the element resizes. This is extremely useful for responsive design, particularly with images, videos, and other media to prevent layout shifts during page load. In Tailwind v4, the aspect-ratio utility has enhanced browser support and performance.
Layout1 variant4 examples
Basic usage:
<div class="aspect-square bg-blue-200 w-full max-w-md">
This div will always be perfectly square.
</div>
Examples
<div class="aspect-square bg-blue-200 w-full max-w-md">
This div will always be perfectly square.
</div>
<div class="aspect-video bg-green-200 w-full max-w-xl">
This div maintains a 16:9 aspect ratio like a video.
</div>
<!-- Image with maintained aspect ratio -->
<div class="aspect-[4/3] bg-gray-200 w-full max-w-lg overflow-hidden">
<img src="example.jpg" alt="Example" class="w-full h-full object-cover" />
</div>
<!-- Responsive card with image maintaining aspect ratio -->
<div class="max-w-sm rounded-lg overflow-hidden shadow-lg">
<div class="aspect-[3/2] bg-gray-100">
<img class="w-full h-full object-cover" src="example.jpg" alt="Card image">
</div>
<div class="p-4">
<h3 class="font-bold text-xl mb-2">Card Title</h3>
<p class="text-gray-700">The image above maintains a 3:2 aspect ratio regardless of card width.</p>
</div>
</div>
Live Preview
Try the Aspect Ratio utility with different values
Variants
auto: no aspect ratio enforced, square: 1:1 ratio, video: 16:9 ratio, [custom]: arbitrary values using bracket notation like aspect-[4/3]
autosquarevideo[custom]
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...
ColumnsCreates multi-column text layouts, similar to newspaper or m...
View all Layout utilities