Fonts
The Ellemment Stack comes with Tailwind CSS's default font configuration. While this provides a solid foundation, you can easily customize fonts to match your brand identity.
Custom Font Implementation
Step 1: Font Files Setup
-
Create font directory:
mkdir -p ./public/fonts
-
Add font files:
- Download from Google Fonts
- Place files in
./public/fonts
- Generate CSS using Transfonter
- Set Transfonter's fonts directory to
fonts
-
Verify font URLs:
/* Should be relative to public folder */ url('/fonts/yourfont/yourfont-200.woff2')
Step 2: Configuration Updates
-
CSS Variables:
/* tailwind.css */ @layer base { :root { --font-sans: <YourFont>; } }
-
Tailwind Configuration:
import defaultTheme from 'tailwindcss/defaultTheme.js'
// tailwind.config.ts extend: { ...extendedTheme, fontFamily: { sans: ['var(--font-sans)', ...defaultTheme.fontFamily.sans], } }
-
Stylesheet Import:
// app/routes/root.tsx import fontStyleSheetUrl from './styles/yourfont.css?url'
// Add to links array { rel: 'preload', href: fontStyleSheetUrl, as: 'style' }, { rel: 'stylesheet', href: fontStyleSheetUrl },
-
Server Configuration:
// server/index.ts app.use( '/fonts', express.static('public/fonts', { immutable: true, maxAge: '1y' }) )
Performance Optimization
Font Metric Overrides
Prevent Cumulative Layout Shift (CLS) by implementing font metric overrides:
-
Generate Overrides:
# For single font file npx fontpie ./local/font/location.woff2 -w font-weight -s normal/italic -n YourFont ,[object Object],
npx fontpie-from-css ./public/fonts/yourfont/yourfont.css
-
Example Output:
@font-face { font-family: 'CustomFont Fallback'; font-style: normal; font-weight: 200; src: local('Arial'); ascent-override: 103.02%; descent-override: 35.97%; line-gap-override: 0%; size-adjust: 98.13%; }
-
Implementation:
- Add overrides to your font stylesheet
- Include fallback in root configuration:
/* tailwind.css */ @layer base { :root { --font-sans: <YourFont> <YourFontFallback>; } }
Best Practices
-
Font Loading:
- Use
font-display: swap
- Preload critical fonts
- Implement metric overrides
- Consider subsetting for specific character ranges
- Use
-
Performance:
- Compress font files
- Use modern formats (woff2)
- Cache fonts aggressively
- Load only necessary weights/styles
-
Accessibility:
- Ensure readable fallback fonts
- Maintain sufficient contrast ratios
- Consider font size adjustments
- Test with screen readers
Troubleshooting
Common issues and solutions:
-
Layout Shift:
- Verify metric overrides implementation
- Check fallback font configuration
- Monitor CLS in performance tools
-
Font Loading:
- Confirm file paths
- Check CORS configuration
- Verify preload implementation
- Monitor network requests
-
Font Display:
- Test across browsers
- Verify CSS syntax
- Check font-family order
- Confirm weight/style declarations
For more performance considerations, check the performance guide.