CSS Variables
Fonttrio uses CSS custom properties (CSS variables) to provide a flexible, maintainable system for applying font pairings to your project. This approach makes it easy to swap fonts, customize scales, and maintain consistency across your entire application.Why CSS Variables?
CSS variables offer several advantages for managing typography:- Single source of truth: Define fonts once, use everywhere
- Easy theming: Switch entire pairings by changing a few variables
- Dynamic updates: Change typography at runtime without recompiling
- Scoped customization: Override variables for specific components
- Framework agnostic: Works with any CSS-based framework
Core Font Variables
Every Fonttrio pairing defines three core CSS variables for the font families:Variable Structure in Pairings
Each pairing in the Fonttrio registry defines its CSS variables in thecssVars section:
How Variables are Applied
Fonttrio uses a two-layer variable system for maximum flexibility:Layer 1: Font Family Variables
Individual fonts define their own CSS variable:Layer 2: Semantic Variables
Pairings reference these font variables through semantic names:- Switch pairings by changing only 3 variables
- Override specific fonts while keeping others
- Create variations of existing pairings
Using Variables in Your CSS
Once a pairing is installed, use the semantic variables in your stylesheets:Fonttrio’s Global CSS Setup
Here’s how Fonttrio sets up its own CSS variable system in/home/daytona/workspace/source/app/globals.css:85-98:
app/globals.css
Typography-Specific Variables
Beyond font families, you can create variables for the entire typography scale:Scoped Customization
CSS variables can be scoped to specific elements or components:Scoped variables allow you to use different pairings in different parts of your site without conflicts.
Dark Mode Support
CSS variables make theme switching trivial. Here’s how Fonttrio handles dark mode in/home/daytona/workspace/source/app/globals.css:7-50:
app/globals.css
.dark class is applied, without touching font definitions.
Dynamic Updates with JavaScript
You can change CSS variables programmatically:Fallback Fonts
Always provide fallback fonts for better loading experience:Tailwind CSS Integration
If you’re using Tailwind, extend your config to use CSS variables:tailwind.config.js
Next.js Font Integration
Fonttrio works seamlessly with Next.js font optimization:app/layout.tsx
Variable Naming Conventions
Fonttrio follows these naming conventions:Best Practices
Use Semantic Names: Prefer
--font-heading over --font-playfair so you can swap fonts without changing references.Layer Your Variables: Create base variables for individual fonts, then semantic variables that reference them.
Provide Fallbacks: Always include fallback fonts in case custom fonts fail to load.
Scope Strategically: Use scoped variables for component-level customization without affecting global styles.
Test Everywhere: CSS variables work in all modern browsers, but test your fallbacks in older environments.
Common Patterns
Pattern 1: Pairing Switcher
Pattern 2: Responsive Typography
Pattern 3: User Preferences
Next Steps
Font Pairings
Learn about Fonttrio’s pairing structure
Typography Scale
Understand how typography scales work
Installation
Install Fonttrio and start using CSS variables
Customization
Customize pairings for your project