Explain Codes LogoExplain Codes Logo

How do I use .woff fonts for my website?

html
custom-fonts
font-loading
browser-compatibility
Anton ShumikhinbyAnton Shumikhin·Aug 19, 2024
TLDR

To use .woff fonts in your site, you only need to define a CSS @font-face rule, as shown below:

@font-face { font-family: 'YourFont'; /* Remember: Be creative with names, but no Comic Sans */ src: url('fonts/YourFont.woff2') format('woff2'), /* Like the cool kid in the town */ url('fonts/YourFont.woff') format('woff'); /* Backup dancer for older browsers */ } body { font-family: 'YourFont', Arial, sans-serif; /* Arial as bodyguard when custom font fails */ }

Make sure your replace 'YourFont' with your custom font name, and 'fonts/YourFont.woff2' as well as 'fonts/YourFont.woff' with your font paths. This process loads the .woff font, while using fallback default system fonts in situations where the custom font cannot load.

Custom font checklist: Dos and Don'ts

Always consider browser compatibility and the performance implications of using custom fonts. Do your homework on how your font fares across different browsers and operating systems.

For an optimal performance, prioritize the .woff2 format as it has better compression capabilities, which effectively means faster load times. Side note: No, .woff2 is not a sequel to .woff, it's an improvement.

Also remember, proper font loading techniques such as font loading APIs or the CSS font-display property can help avoid the flash of unstyled text (FOUT) or flash of invisible text (FOIT).

Storing fonts: All in one place

Organizing your custom fonts

Place your custom font files in a structured directory, typically a fonts subfolder, to keep your workspace tidy.

Serving fonts efficiently

To decrease site loading times, make sure you enable HTTP compression in your server setup for .woff and .woff2 files and that browser caching is optimized to reduce repeat downloads for recurring visitors.

Browser support: Not all are alike

Firefox compatibility

Firefox users deserve the best too. To ensure Firefox compatibility, make sure your server has the correct MIME types configured for serving .woff and .woff2 formats. Put simply, Firefox needs this information to understand the file type it's dealing with.

Supporting older browsers

Offer a fallback for older browsers (Yes, there are people still using old browsers. Some even think the world is flat.) that do not support our hero .woff by including an alternative font format, such as Embedded OpenType (.eot).

Simple steps: Visualizing font usage

1. Acquire your font: **Find your .woff or .woff2 file** 2. Check for browser support: **Confirm if the font works in all browsers** 3. Arrange your font files: **Put the font files in a specific directory** 4. Apply your font: **Use CSS to apply the font to HTML elements**

Just like baking a cake! Your ingredients are ready, let's move on to baking.

Upgrading your font usage

Implementing uniqueness

Do you want your text to have a beard? You can make it bold. Want to tilt it a bit? Make it italic. By using distinct @font-face rules for each font style and weight, you can easily manage each variant.

Using local fonts

Why download a font file if it's already there? If some users might already have your font installed locally, you can use the local() function. But proceed with caution! Sometimes, the local versions might differ from your intended font.

Applying and enhancing your custom fonts

Tailoring your typography

Apply your font to specific elements or to the body or html tag for a generic solution, giving a consistent look across your website.

Planning for the worst

Always, and I mean always, define a fallback font. In situations where your first-choice custom font fails to load, you'll thank yourself for having a plan B.