Skip to content

Tailwind CSS - Background Attachment

# Modern Guide to Tailwind CSS: Background Attachment

Section titled “# Modern Guide to Tailwind CSS: Background Attachment”

This chapter covers the background-attachment utilities in Tailwind CSS. These classes control whether a background image’s position is fixed within the viewport or scrolls along with its containing block.

## Understanding Background Attachment Utilities

Section titled “## Understanding Background Attachment Utilities”

Think of it this way:

  • bg-fixed: The background is like a sticker on your monitor. As you scroll the page content, the sticker stays in one place. This creates a parallax effect.
  • bg-local: The background is like a pattern printed on the content itself. If the content is inside a scrollable container, the background scrolls with the content.
  • bg-scroll: The background is like a pattern on the container’s wall. It doesn’t scroll with the container’s content but will scroll with the main browser window. This is the default behavior.
ClassCSS PropertiesBehavior
bg-fixedbackground-attachment: fixed;Fixed relative to the viewport.
bg-localbackground-attachment: local;Scrolls with the element’s content.
bg-scrollbackground-attachment: scroll;Scrolls with the element, but not its content. (Default)

## Interactive Example: Comparing Attachment Behaviors

Section titled “## Interactive Example: Comparing Attachment Behaviors”

The following example demonstrates all three utilities side-by-side. Scroll within each box and scroll the main page to see the distinct behaviors.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Background Attachment Example</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* For demonstration purposes to ensure the main page is scrollable */
body { height: 150vh; }
</style>
</head>
<body class="bg-slate-100 dark:bg-slate-900 p-8 font-sans">
<h1 class="text-3xl font-bold text-slate-900 dark:text-white mb-2 text-center">Background Attachment</h1>
<p class="text-center text-slate-600 dark:text-slate-400 mb-8">Scroll the page and each box to see the difference.</p>
<div class="grid md:grid-cols-3 gap-8 max-w-7xl mx-auto">
<!-- 1. bg-fixed -->
<div>
<h2 class="font-bold text-xl mb-3 text-slate-800 dark:text-slate-200">`bg-fixed`</h2>
<div class="h-96 w-full rounded-lg overflow-auto border-4 border-white shadow-lg bg-cover bg-center bg-fixed" style="background-image:url('https://images.unsplash.com/photo-1559128010-4492ae8e4caf?q=80&w=1974&auto=format&fit=crop')">
<div class="p-4 text-white bg-black/50">
<p class="font-semibold">This background is fixed to the viewport. It won't scroll with the box or the page, creating a parallax effect.</p>
<p class="mt-40">Scroll down...</p>
</div>
</div>
</div>
<!-- 2. bg-local -->
<div>
<h2 class="font-bold text-xl mb-3 text-slate-800 dark:text-slate-200">`bg-local`</h2>
<div class="h-96 w-full rounded-lg overflow-auto border-4 border-white shadow-lg bg-cover bg-center bg-local" style="background-image:url('https://images.unsplash.com/photo-1507525428034-b723a9ce6890?q=80&w=2070&auto=format&fit=crop')">
<div class="p-4 text-white bg-black/50">
<p class="font-semibold">This background scrolls with the content inside this box. Notice how the image moves as you scroll within this container.</p>
<p class="mt-80">A lot of content to create scrolling...</p>
<p class="mt-80">End of content.</p>
</div>
</div>
</div>
<!-- 3. bg-scroll (Default) -->
<div>
<h2 class="font-bold text-xl mb-3 text-slate-800 dark:text-slate-200">`bg-scroll`</h2>
<div class="h-96 w-full rounded-lg overflow-auto border-4 border-white shadow-lg bg-cover bg-center bg-scroll" style="background-image:url('https://images.unsplash.com/photo-1501854140801-50d01698950b?q=80&w=1950&auto=format&fit=crop')">
<div class="p-4 text-white bg-black/50">
<p class="font-semibold">This background does not scroll with the content in the box, but it does scroll with the main page. It is fixed to the element.</p>
<p class="mt-80">A lot of content to create scrolling...</p>
<p class="mt-80">End of content.</p>
</div>
</div>
</div>
</div>
</body>
</html>
  • Parallax Effects: bg-fixed is the classic choice for creating simple, yet elegant, parallax scrolling effects, often used on landing pages.
  • Styled Content Areas: bg-local is useful when you want a background pattern or image to be tied directly to a specific block of text or content within a scrollable div.
  • Performance Warning: While bg-fixed is great for visual effects, it can cause performance issues (jank or stuttering) on some devices, especially mobile. The browser needs to repaint the background on every scroll event, which can be taxing. Use it thoughtfully and test on target devices. For complex animations, consider using JavaScript-based parallax libraries that use transform for better performance.

Ensure that any text placed over a background image has sufficient contrast to be readable. You can use overlay classes like bg-black/50 (a semi-transparent black background) to darken the image and improve text legibility, which is crucial for accessibility (a11y).