Tailwind image size and responsive example

tailwind

To set the size of an image using Tailwind CSS, you can use the w- and h- classes. These classes take a size value as an argument, which can be a length in pixels (e.g. w-64, h-32) or a predefined size key (e.g. w-full, h-auto).

For example, you can set the width of an image to 200 pixels and the height to 100 pixels like this:

<img src="https://via.placeholder.com/600x400" alt="Image" class="w-200 h-100">

To make the image responsive, you can use the object-cover class, which scales the image to cover the container while preserving its aspect ratio. You can also use the w-full and h-full classes to make the image take up the full width and height of its container, respectively.

For example, you can create a responsive image like this:


<div class="w-full h-64 bg-gray-300">
    <img src="https://via.placeholder.com/600x400" alt="Image" class="object-cover w-full h-full">
</div>

In this example, the w-full and h-full classes make the image take up the full width and height of the container, and the object-cover class scales the image to cover the container while preserving its aspect ratio. The bg-gray-300 class adds a light gray background color to the container.

You can also use media queries to create responsive images that change size at different breakpoints. For example, you can use the md:w- and md:h- classes to set the width and height of the image at the md breakpoint (768px and up).

For example:


<div class="w-full h-64 bg-gray-300">
    <img src="https://via.placeholder.com/600x400" alt="Image" class="object-cover w-full h-full md:w-48 md:h-32">
</div>

In this example, the image will take up the full width and height of the container on screens smaller than 768px, and it will have a width of 48 pixels and a height of 32 pixels on screens 768px and up.