Tailwind CSS - Background Origin
Tailwind CSS - Background Origin
Section titled “Tailwind CSS - Background Origin”Utilities for controlling the origin of an element’s background image or color. It specifies where the background-position is relative to.
Understanding a CSS Box
Section titled “Understanding a CSS Box”To understand background origin, you first need to understand the CSS box model. An element’s box consists of three main areas:
- Content Box: Where your text and other content lives.
- Padding Box: The space between the content and the border.
- Border Box: The area occupied by the border itself.
The bg-origin-* utilities determine if the background’s starting point (the top-left corner) is aligned with the border, the padding, or the content.
Background Origin Classes
Section titled “Background Origin Classes”Tailwind provides three utilities to control the background origin.
| Class | CSS Properties | Description |
|---|---|---|
bg-origin-border | background-origin: border-box; | Background is positioned relative to the border box. It can extend underneath the border. |
bg-origin-padding | background-origin: padding-box; | Background is positioned relative to the padding box (default behavior). |
bg-origin-content | background-origin: content-box; | Background is positioned relative to the content box. |
Visual Demonstration
Section titled “Visual Demonstration”The difference between these utilities is most apparent when an element has significant padding and a dashed or semi-transparent border. In the example below, we use a dashed border to reveal what’s happening underneath. For the background, we use a CSS gradient to make the example self-contained.
Example: Comparing Background Origins
Section titled “Example: Comparing Background Origins”<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Background Origin Comparison</title> <!-- For demonstration, we use the Tailwind Play CDN. In a real project, set up Tailwind CSS with a build step. --> <script src="https://cdn.tailwindcss.com"></script></head><body class="bg-slate-100 p-8 antialiased">
<div class="max-w-4xl mx-auto"> <h1 class="text-3xl font-bold text-center text-slate-800 mb-8">Background Origin Utilities</h1>
<div class="grid sm:grid-cols-3 gap-8 text-center">
<!-- Background Origin: Border --> <div> <h2 class="font-mono text-slate-600 mb-2">bg-origin-border</h2> <div class="bg-origin-border p-6 h-40 border-8 border-dashed border-purple-400 bg-gradient-to-r from-cyan-400 to-blue-600 rounded-lg flex items-center justify-center"> <p class="text-white font-bold bg-black/50 p-2 rounded">The gradient starts at the border.</p> </div> </div>
<!-- Background Origin: Padding (Default) --> <div> <h2 class="font-mono text-slate-600 mb-2">bg-origin-padding</h2> <div class="bg-origin-padding p-6 h-40 border-8 border-dashed border-purple-400 bg-gradient-to-r from-cyan-400 to-blue-600 rounded-lg flex items-center justify-center"> <p class="text-white font-bold bg-black/50 p-2 rounded">The gradient starts at the padding.</p> </div> </div>
<!-- Background Origin: Content --> <div> <h2 class="font-mono text-slate-600 mb-2">bg-origin-content</h2> <div class="bg-origin-content p-6 h-40 border-8 border-dashed border-purple-400 bg-gradient-to-r from-cyan-400 to-blue-600 rounded-lg flex items-center justify-center"> <p class="text-white font-bold bg-black/50 p-2 rounded">The gradient starts at the content.</p> </div> </div>
</div> </div>
</body></html>When to Use Background Origin
Section titled “When to Use Background Origin”While bg-origin-padding is the default and most common behavior, the other utilities are useful for specific visual effects:
bg-origin-border: Use this when you have a transparent or dashed border and you want the background to be visible underneath it, creating a cohesive look.bg-origin-content: Use this when you want to constrain a background pattern or image strictly within the content area, creating a clear separation between the background and the padding.
These utilities are often used in combination with background-clip to control not just the origin but also the painting area of the background.