Back to Tailwindcss

Background Attachment

src/docs/background-attachment.mdx

latest5.3 KB
Original Source

import { ApiTable } from "@/components/api-table.tsx"; import { Example } from "@/components/example.tsx"; import { Figure } from "@/components/figure.tsx"; import { ResponsiveDesign } from "@/components/content.tsx";

export const title = "background-attachment"; export const description = "Utilities for controlling how a background image behaves when scrolling.";

<ApiTable rows={[ ["bg-fixed", "background-attachment: fixed;"], ["bg-local", "background-attachment: local;"], ["bg-scroll", "background-attachment: scroll;"], ]} />

Examples

Fixing the background image

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

<Figure hint="Scroll the content to see the background image fixed in place"> <Example padding={false}> { <div className="mx-auto h-80 overflow-y-scroll border-x border-x-gray-200 bg-[url('https://images.unsplash.com/photo-1554629947-334ff61d85dc?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&h=800&q=80')] bg-cover bg-[center_-100px] dark:border-gray-800"> <div className="mt-45"> <div className="border-x border-x-gray-200 bg-white p-4 text-gray-500 sm:p-8 dark:border-x-gray-800 dark:bg-gray-900 dark:text-gray-400"> <div className="font-inter text-2xl font-extrabold tracking-tight text-black dark:text-white"> My trip to the summit </div> <div className="mt-1 text-sm font-medium text-gray-500">November 16, 2021 · 4 min read</div> <p className="mt-4 leading-7"> 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? </p> <p className="mt-4 leading-7"> 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. </p> </div> </div> </div> } </Example>
html
<!-- [!code classes:bg-fixed] -->
<div class="bg-[url(/img/mountains.jpg)] bg-fixed ...">
  <!-- ... -->
</div>
</Figure>

Scrolling with the container

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

<Figure hint="Scroll the content to see the background image scroll with the container"> <Example padding={false}> { <div> <div className="h-64 overflow-y-scroll bg-[url('https://images.unsplash.com/photo-1554629947-334ff61d85dc?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&h=666&q=80')] bg-cover bg-local"> <div className="min-h-full bg-black/40 p-8 sm:px-16 sm:py-12"> <p className="text-center font-serif text-xl leading-6 text-white italic sm:text-2xl sm:leading-8"> 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. </p> <div className="font-inter mt-6 text-center text-sm font-bold text-white sm:text-base">&mdash; Newman</div> </div> </div> </div> } </Example>
html
<!-- [!code classes:bg-local] -->
<div class="bg-[url(/img/mountains.jpg)] bg-local ...">
  <!-- ... -->
</div>
</Figure>

Scrolling with the viewport

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

<Figure hint="Scroll the content to see the background image fixed in the container"> <Example padding={false}> { <div> <div className="h-64 overflow-y-scroll bg-[url('https://images.unsplash.com/photo-1554629947-334ff61d85dc?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&h=666&q=80')] bg-cover bg-scroll"> <div className="min-h-full bg-black/40 p-8 sm:px-16 sm:py-12"> <p className="text-center font-serif text-xl leading-6 text-white italic sm:text-2xl sm:leading-8"> 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. </p> <div className="font-inter mt-6 text-center text-sm font-bold text-white sm:text-base">&mdash; Newman</div> </div> </div> </div> } </Example>
html
<!-- [!code classes:bg-scroll] -->
<div class="bg-[url(/img/mountains.jpg)] bg-scroll ...">
  <!-- ... -->
</div>
</Figure>

Responsive design

<ResponsiveDesign property="background-attachment" defaultClass="bg-local" featuredClass="bg-fixed" />