Google Fonts Integration
What you'll learn: How to connect Google Fonts to your project and make informed choices about loading methods.
Now that you understand self-hosting versus font services, let's explore the most popular font service: Google Fonts. Google Fonts offers hundreds of free, high-quality typefaces with extremely simple integration—but the method you choose affects page performance.
Two Integration Methods
The <link> Method
Place a link tag in your HTML <head>, just like linking a stylesheet:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
The preconnect links establish early connections to Google's servers, reducing loading delays. This method is straightforward and recommended for most projects.
The @import Method
Alternatively, import fonts directly in your CSS:
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
Performance Considerations
Think of font loading like ordering pizza: The <link> method places the order immediately when the page loads. The @import method waits until the CSS file is parsed before ordering—adding unnecessary delay.
The <link> method is faster because browsers discover it earlier in the parsing process. The @import method blocks rendering until the CSS loads, then requests the font, creating a performance bottleneck.
The display=swap parameter shows fallback text immediately while fonts download, preventing invisible text.
Using Your Google Font
After integration, reference the font in your CSS:
body {
font-family: 'Roboto', sans-serif;
}
Key Takeaway: Use the <link> method with preconnect for fastest Google Fonts integration; avoid @import as it delays font discovery and hurts performance.