1. 背景
  2. 背景附件

Quick reference

属性
bg-fixedbackground-attachment: fixed;
bg-localbackground-attachment: local;
bg-scrollbackground-attachment: scroll;

基本用法(Basic usage)

固定(Fixed)

使用 bg-fixed 将背景图片固定相对于视口的位置。

🌐 Use bg-fixed 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-fixed ..." style="background-image: url(...)"></div>

局部(Local)

使用 bg-local 使背景图片随容器和视口滚动。

🌐 Use bg-local 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-local ..." style="background-image: url(...)"></div>

滚动(Scroll)

使用 bg-scroll 可以随着视口滚动背景图片,但不会随着容器滚动。

🌐 Use bg-scroll 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-scroll ..." style="background-image: url(...)"></div>

有条件申请

悬停、聚焦及其他状态

Tailwind 允许你使用变体修饰符在不同状态下有条件地应用工具类。例如,使用hover:bg-fixed 仅在 hover 时应用 bg-fixed 工具。

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

有关所有可用状态修饰符的完整列表,请查看悬停、聚焦、以及其他状态 文档。

断点和媒体查询

你还可以使用变体修饰符来定位媒体查询,例如响应式断点、暗黑模式、首选减少运动等。例如,使用 md:bg-fixed 仅在中等屏幕尺寸及以上时应用 bg-fixed 工具。

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

要了解更多信息,请查看有关 响应式设计暗黑模式、以及 其他媒体查询修饰符 的文档。