font-weight and font-style Mapping
What you'll learn: How to connect multiple font files to a single font family so browsers display the correct weight and style.
When you download a font like "Roboto," it often comes as separate files for each variant: regular, bold, italic, bold-italic, light, etc. Your job is to teach the browser which file to use when you apply font-weight: bold or font-style: italic in your CSS.
Why Mapping Matters
Without proper mapping, browsers might fake bold or italic styles by algorithmically distorting your regular font file—creating ugly, inconsistent typography. By mapping each file correctly, you ensure authentic rendering.
How It Works
Inside each @font-face declaration, you specify:
font-family— the shared name (like "Roboto")font-weight— the weight this file represents (100–900)font-style— whether this file is normal or italicsrc— the actual file path
@font-face {
font-family: 'Roboto';
src: url('roboto-regular.woff2') format('woff2');
font-weight: 400;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('roboto-bold.woff2') format('woff2');
font-weight: 700;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('roboto-italic.woff2') format('woff2');
font-weight: 400;
font-style: italic;
}
Now when you write font-family: 'Roboto'; font-weight: 700; the browser knows to fetch roboto-bold.woff2.
Think of It Like a Wardrobe
Your "Roboto" family is the wardrobe. Each @font-face rule is a labeled drawer: "casual outfit, weight 400," "formal outfit, weight 700," etc. When you request a style, the browser opens the right drawer.
Key Takeaway: Use separate @font-face declarations with matching font-weight and font-style descriptors to map each font file accurately, ensuring browsers render the intended typeface variant.