Explain Codes LogoExplain Codes Logo

Use multiple @font-face rules in CSS

html
responsive-design
css
font-face
Alex KataevbyAlex KataevΒ·Mar 9, 2025
⚑TLDR

To @font-face or not @font-face, that is the question πŸ€”. Merge several @font-face plans in CSS for varying styles within the same font family. Unify them by setting the font-family label and define font-weight and font-style traits. Linking each style to a distinct src boost your style game ↗️.

@font-face { font-family: 'CustomFont'; src: url('font.woff2') format('woff2'); /* Regular but significant */ } @font-face { font-family: 'CustomFont'; src: url('font-bold.woff2') format('woff2'); font-weight: bold; /* Bold yet balanced */ } @font-face { font-family: 'CustomFont'; src: url('font-italic.woff2') format('woff2'); font-style: italic; /* Italic but not unstable */ }

CSS then suits up each style like a fashion diva 🀩 with font-weight or font-style:

.normal { font-family: 'CustomFont'; } /* Regular, not vanilla */ .bold { font-family: 'CustomFont'; font-weight: bold; } /* Bold as love */ .italic { font-family: 'CustomFont'; font-style: italic; } /* Italic like the Leaning Tower of Pisa */

Essential and apparent, each style gets loaded as requested, upgrading performance and design accuracy.

Assuring cross-device compatibility and top-tier performance

While dancing with @font-face, remember to spin multiple formats that ensure a cross-device embrace. Encompass diverse source files in your src property to enable a widespread user engagement. Contemporary browsers primarily support WOFF and WOFF2 formats, with WOFF2 owning the dance floor due to better compression πŸ•Ί.

@font-face { font-family: 'OptimizedFont'; src: url('optimized-font.woff2') format('woff2'), url('optimized-font.woff') format('woff'); font-weight: normal; font-style: normal; /* Normalize to optimise */ }

Optimizing font weight-lifting πŸ‹οΈβ€β™€οΈ

In the web development arena, where speed is the champion πŸ₯‡, optimizing font loading is essential. Harness techniques such as font subsetting to trim down file sizes or font loading APIs to control how the fonts behave. Consider font-display to manage the situation when fonts behave like introverts, reducing the flash of unstyled text (FOUT) or flash of invisible text (FOIT).

Backup dancers: Fallback fonts

Much like a famous band has backup singers, always have web-safe backup fonts in your font-family group, ready to jump in and save the day when the primary font can't make it to the stage 🎀.

.body-text { font-family: 'CustomFont', Arial, sans-serif; /* Always go with a plan B(Arial, sans-serif here)! */ }

Responsiveness: the crowd-pleaser

Test and ensure readability across various devices and audience sizes. Choose audience-friendly CSS units like em, rem, and %'s that let fonts scale conveniently with the screen size, much like a charismatic conductor.

Efficiently Managing font variations

Using font-weight and style as an organizing toolkit

Ensure your font closet is well-organized by distinguishing different font files (weights and styles) in separate @font-face rules. Your font-weight and font-style in the @font-face should mirror their respective usage in CSS selectors to maintain fashion-forward consistency πŸ’Ό.

@font-face { font-family: 'VariationFont'; src: url('variation-font-regular.woff2') format('woff2'); font-weight: normal; /* Normal, but never boring */ } @font-face { font-family: 'VariationFont'; src: url('variation-font-thin.woff2') format('woff2'); font-weight: 100; /* Thin but not skinny */ }

Assigning specific typefaces: Customizing user experience

Serving a customized font dish for UI components like headers, paragraph text, and buttons leads to a unique and engaging user experience, much like fresh dinner at a 5-star restaurant πŸ’».

#branding { font-family: 'VariationFont', sans-serif; font-weight: 300; } .article-content { font-family: 'VariationFont', serif; font-weight: normal; }

Testing and validation: the final pit-stop πŸ›‘

Validation is always important. Especially after executing your @font-face commands, visually verify the implementation across different browsers and devices. Ensure each font variation is parading as expected.