Backgrounds

Background Attachment

Utilities for controlling how a background image behaves when scrolling.

Quick reference

Class
Properties
bg-fixedbackground-attachment: fixed;
bg-localbackground-attachment: local;
bg-scrollbackground-attachment: scroll;

Basic usage

Fixed

Use bg-fixed to fix the background image relative to the viewport.

Background Attachment - 图1

  1. <div class="bg-fixed ..." style="background-image: url(...)"></div>

Local

Use bg-local to scroll the background image with the container and the viewport.

Background Attachment - 图2

  1. <div class="bg-local ..." style="background-image: url(...)"></div>

Scroll

Use bg-scroll to scroll the background image with the viewport, but not with the container.

Background Attachment - 图3

  1. <div class="bg-scroll ..." style="background-image: url(...)"></div>

Applying conditionally

Hover, focus, and other states

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:bg-fixed to only apply the bg-fixed utility on hover.

  1. <div class="bg-local hover:bg-fixed">
  2. <!-- ... -->
  3. </div>

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

Breakpoints and media queries

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:bg-fixed to apply the bg-fixed utility at only medium screen sizes and above.

  1. <div class="bg-local md:bg-fixed">
  2. <!-- ... -->
  3. </div>

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.