Utilities for controlling how a background image behaves when scrolling.
Class |
Properties |
---|---|
bg-fixed | background-attachment: fixed; |
bg-local | background-attachment: local; |
bg-scroll | background-attachment: scroll; |
Use bg-fixed
to fix the background image relative to the viewport.
<div class="bg-fixed ..." style="background-image: url(...)"></div>
Use bg-local
to scroll the background image with the container and the viewport.
<div class="bg-local ..." style="background-image: url(...)"></div>
Use bg-scroll
to scroll the background image with the viewport, but not with the container.
<div class="bg-scroll ..." style="background-image: url(...)"></div>
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.
<div class="bg-local hover:bg-fixed"> <!-- ... --> </div>
For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.
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.
<div class="bg-local md:bg-fixed"> <!-- ... --> </div>
To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.
© 2022 Tailwind Labs Inc.
https://tailwindcss.com/docs/background-attachment