class BackgroundAttachment(Enum):"""Background attachment utility values""" FIXED ="fixed" LOCAL ="local" SCROLL ="scroll"bg_attachment = enums_to_simple_factory("bg", [BackgroundAttachment], "Background attachment utilities for controlling how a background image behaves when scrolling") # Background attachment factory
test_backgrounds_attachment_examples
def test_backgrounds_attachment_examples():
Test background attachment utilities.
Exported source
def test_backgrounds_attachment_examples():"""Test background attachment utilities."""assertstr(bg_attachment.fixed) =="bg-fixed"assertstr(bg_attachment.local) =="bg-local"assertstr(bg_attachment.scroll) =="bg-scroll"# Run the teststest_backgrounds_attachment_examples()
Background Clip
Control the bounding box of an element’s background:
class BackgroundClip(Enum):"""Background clip utility values""" BORDER ="border" PADDING ="padding" CONTENT ="content" TEXT ="text"bg_clip = enums_to_simple_factory("bg-clip", [BackgroundClip], "Background clip utilities for controlling the bounding box of an element's background") # Background clip factory
test_backgrounds_clip_examples
def test_backgrounds_clip_examples():
Test background clip utilities.
Exported source
def test_backgrounds_clip_examples():"""Test background clip utilities."""assertstr(bg_clip.border) =="bg-clip-border"assertstr(bg_clip.padding) =="bg-clip-padding"assertstr(bg_clip.content) =="bg-clip-content"assertstr(bg_clip.text) =="bg-clip-text"# Run the teststest_backgrounds_clip_examples()
Background Color
The most commonly used background utility - sets the background color of an element:
Exported source
bg = ColoredFactory("bg", "Background color utilities for controlling an element's background color") # The background color factory
Basic Background Colors
Apply background colors using the standard color palette:
test_backgrounds_color_examples
def test_backgrounds_color_examples():
Test background color utilities with various color values.
Exported source
def test_backgrounds_color_examples():"""Test background color utilities with various color values."""# Test all 22 Tailwind color familiesassertstr(bg.red._500) =="bg-red-500"assertstr(bg.orange._500) =="bg-orange-500"assertstr(bg.amber._500) =="bg-amber-500"assertstr(bg.yellow._500) =="bg-yellow-500"assertstr(bg.lime._500) =="bg-lime-500"assertstr(bg.green._500) =="bg-green-500"assertstr(bg.emerald._500) =="bg-emerald-500"assertstr(bg.teal._500) =="bg-teal-500"assertstr(bg.cyan._500) =="bg-cyan-500"assertstr(bg.sky._500) =="bg-sky-500"assertstr(bg.blue._500) =="bg-blue-500"assertstr(bg.indigo._500) =="bg-indigo-500"assertstr(bg.violet._500) =="bg-violet-500"assertstr(bg.purple._500) =="bg-purple-500"assertstr(bg.fuchsia._500) =="bg-fuchsia-500"assertstr(bg.pink._500) =="bg-pink-500"assertstr(bg.rose._500) =="bg-rose-500"assertstr(bg.slate._500) =="bg-slate-500"assertstr(bg.gray._500) =="bg-gray-500"assertstr(bg.zinc._500) =="bg-zinc-500"assertstr(bg.neutral._500) =="bg-neutral-500"assertstr(bg.stone._500) =="bg-stone-500"# Test different shadesassertstr(bg.blue._300) =="bg-blue-300"assertstr(bg.green._950) =="bg-green-950"# Special colorsassertstr(bg.transparent) =="bg-transparent"assertstr(bg.black) =="bg-black"assertstr(bg.white) =="bg-white"assertstr(bg.current) =="bg-current"assertstr(bg.inherit) =="bg-inherit"# Run the teststest_backgrounds_color_examples()
Background Opacity
Control the opacity of background colors:
test_backgrounds_opacity_examples
def test_backgrounds_opacity_examples():
Test background colors with opacity modifiers.
Exported source
def test_backgrounds_opacity_examples():"""Test background colors with opacity modifiers."""# Standard opacity valuesassertstr(bg.red._500.opacity(50)) =="bg-red-500/50"assertstr(bg.blue._300.opacity(75)) =="bg-blue-300/75"assertstr(bg.black.opacity(10)) =="bg-black/10"# Arbitrary opacity valuesassertstr(bg.green._600.opacity("[0.87]")) =="bg-green-600/[0.87]"assertstr(bg.purple._400.opacity("(--my-opacity)")) =="bg-purple-400/(--my-opacity)"# Run the teststest_backgrounds_opacity_examples()
def GradientUtility( gradient_type:str, # Type of gradient (linear, radial, conic) value:Union=None, # Direction, angle, or custom value negative:bool=False, # Whether to negate the angle):
Utility class for gradient backgrounds with angle support.
Gradient Backgrounds
Create gradient backgrounds with various types:
GradientFactory
def GradientFactory( gradient_type:str, # Type of gradient (linear, radial, conic) doc:Optional=None, # Documentation):
Factory for gradient utilities with angle and direction support.
def GradientStopUtility( stop_type:str, # Type of stop (from, via, to) value:Union=None, # Color or percentage is_position:bool=False, # Whether this is a position value):
Utility class for gradient color stops with percentage support.
GradientStopFactory
def GradientStopFactory( stop_type:str, # Type of stop (from, via, to) doc:Optional=None, # Documentation):
Enhanced factory for gradient color stops with percentage support.
Exported source
# Create enhanced gradient stop factoriesfrom_color = GradientStopFactory("from", "Gradient from color/position utilities for defining the starting color or position of a gradient")via_color = GradientStopFactory("via", "Gradient via color/position utilities for defining the middle color or position of a gradient")to_color = GradientStopFactory("to", "Gradient to color/position utilities for defining the ending color or position of a gradient")
Test Background Image Utilities
test_backgrounds_gradient_examples
def test_backgrounds_gradient_examples():
Test gradient background utilities.
Exported source
def test_backgrounds_gradient_examples():"""Test gradient background utilities."""# Test bg-noneassertstr(bg_none) =="bg-none"# Test linear gradient directionsassertstr(bg_linear.to_t) =="bg-linear-to-t"assertstr(bg_linear.to_tr) =="bg-linear-to-tr"assertstr(bg_linear.to_r) =="bg-linear-to-r"assertstr(bg_linear.to_br) =="bg-linear-to-br"assertstr(bg_linear.to_b) =="bg-linear-to-b"assertstr(bg_linear.to_bl) =="bg-linear-to-bl"assertstr(bg_linear.to_l) =="bg-linear-to-l"assertstr(bg_linear.to_tl) =="bg-linear-to-tl"# Test linear gradient anglesassertstr(bg_linear._45) =="bg-linear-45"assertstr(bg_linear._90) =="bg-linear-90"assertstr(bg_linear._180) =="bg-linear-180"assertstr(bg_linear(45)) =="bg-linear-45"assertstr(bg_linear(90, negative=True)) =="-bg-linear-90"assertstr(bg_linear.neg_45) =="-bg-linear-45"# Test radial gradientassertstr(bg_radial()) =="bg-radial"assertstr(bg_radial("--gradient-shape")) =="bg-radial-(--gradient-shape)"assertstr(bg_radial("circle at center")) =="bg-radial-[circle at center]"# Test conic gradientassertstr(bg_conic._45) =="bg-conic-45"assertstr(bg_conic(90)) =="bg-conic-90"assertstr(bg_conic(180, negative=True)) =="-bg-conic-180"assertstr(bg_conic("--conic-angle")) =="bg-conic-(--conic-angle)"assertstr(bg_conic("from 45deg at center")) =="bg-conic-[from 45deg at center]"# Run the teststest_backgrounds_gradient_examples()
test_backgrounds_gradient_stops_examples
def test_backgrounds_gradient_stops_examples():
Test gradient color stop utilities.
Exported source
def test_backgrounds_gradient_stops_examples():"""Test gradient color stop utilities."""# Test from colorsassertstr(from_color.red._500) =="from-red-500"assertstr(from_color.blue._300) =="from-blue-300"assertstr(from_color.transparent) =="from-transparent"assertstr(from_color.black.opacity(50)) =="from-black/50"# Test from percentagesassertstr(from_color._0) =="from-0%"assertstr(from_color._50) =="from-50%"assertstr(from_color._100) =="from-100%"assertstr(from_color(25)) =="from-25%"assertstr(from_color("33%")) =="from-33%"# Test from custom properties and arbitrary valuesassertstr(from_color("--gradient-start")) =="from-(--gradient-start)"assertstr(from_color("10px")) =="from-[10px]"# Test via colorsassertstr(via_color.green._400) =="via-green-400"assertstr(via_color.purple._600) =="via-purple-600"assertstr(via_color.white) =="via-white"assertstr(via_color.yellow._200.opacity(75)) =="via-yellow-200/75"# Test via percentagesassertstr(via_color._50) =="via-50%"assertstr(via_color(75)) =="via-75%"# Test to colorsassertstr(to_color.pink._500) =="to-pink-500"assertstr(to_color.indigo._700) =="to-indigo-700"assertstr(to_color.current) =="to-current"assertstr(to_color.cyan._300.opacity(25)) =="to-cyan-300/25"# Test to percentagesassertstr(to_color._100) =="to-100%"assertstr(to_color(90)) =="to-90%"assertstr(to_color("--gradient-end")) =="to-(--gradient-end)"# Run the teststest_backgrounds_gradient_stops_examples()
Test composing gradient backgrounds with color stops.
Exported source
def test_backgrounds_gradient_composition_examples():"""Test composing gradient backgrounds with color stops."""from fasthtml.common import Divfrom cjm_fasthtml_tailwind.utilities.spacing import pfrom cjm_fasthtml_tailwind.utilities.typography import font_size, text_color# Simple two-color gradient gradient1 = Div("Gradient Background", cls=combine_classes( bg_linear.to_r, from_color.blue._500, to_color.purple._600, p(8), text_color.white ) )assert"bg-linear-to-r"in gradient1.attrs['class']assert"from-blue-500"in gradient1.attrs['class']assert"to-purple-600"in gradient1.attrs['class']# Three-color gradient with via gradient2 = Div("Three Color Gradient", cls=combine_classes( bg_linear.to_br, from_color.red._400, via_color.yellow._500, to_color.green._600, p(8), text_color.white ) )assert"via-yellow-500"in gradient2.attrs['class']# Gradient with angle gradient3 = Div("Angled Gradient", cls=combine_classes( bg_linear._45, from_color.indigo._500, to_color.pink._500, p(8) ) )assert"bg-linear-45"in gradient3.attrs['class']# Radial gradient radial = Div("Radial Gradient", cls=combine_classes( bg_radial(), from_color.indigo._300, to_color.pink._500, p(16) ) )assert"bg-radial"in radial.attrs['class']# Conic gradient conic = Div("Conic Gradient", cls=combine_classes( bg_conic._180, from_color.red._500, via_color.yellow._500, to_color.blue._500, p(16) ) )assert"bg-conic-180"in conic.attrs['class']# Gradient with opacity gradient_opacity = Div("Semi-transparent gradient", cls=combine_classes( bg_linear.to_t, from_color.black.opacity(90), to_color.transparent, p(8) ) )assert"from-black/90"in gradient_opacity.attrs['class']assert"to-transparent"in gradient_opacity.attrs['class']# Gradient with position percentages gradient_positions = Div("Gradient with positions", cls=combine_classes( bg_linear.to_r, from_color.blue._500, from_color._10, # Start at 10% via_color.green._500, via_color._50, # Middle at 50% to_color.red._500, to_color._90, # End at 90% p(8) ) )assert"from-10%"in gradient_positions.attrs['class']assert"via-50%"in gradient_positions.attrs['class']assert"to-90%"in gradient_positions.attrs['class']# Run the teststest_backgrounds_gradient_composition_examples()
Background Origin
Control how an element’s background is positioned relative to borders, padding, and content:
test_backgrounds_origin_examples
def test_backgrounds_origin_examples():
Test background origin utilities.
Exported source
bg_origin = SimpleFactory( {"border": "bg-origin-border","padding": "bg-origin-padding","content": "bg-origin-content" },"Background origin utilities for controlling how an element's background is positioned relative to borders, padding, and content") # Background origin factory
Exported source
def test_backgrounds_origin_examples():"""Test background origin utilities."""assertstr(bg_origin.border) =="bg-origin-border"assertstr(bg_origin.padding) =="bg-origin-padding"assertstr(bg_origin.content) =="bg-origin-content"# Run the teststest_backgrounds_origin_examples()
Background Position
Control the position of background images:
test_backgrounds_position_examples
def test_backgrounds_position_examples():
Test background position utilities.
Exported source
bg_position = SimpleFactory( {"top_left": "bg-top-left","top": "bg-top","top_right": "bg-top-right","left": "bg-left","center": "bg-center","right": "bg-right","bottom_left": "bg-bottom-left","bottom": "bg-bottom","bottom_right": "bg-bottom-right" },"Background position utilities for controlling the position of an element's background image") # Background position factory
Exported source
def test_backgrounds_position_examples():"""Test background position utilities."""assertstr(bg_position.top_left) =="bg-top-left"assertstr(bg_position.top) =="bg-top"assertstr(bg_position.top_right) =="bg-top-right"assertstr(bg_position.center) =="bg-center"assertstr(bg_position.bottom) =="bg-bottom"# Run the teststest_backgrounds_position_examples()
Background Repeat
Control the repetition of background images:
test_backgrounds_repeat_examples
def test_backgrounds_repeat_examples():
Test background repeat utilities.
Exported source
bg_repeat = SimpleFactory( {"repeat": "bg-repeat","repeat_x": "bg-repeat-x","repeat_y": "bg-repeat-y","repeat_space": "bg-repeat-space","repeat_round": "bg-repeat-round","no_repeat": "bg-no-repeat" },"Background repeat utilities for controlling the repetition of an element's background image") # Background repeat factory
Exported source
def test_backgrounds_repeat_examples():"""Test background repeat utilities."""assertstr(bg_repeat.repeat) =="bg-repeat"assertstr(bg_repeat.repeat_x) =="bg-repeat-x"assertstr(bg_repeat.repeat_y) =="bg-repeat-y"assertstr(bg_repeat.no_repeat) =="bg-no-repeat"# Run the teststest_backgrounds_repeat_examples()
Background Size
Control the background size of an element’s background image:
test_backgrounds_size_examples
def test_backgrounds_size_examples():
Test background size utilities.
Exported source
bg_size = SimpleFactory( {"auto": "bg-auto","cover": "bg-cover","contain": "bg-contain" },"Background size utilities for controlling the background size of an element's background image") # Background size factory
Exported source
def test_backgrounds_size_examples():"""Test background size utilities."""assertstr(bg_size.auto) =="bg-auto"assertstr(bg_size.cover) =="bg-cover"assertstr(bg_size.contain) =="bg-contain"# Run the teststest_backgrounds_size_examples()
Arbitrary Background Values
Test support for arbitrary background values:
test_backgrounds_arbitrary_examples
def test_backgrounds_arbitrary_examples():
Test background utilities with arbitrary values.
Exported source
def test_backgrounds_arbitrary_examples():"""Test background utilities with arbitrary values."""# Arbitrary color valuesassertstr(bg("#123456")) =="bg-[#123456]"assertstr(bg("rgb(10, 20, 30)")) =="bg-[rgb(10, 20, 30)]"assertstr(bg("linear-gradient(to right, red, blue)")) =="bg-[linear-gradient(to right, red, blue)]"# CSS custom propertiesassertstr(bg("--custom-bg-color")) =="bg-(--custom-bg-color)"assertstr(bg("--theme-background")) =="bg-(--theme-background)"# Run the teststest_backgrounds_arbitrary_examples()
Practical Examples
Let’s see how to use these background utilities in real FastHTML components:
test_backgrounds_fasthtml_examples
def test_backgrounds_fasthtml_examples():
Test background utilities in practical FastHTML component examples.
Exported source
def test_backgrounds_fasthtml_examples():"""Test background utilities in practical FastHTML component examples."""from fasthtml.common import Div, H1, P, Sectionfrom cjm_fasthtml_tailwind.utilities.spacing import pfrom cjm_fasthtml_tailwind.utilities.borders import roundedfrom cjm_fasthtml_tailwind.utilities.typography import font_size, text_colorfrom cjm_fasthtml_tailwind.utilities.layout import positionfrom cjm_fasthtml_tailwind.utilities.flexbox_and_grid import gap, grid_display# Card with colored background card = Div( H1("Welcome", cls=str(text_color.white)), P("This is a card with a blue background"), cls=combine_classes(bg.blue._500, p(6), rounded.lg) )assert"bg-blue-500"in card.attrs['class']# Hero section with gradient overlay hero = Section( Div( H1("Hero Title"), cls=combine_classes(bg.black.opacity(50), p(8)) ), cls=combine_classes( bg_size.cover, bg_position.center, bg_repeat.no_repeat, position.relative ) )assertstr(bg.black.opacity(50)) =="bg-black/50"assert"bg-cover"in hero.attrs['class']assert"bg-center"in hero.attrs['class']# Text with gradient background using v4 syntax gradient_text = H1("Gradient Text", cls=combine_classes( bg_clip.text, text_color.transparent, bg_linear.to_r, from_color.blue._500, to_color.red._600, font_size._7xl ) )assert"bg-clip-text"in gradient_text.attrs['class']assert"bg-linear-to-r"in gradient_text.attrs['class']assert"from-blue-500"in gradient_text.attrs['class']assert"to-red-600"in gradient_text.attrs['class']# Return all examples in a grid layoutreturn Div( card, hero, gradient_text, cls=combine_classes(grid_display, gap(5)) )# Run the teststest_backgrounds_fasthtml_examples()