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. 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

ClassProperties
bg-autobackground-size: auto;
bg-coverbackground-size: cover;
bg-containbackground-size: contain;

Windframe Tailwind blocks

Timeline

Build modern projects using Bootstrap 5 and Contrast

Trying to create components and pages for a web app or website from scratch while maintaining a modern User interface can be very tedious. This is why we created Contrast, to help drastically reduce the amount of time we spend doing that. so we can focus on building some other aspects of the project.

Contrast Bootstrap PRO consists of a Premium UI Kit Library featuring over 10000+ component variants. Which even comes bundled together with its own admin template comprising of 5 admin dashboards and 23+ additional admin and multipurpose pages for building almost any type of website or web app.
See a demo and learn more about Contrast Bootstrap Pro by clicking here.

ad-banner