1. 背景
  2. background-attachment

背景

background-attachment

用于控制滚动时背景图片的行为方式的工具。

样式
bg-fixed
background-attachment: fixed;
bg-local
background-attachment: local;
bg-scroll
background-attachment: scroll;

示例(Examples)

¥Examples

修复背景图片(Fixing the background image)

¥Fixing the background image

使用 bg-fixed 工具相对于视口修复背景图片:

¥Use the bg-fixed utility to fix the background image relative to the viewport:

滚动内容以查看固定在适当位置的背景图片

My trip to the summit
November 16, 2021 · 4 min read

Maybe we can live without libraries, people like you and me. Maybe. Sure, we're too old to change the world, but what about that kid, sitting down, opening a book, right now, in a branch at the local library and finding drawings of pee-pees and wee-wees on the Cat in the Hat and the Five Chinese Brothers? Doesn't HE deserve better?

Look. If you think this is about overdue fines and missing books, you'd better think again. This is about that kid's right to read a book without getting his mind warped! Or: maybe that turns you on, Seinfeld; maybe that's how y'get your kicks. You and your good-time buddies.

<div class="bg-[url(/img/mountains.jpg)] bg-fixed ...">
<!-- ... -->
</div>

使用容器(Scrolling with the container)

¥Scrolling with the container

使用 bg-local 工具将背景图片与容器和视口一起滚动:

¥Use the bg-local utility to scroll the background image with the container and the viewport:

滚动内容以查看背景图片随容器滚动

Because the mail never stops. It just keeps coming and coming and coming, there's never a let-up. It's relentless. Every day it piles up more and more and more. And you gotta get it out but the more you get it out the more it keeps coming in. And then the barcode reader breaks and it's Publisher's Clearing House day.

— Newman
<div class="bg-[url(/img/mountains.jpg)] bg-local ...">
<!-- ... -->
</div>

随视口滚动(Scrolling with the viewport)

¥Scrolling with the viewport

使用 bg-scroll 工具将背景图片与视口一起滚动,但不与容器一起滚动:

¥Use the bg-scroll utility to scroll the background image with the viewport, but not with the container:

滚动内容以查看固定在容器中的背景图片

Because the mail never stops. It just keeps coming and coming and coming, there's never a let-up. It's relentless. Every day it piles up more and more and more. And you gotta get it out but the more you get it out the more it keeps coming in. And then the barcode reader breaks and it's Publisher's Clearing House day.

— Newman
<div class="bg-[url(/img/mountains.jpg)] bg-scroll ...">
<!-- ... -->
</div>

响应式设计(Responsive design)

¥Responsive design

background-attachment 工具前面使用断点变体如 md: 仅在 medium 屏幕尺寸及以上时应用工具:

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

请参阅 变体文档 详细了解如何使用变体。

TailwindCSS v4.0 中文网 - 粤ICP备13048890号