tailwind-css
  1. tailwind-css-background-attachment

Tailwind CSS Background Attachment

The background-attachment utility in Tailwind CSS is used to set the background image attachment of an element. It allows you to control whether the background image should scroll with the content or remain fixed in the viewport.

Syntax

Here's the basic syntax for using the background-attachment utility:

<div class="bg-fixed"></div> <!-- fixed attachment -->
<div class="bg-local"></div> <!-- scroll attachment -->
<div class="bg-scroll"></div> <!-- scroll attachment -->

Example

Here's an example of using the background-attachment utility in Tailwind CSS:

<div class="bg-fixed bg-cover bg-center h-screen" style="background-image: url('your-image.jpg');">
  <h1 class="text-white text-4xl font-bold">Background Attachment Example</h1>
</div>

<div class="bg-local bg-repeat h-screen" style="background-image: url('your-image.jpg');">
  <h1 class="text-black text-4xl font-bold">Background Attachment Example</h1>
</div>

<div class="bg-scroll bg-cover bg-center h-screen" style="background-image: url('your-image.jpg');">
  <h1 class="text-white text-4xl font-bold">Background Attachment Example</h1>
</div>

Output

The above example would generate the following output:

Tailwind CSS Background Attachment Example Output

Explanation

  • Use .bg-fixed utility to set fixed attachment for your background image to the viewport.
  • Use .bg-local utility to set scroll attachment for your background image to the element's content.
  • Use .bg-scroll utility to set scroll attachment for your background image to the viewport.

Use

Use the background-attachment utility to control the attachment of your background images in Tailwind CSS.

Important Points

  • The background-attachment utility is used to set the background image attachment of an element.
  • It allows you to control whether the background image should scroll with the content or remain fixed in the viewport.
  • There are three values for this utility: bg-fixed, bg-local, and bg-scroll.

Summary

The background-attachment utility in Tailwind CSS allows you to control the attachment of your background images, making it easy to create visually interesting designs with fixed or scrolling backgrounds.

Published on: