Background Attachment

Utilities for controlling how a background image behaves when scrolling.

Class reference

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

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>

Responsive

To control the background attachment of an element at a specific breakpoint, add a {screen}: prefix to any existing background attachment utility. For example, use md:bg-fixed to apply the bg-fixed utility at only medium screen sizes and above.

For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.

Background Attachment - 图4

all

Background Attachment - 图5

sm

Background Attachment - 图6

md

Background Attachment - 图7

lg

Background Attachment - 图8

xl

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

Customizing

Responsive and pseudo-class variants

By default, only responsive variants are generated for background attachment utilities.

You can control which variants are generated for the background attachment utilities by modifying the backgroundAttachment property in the variants section of your tailwind.config.js file.

For example, this config will also generate hover and focus variants:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // ...
  5. - backgroundAttachment: ['responsive'],
  6. + backgroundAttachment: ['responsive', 'hover', 'focus'],
  7. }
  8. }

Disabling

If you don’t plan to use the background attachment utilities in your project, you can disable them entirely by setting the backgroundAttachment property to false in the corePlugins section of your config file:

  1. // tailwind.config.js
  2. module.exports = {
  3. corePlugins: {
  4. // ...
  5. + backgroundAttachment: false,
  6. }
  7. }

← Word Break   Background Clip →