css-variables Snippet
snippets/css-variables.liquid injects theme CSS custom properties (CSS variables) for typography and color schemes. It outputs @font-face rules for all font variations and generates per-scheme CSS variables used across the theme. The component also includes Alpine.js x-cloak styling to prevent flash of unstyled content.
What It Does
- Generates
@font-facerules for body and heading fonts with all variations. - Creates CSS custom properties for each color scheme.
- Calculates background contrast colors dynamically.
- Sets typography CSS variables (font families, weights, styles).
- Sets layout CSS variables (page width, margins).
- Includes Alpine.js
x-cloakstyling. - Outputs body color rules for all color schemes.
Parameters
None. This snippet takes no parameters.
Dependencies & Assets
| Type | Files / Components |
|---|---|
| CSS | Inline {% style %} block with all CSS variables |
| JavaScript | None required (but Alpine.js x-cloak styling included) |
| Shopify Filters | font_face, font_modify, color_brightness, color_lighten, color_darken |
| Data | Requires theme settings: type_body_font, type_heading_font, color_schemes, max_page_width, min_page_margin |
- All CSS generated inline via
{% style %}block. - No external CSS files required.
- Shopify font and color filters handle font loading and color calculations.
Dynamic Styles
The snippet generates comprehensive inline styles including:
Alpine.js Cloak
[x-cloak] {
display: none !important;
}- Alpine.js support: Hides elements with
x-cloakattribute until Alpine.js loads.
Font Face Rules
{{ settings.type_body_font | font_face: font_display: 'swap' }}
{{ settings.type_body_font | font_modify: 'weight', 'bold' | font_face: font_display: 'swap' }}
{{ settings.type_body_font | font_modify: 'weight', 'bold' | font_modify: 'style', 'italic' | font_face: font_display: 'swap' }}
{{ settings.type_body_font | font_modify: 'style', 'italic' | font_face: font_display: 'swap' }}- Font variations: Generates
@font-facerules for:- Regular
- Bold
- Bold italic
- Italic
Color Scheme Variables
.color-{{ scheme.id }} {
--color-background: {{ scheme.settings.background.red }},{{ scheme.settings.background.green }},{{ scheme.settings.background.blue }};
--color-foreground: {{ scheme.settings.text.red }},{{ scheme.settings.text.green }},{{ scheme.settings.text.blue }};
/* ... additional color variables ... */
}- Per-scheme variables: Each color scheme gets its own CSS variables.
- RGB format: Colors stored as RGB values (comma-separated) for use with
rgba()andrgb().
Typography Variables
:root {
--font-body-family: {{ settings.type_body_font.family }}, {{ settings.type_body_font.fallback_families }};
--font-body-weight: {{ settings.type_body_font.weight }};
--font-heading-family: {{ settings.type_heading_font.family }}, {{ settings.type_heading_font.fallback_families }};
/* ... additional typography variables ... */
}- Font families: Includes font family and fallback families.
- Font weights: Includes regular and bold weights.
- Font scales: Includes scale variables (default: 1).
Layout Variables
:root {
--page-width: {{ settings.max_page_width }}px;
--page-margin: {{ settings.min_page_margin }}px;
}- Page width: Maximum page width in pixels.
- Page margin: Minimum page margin in pixels.
Markup Structure
The snippet outputs only CSS (no HTML markup). It uses a {% style %} block to generate inline styles.
Behavior
- Font loading: Generates
@font-facerules for all font variations withfont-display: swap. - Color scheme generation: Loops through all color schemes and generates CSS variables.
- Contrast calculation: Dynamically calculates background contrast color based on brightness.
- Variable scope: Variables scoped to
:rootand.color-[scheme-id]classes. - Body styling: Applies color rules to body and all color scheme classes.
Usage Example
{% render 'css-variables' %}Typically used in:
- Theme layout (
layout/theme.liquid) in<head>section
Implementation Notes
No parameters: Snippet takes no parameters - reads all data from theme settings.
Font face generation: Generates
@font-facerules for:- Body font: regular, bold, bold italic, italic
- Heading font: regular, bold, bold italic, italic
Font display swap: All fonts use
font-display: swapfor better performance.Color scheme loop: Loops through
settings.color_schemesto generate variables for each scheme.Scheme class generation: Builds selector list for body color rules:
body, .color-scheme1, .color-scheme2, ...RGB color format: Colors stored as RGB values (comma-separated) for use with:
rgb(var(--color-background))rgba(var(--color-foreground), 0.75)
Background contrast calculation: Dynamically calculates contrast color based on background brightness:
- Brightness ≤ 26: Lighten by 50%
- Brightness ≤ 65: Lighten by 5%
- Brightness > 65: Darken by 25%
Gradient background: Uses
background_gradientif set, otherwise falls back tobackground.Color variables generated:
--color-background--gradient-background--color-foreground--color-background-contrast--color-shadow--color-button--color-button-text--color-secondary-button--color-secondary-button-text--color-link--color-badge-foreground--color-badge-background--color-badge-border--payment-terms-background-color
Typography variables generated:
--font-body-family--font-body-style--font-body-weight--font-body-weight-bold--font-body-scale--font-heading-family--font-heading-style--font-heading-weight--font-heading-scale
Layout variables generated:
--page-width--page-margin
Alpine.js cloak: Includes
[x-cloak]styling to prevent flash of unstyled content.Root variables: Typography and layout variables set on
:rootfor global access.Scheme-specific variables: Color variables set on
.color-[scheme-id]classes.Body color rules: Applies color rules to
bodyand all color scheme classes.Font modification: Uses
font_modifyfilter to create font variations (bold, italic).Color brightness: Uses
color_brightnessfilter to calculate background brightness.Color manipulation: Uses
color_lightenandcolor_darkenfilters for contrast calculation.Scheme class building: Builds comma-separated list of scheme classes for body selector.
First scheme handling: First scheme uses
:root,prefix in selector.RGB extraction: Extracts RGB values from color objects for CSS variable format.
Gradient fallback: Falls back to solid background if gradient not set.
Payment terms color: Includes special variable for payment terms background.
Font fallback families: Includes fallback font families for better compatibility.
Bold weight calculation: Calculates bold weight as
weight + 300capped at 900.Font scale defaults: Font scales default to 1 (can be overridden).
Page width format: Page width includes
pxunit.Page margin format: Page margin includes
pxunit.Body color opacity: Body foreground color uses
rgba()with 0.75 opacity.Body background: Body background uses
rgb()for solid color.No HTML output: Snippet outputs only CSS, no HTML markup.
Inline styles: All CSS generated inline in
<style>tag.Performance: Inline styles prevent additional HTTP request but increase HTML size.
Font loading strategy:
font-display: swapensures text remains visible during font load.Color scheme flexibility: Each color scheme can have different color values.
Variable naming: Variables use kebab-case naming convention.
RGB format advantage: RGB format allows opacity control via
rgba().Contrast calculation logic: Contrast calculation ensures readable text on any background.
Gradient support: Supports both gradient and solid backgrounds.
Scheme class prefix: All scheme classes use
.color-prefix.Body selector: Body and scheme classes share same color rules.
Font weight limits: Bold weight calculation ensures it doesn't exceed 900.
Font style support: Supports both regular and italic font styles.
Font weight support: Supports regular and bold font weights.
Fallback families: Includes fallback font families for better compatibility.
Payment terms integration: Includes special variable for payment terms component.
Badge colors: Includes separate variables for badge foreground, background, and border.
Button colors: Includes separate variables for primary and secondary buttons.
Link color: Link color uses secondary button label color.
Shadow color: Includes shadow color variable for depth effects.