Backgrounds
Overview
Background utilities help you control the background styling of elements including colors, images, sizing, positioning, repeat behavior, and more.
Why Use Background Utilities?
Background utilities help you:
- Integrate with design tokens: Reference your color system directly using
ref() - Handle responsive images: Control how background images scale and position
- Create consistent patterns: Generate reusable background styles across your application
- Support theming: Background colors can automatically adapt to theme changes
useBackgroundColorUtility
The useBackgroundColorUtility() function creates utility classes for setting background colors.
import { styleframe } from "styleframe";
import { useColor } from "@styleframe/theme";
import { useBackgroundColorUtility } from "@styleframe/theme";
const s = styleframe();
const { ref } = s;
// Define colors
const { colorPrimary, colorSecondary } = useColor(s, {
primary: '#006cff',
secondary: '#6c757d',
} as const);
// Create background color utilities
useBackgroundColorUtility(s, {
inherit: 'inherit',
current: 'currentColor',
transparent: 'transparent',
primary: ref(colorPrimary),
secondary: ref(colorSecondary),
white: '#fff',
black: '#000',
});
export default s;
:root {
--color--primary: oklch(0.5749 0.233917 259.9541 / 1);
--color--secondary: oklch(0.5575 0.0165 244.89 / 1);
}
._bg\:inherit { background-color: inherit; }
._bg\:current { background-color: currentColor; }
._bg\:transparent { background-color: transparent; }
._bg\:primary { background-color: var(--color--primary); }
._bg\:secondary { background-color: var(--color--secondary); }
._bg\:white { background-color: #fff; }
._bg\:black { background-color: #000; }
<div class="_bg:primary">Primary background</div>
<div class="_bg:secondary">Secondary background</div>
<header class="_bg:white">White header</header>
<footer class="_bg:black">Black footer</footer>
ref() to reference your color design tokens so background colors automatically update with theme changes.useBackgroundImageUtility
The useBackgroundImageUtility() function creates utility classes for setting background images.
import { styleframe } from "styleframe";
import { useBackgroundImageUtility } from "@styleframe/theme";
const s = styleframe();
useBackgroundImageUtility(s, {
none: 'none',
'gradient-primary': 'linear-gradient(to right, #006cff, #00b4d8)',
'gradient-dark': 'linear-gradient(to bottom, #1a1a1a, #2d2d2d)',
});
export default s;
._background-image\:none { background-image: none; }
._background-image\:gradient-primary { background-image: linear-gradient(to right, #006cff, #00b4d8); }
._background-image\:gradient-dark { background-image: linear-gradient(to bottom, #1a1a1a, #2d2d2d); }
<div class="_background-image:gradient-primary">Gradient background</div>
<section class="_background-image:gradient-dark">Dark gradient section</section>
useBackgroundSizeUtility
The useBackgroundSizeUtility() function creates utility classes for controlling background image sizing.
import { styleframe } from "styleframe";
import { useBackgroundSizeUtility } from "@styleframe/theme";
const s = styleframe();
// Uses built-in defaults: auto, cover, contain
useBackgroundSizeUtility(s);
export default s;
._background-size\:auto { background-size: auto; }
._background-size\:cover { background-size: cover; }
._background-size\:contain { background-size: contain; }
<div class="_background-size:cover">Full coverage background</div>
<div class="_background-size:contain">Contained background</div>
Default Values
| Key | Value | Description |
|---|---|---|
auto | auto | Default size, image displays at original dimensions |
cover | cover | Scale to cover entire element, may crop |
contain | contain | Scale to fit within element, may leave gaps |
useBackgroundPositionUtility
The useBackgroundPositionUtility() function creates utility classes for positioning background images.
import { styleframe } from "styleframe";
import { useBackgroundPositionUtility } from "@styleframe/theme";
const s = styleframe();
// Uses built-in defaults for all position combinations
useBackgroundPositionUtility(s);
export default s;
._background-position\:bottom { background-position: bottom; }
._background-position\:center { background-position: center; }
._background-position\:left { background-position: left; }
._background-position\:left-bottom { background-position: left bottom; }
._background-position\:left-top { background-position: left top; }
._background-position\:right { background-position: right; }
._background-position\:right-bottom { background-position: right bottom; }
._background-position\:right-top { background-position: right top; }
._background-position\:top { background-position: top; }
<div class="_background-position:center">Centered background</div>
<div class="_background-position:top">Top-aligned background</div>
<div class="_background-position:right-bottom">Bottom-right background</div>
Default Values
| Key | Value | Description |
|---|---|---|
bottom | bottom | Align to bottom center |
center | center | Center horizontally and vertically |
left | left | Align to left center |
left-bottom | left bottom | Align to bottom left corner |
left-top | left top | Align to top left corner |
right | right | Align to right center |
right-bottom | right bottom | Align to bottom right corner |
right-top | right top | Align to top right corner |
top | top | Align to top center |
useBackgroundRepeatUtility
The useBackgroundRepeatUtility() function creates utility classes for controlling how background images repeat.
import { styleframe } from "styleframe";
import { useBackgroundRepeatUtility } from "@styleframe/theme";
const s = styleframe();
useBackgroundRepeatUtility(s, {
repeat: 'repeat',
'no-repeat': 'no-repeat',
'repeat-x': 'repeat-x',
'repeat-y': 'repeat-y',
round: 'round',
space: 'space',
});
export default s;
._background-repeat\:repeat { background-repeat: repeat; }
._background-repeat\:no-repeat { background-repeat: no-repeat; }
._background-repeat\:repeat-x { background-repeat: repeat-x; }
._background-repeat\:repeat-y { background-repeat: repeat-y; }
._background-repeat\:round { background-repeat: round; }
._background-repeat\:space { background-repeat: space; }
<div class="_background-repeat:no-repeat">Single background image</div>
<div class="_background-repeat:repeat-x">Horizontally repeating pattern</div>
useBackgroundAttachmentUtility
The useBackgroundAttachmentUtility() function creates utility classes for controlling how background images scroll.
import { styleframe } from "styleframe";
import { useBackgroundAttachmentUtility } from "@styleframe/theme";
const s = styleframe();
// Uses built-in defaults: fixed, local, scroll
useBackgroundAttachmentUtility(s);
export default s;
._background-attachment\:fixed { background-attachment: fixed; }
._background-attachment\:local { background-attachment: local; }
._background-attachment\:scroll { background-attachment: scroll; }
<div class="_background-attachment:fixed">Parallax-style fixed background</div>
<div class="_background-attachment:scroll">Normal scrolling background</div>
Default Values
| Key | Value | Description |
|---|---|---|
fixed | fixed | Background stays fixed relative to viewport |
local | local | Background scrolls with element's content |
scroll | scroll | Background scrolls with the element |
useBackgroundClipUtility
The useBackgroundClipUtility() function creates utility classes for controlling how far the background extends.
import { styleframe } from "styleframe";
import { useBackgroundClipUtility } from "@styleframe/theme";
const s = styleframe();
useBackgroundClipUtility(s, {
border: 'border-box',
padding: 'padding-box',
content: 'content-box',
text: 'text',
});
export default s;
._background-clip\:border { background-clip: border-box; }
._background-clip\:padding { background-clip: padding-box; }
._background-clip\:content { background-clip: content-box; }
._background-clip\:text { background-clip: text; }
<div class="_background-clip:padding">Clip to padding box</div>
<h1 class="_background-clip:text _background-image:gradient-primary" style="color: transparent;">
Gradient text
</h1>
background-clip: text with a gradient background and transparent text color to create gradient text effects.useBackgroundOriginUtility
The useBackgroundOriginUtility() function creates utility classes for setting the background's origin point.
import { styleframe } from "styleframe";
import { useBackgroundOriginUtility } from "@styleframe/theme";
const s = styleframe();
useBackgroundOriginUtility(s, {
border: 'border-box',
padding: 'padding-box',
content: 'content-box',
});
export default s;
._background-origin\:border { background-origin: border-box; }
._background-origin\:padding { background-origin: padding-box; }
._background-origin\:content { background-origin: content-box; }
<div class="_background-origin:border">Background starts at border</div>
<div class="_background-origin:content">Background starts at content</div>
Examples
Hero Section with Background Image
import { styleframe } from "styleframe";
import {
useBackgroundColorUtility,
useBackgroundImageUtility,
useBackgroundSizeUtility,
useBackgroundPositionUtility,
useBackgroundRepeatUtility,
} from "@styleframe/theme";
const s = styleframe();
// Background utilities for hero sections
useBackgroundColorUtility(s, {
overlay: 'rgba(0, 0, 0, 0.5)',
});
useBackgroundImageUtility(s, {
hero: 'url("/images/hero.jpg")',
none: 'none',
});
useBackgroundSizeUtility(s);
useBackgroundPositionUtility(s);
useBackgroundRepeatUtility(s, { 'no-repeat': 'no-repeat' });
export default s;
._bg\:overlay { background-color: rgba(0, 0, 0, 0.5); }
._background-image\:hero { background-image: url("/images/hero.jpg"); }
._background-image\:none { background-image: none; }
._background-size\:auto { background-size: auto; }
._background-size\:cover { background-size: cover; }
._background-size\:contain { background-size: contain; }
._background-position\:center { background-position: center; }
/* ... more position utilities */
._background-repeat\:no-repeat { background-repeat: no-repeat; }
Usage in HTML:
<section class="_background-image:hero _background-size:cover _background-position:center _background-repeat:no-repeat">
<div class="_bg:overlay">
<h1>Welcome</h1>
</div>
</section>
Gradient Text Effect
import { styleframe } from "styleframe";
import { useBackgroundImageUtility, useBackgroundClipUtility } from "@styleframe/theme";
const s = styleframe();
const { selector } = s;
useBackgroundImageUtility(s, {
'gradient-rainbow': 'linear-gradient(90deg, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #8b00ff)',
});
useBackgroundClipUtility(s, {
text: 'text',
});
// Create a reusable gradient text class
selector('.gradient-text', {
backgroundImage: 'linear-gradient(90deg, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #8b00ff)',
backgroundClip: 'text',
WebkitBackgroundClip: 'text',
color: 'transparent',
});
export default s;
._background-image\:gradient-rainbow {
background-image: linear-gradient(90deg, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #8b00ff);
}
._background-clip\:text { background-clip: text; }
.gradient-text {
background-image: linear-gradient(90deg, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #8b00ff);
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}
Best Practices
- Use design tokens for colors: Reference your color system with
ref()for consistent backgrounds - Optimize background images: Use appropriate image formats and sizes for performance
- Consider accessibility: Ensure sufficient contrast between backgrounds and foreground content
- Use cover for hero images:
background-size: coveris usually best for full-width hero sections - Test fixed attachments on mobile:
background-attachment: fixedcan cause issues on some mobile browsers - Prefer CSS gradients: Use CSS gradients instead of image files when possible for better performance
FAQ
useBackgroundSizeUtility with responsive modifiers. Use cover for full-bleed images that scale to fill their container, or contain when you need to show the entire image without cropping.background-origin defines where the background image starts (its origin point), while background-clip defines where the background is visible (its clipping area). For gradient text effects, you need background-clip: text.