Skip to main content

Image Ratio

To add a ratio to an image using Tailwind CSS, you can use the aspect-ratio utility class. Here's an example of how you can use it:

<div class="aspect-ratio-16/9">
<img src="your-image-source" alt="your-image-description">
</div>

The aspect-ratio-16/9 class sets the aspect ratio of the container to 16:9, which means that the width of the container will be 16 units and the height will be 9 units. The image inside the container will be automatically sized to fit within these dimensions, maintaining the aspect ratio.

You can also use the aspect-ratio utility to set a custom aspect ratio. For example, to set the aspect ratio to 4:3, you can use the following class: aspect-ratio-4/3.

<div class="aspect-ratio-4/3">
<img src="your-image-source" alt="your-image-description">
</div>

Note that the aspect-ratio utility only sets the aspect ratio of the container, and not the image itself. To ensure that the image maintains its aspect ratio when resized, you should use either pass the Tailwind CSS class for object-fit: cover or approach this globally using the following CSS:

.aspect-ratio-16/9 img {
object-fit: cover;
}

This will ensure that the image is resized to fill the container while maintaining its aspect ratio.