Last updated: 3 May 2024

Tailwind CSS Background Size

The tailwind background size utility class in Tailwind CSS allows you to control the size of background images applied to elements.


Tailwind Background Size

The tailwind background size utility class in Tailwind CSS allows you to control the size of background images applied to elements. With Tailwind's background size class, you can easily adjust the dimensions of background images to achieve the desired visual effect.

Applying Tailwind Background Size

To apply a specific background size to an element, you can use the bg-{size} utility class, where {size} represents the desired background size. Here are some common values you can use:

  • bg-auto: The background image is displayed at its original size.
  • bg-cover: The background image is scaled proportionally to cover the entire element, potentially cropping parts of the image.
  • bg-contain: The background image is scaled proportionally to fit within the element, ensuring the entire image is visible but potentially leaving empty space. Here's an example:
html
<div class="bg-cover">
This element has a background image set to cover the entire area.
</div>

Preview

Responsive Tailwind Background Size

Tailwind CSS allows you to apply background size classes responsively at different breakpoints. To use responsive tailwind background size classes, you can append the breakpoint prefix to the utility class. For example, md:bg-contain sets the background size to contain starting from the medium breakpoint and above.

html
<div class="bg-contain md:bg-cover">
This element has a background image that fits within the area, but starting
from the medium breakpoint and above, it covers the entire element.
</div>

In the above example, the tailwind background size of the <div> element is set to contain by default (bg-contain), but starting from the medium breakpoint and above, it changes to cover the entire element (md:bg-cover).

Tailwind Background Size Class Table

Class Properties
bg-auto background-size: auto;
bg-cover background-size: cover;
bg-contain background-size: contain;

Windframe Tailwind blocks

Timeline

Windframe is a drag and drop builder for rapidly building tailwind css websites and UIs

Start building stunning tailwind UIs! 

Build from scratch or select prebuilt tailwind templates