Last updated: 18 May 2024

Tailwind CSS Background Image

The tailwind background-image utility class allows you to easily add background images to elements.


Tailwind Background Image

The Tailwind background-image utility class allows you to easily add background images to elements. With Tailwind background image classes, you can enhance the visual appeal of your web pages by adding captivating images as backgrounds.

Applying Tailwind Background Image

To apply a tailwind background image to an element, you can use the bg-{image} utility class, where {image} represents the desired image. Tailwind CSS provides a straightforward syntax to specify background images.

html
<div class="bg-gradient-to-r from-blue-500 to-purple-500">
This element has a gradient background image.
</div>

Preview

This element has a gradient background image.

Predefined Tailwind Background Image Classes

Tailwind CSS offers a set of predefined background image classes that you can use out of the box. Some of the commonly used ones include:

  • bg-fixed: Sets the background image as fixed, preventing it from scrolling with the rest of the content.

  • bg-cover: Scales the background image proportionally to cover the entire element, cropping any excess.

  • bg-contain: Scales the background image proportionally to fit within the element, preserving its aspect ratio.

  • bg-repeat: Repeats the background image both vertically and horizontally to fill the element.

You can combine these classes with color classes or custom CSS to create unique background image effects.

Custom Tailwind Background Images

Tailwind CSS allows you to define your own custom background images using the bg-{name} utility class. This feature is particularly useful when you want to use specific images in your project.

html
<div class="bg-custom-image">This element has a custom background image.</div>

In the above example, the bg-custom-image class is applied to the <div> element, which corresponds to a custom background image defined in the Tailwind CSS configuration file.

Responsive Tailwind Background Image

Tailwind CSS enables you to apply background images responsively at different breakpoints. To use responsive tailwind background image classes, you can append the breakpoint prefix to the background image class. For example, md:bg-cover applies the bg-cover background image starting from the medium breakpoint and above.

html
<div class="bg-cover md:bg-contain">
This element has a cover background image by default, but switches to contain
starting from the medium breakpoint.
</div>

In the above example, the bg-cover class is applied by default, setting the background image to cover the element. However, starting from the medium breakpoint and above (md:bg-contain), the background image changes to be contained within the element.

Tailwind Background Image Classes table

ClassProperties
bg-nonebackground-image: none;
bg-gradient-to-tbackground-image: linear-gradient(to top, var(--tw-gradient-stops));
bg-gradient-to-trbackground-image: linear-gradient(to top right, var(--tw-gradient-stops));
bg-gradient-to-rbackground-image: linear-gradient(to right, var(--tw-gradient-stops));
bg-gradient-to-brbackground-image: linear-gradient(to bottom right, var(--tw-gradient-stops));
bg-gradient-to-bbackground-image: linear-gradient(to bottom, var(--tw-gradient-stops));
bg-gradient-to-blbackground-image: linear-gradient(to bottom left, var(--tw-gradient-stops));
bg-gradient-to-lbackground-image: linear-gradient(to left, var(--tw-gradient-stops));
bg-gradient-to-tlbackground-image: linear-gradient(to top left, var(--tw-gradient-stops));

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