When we talk about Typography in CSS, we are focusing on the words, the part of your website people actually come for. You can have the perfect color scheme and a flawless layout, but if your text is hard to read or just looks off, the whole thing falls apart. Getting typography in CSS right is what separates a site that feels crafted from one that feels just thrown together. It is not about finding one pretty font. It is about building a complete, readable system that gives your content a clear, confident voice.
For us developers, that means moving past random font sizes and guesswork. CSS gives us a precise set of controls. Let us break down the essential ones you will use every day to take control of how your site reads.
Your Core Toolkit for CSS Typography: The Font Properties
Think of these as your main dials for the basic look of your type.
font-family: Picking Your Typeface
This is where you choose the font. The key here is the font stack. You list your preferred fonts in order, so the browser can use the next one if the first is not available.
css
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
} That is a great system font stack. It is fast and looks native on different operating systems. For a custom look, you would swap this out for a web font later.
font-size: Getting the Scale Right
Here is a common early mistake, using px for everything. It locks the size. For a flexible, accessible site, use relative units.
rem: This is your best friend.1remequals whatever font-size you set on the<html>element. It scales predictably from a single source.em: Relative to the font-size of the parent element. Useful but can get tricky with nesting.
css
html {
font-size: 16px; /* This sets your baseline */
}
h1 {
font-size: 2.5rem; /* 40px */
}
p {
font-size: 1rem; /* 16px */
} font-weight: Controlling Thickness
Forget just bold and normal. Use numeric values like 400 (normal), 600 (semibold), or 700 (bold) for finer control.
css
.subtle-heading {
font-weight: 500; /* A nice medium weight */
} The Readability Crew: Spacing and Alignment
If font properties pick the voice, these control the rhythm and pacing. They are crucial for comfortable reading.
line-height: The Most Important Setting
This is your secret weapon for readable text. It is the space between lines. For body text, a unitless number between 1.4 and 1.6 works wonders.
css
article p {
font-size: 1rem;
line-height: 1.6;
} Using a unitless value, just 1.6 not 1.6em, is key. It means the spacing scales perfectly if the font-size changes.
letter-spacing & text-align: The Finishing Touches
A tiny bit of letter-spacing, like 0.3px, can make headings or uppercase text feel more open. For text-align, be wary of justify. It often creates weird gaps in web text.
Using Custom Web Fonts in Your CSS
System fonts are fast, but sometimes you need a specific brand font. Services like Google Fonts make this simple.
First, you add the font to your HTML.
html
<link rel="preconnect" href="https://fonts.googleapis.com"> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
Then, you use it in your CSS just like any other font.
css
body {
font-family: 'Inter', sans-serif;
} Pro Tip: Only load the font weights you actually need, like wght@400;600 in the link above. Do not slow down your site with extras you will never use.
Building a Typography in CSS System
The goal is not to style each heading individually. It is to create a system where all your text sizes and spaces relate to each other harmoniously. Here is a practical starting point.
css
/* 1. Set your base */
html {
font-size: 16px;
}
body {
font-family: 'Inter', sans-serif;
font-size: 1rem;
line-height: 1.6;
color: #222;
}
/* 2. Define a scale */
h1 { font-size: 2.5rem; font-weight: 700; line-height: 1.2; margin-bottom: 1.5rem; }
h2 { font-size: 2rem; font-weight: 600; line-height: 1.3; margin-bottom: 1rem; }
h3 { font-size: 1.5rem; font-weight: 600; line-height: 1.4; margin-bottom: 0.75rem; }
p { font-size: 1rem; margin-bottom: 1.5rem; } See how the margin-bottom is also in rem? This creates a consistent vertical rhythm where everything on the page is spaced in proportion to your text size. It makes the whole layout feel cohesive.
Accessibility in CSS Typography
Good typography in CSS is inherently accessible. Here are non-negotiable points.
- Contrast: Your text color must stand out clearly against the background. Use a checker tool like the WebAIM Contrast Checker. Aim for a contrast ratio of at least 4.5:1 for normal text.
- Do Not Break Zoom: Using
remunits means you are already supporting users who need to zoom in. Never usepxin a way that prevents text resizing. - Line Length: Long lines are hard to read. Limit your text width.
css
.container {
max-width: 65ch; /* The 'ch' unit is based on character width */
} How to Practice and Learn Typography in CSS
The best way to learn is to steal, ethically. Use your browser’s Developer Tools to inspect the text on websites you admire. Look at their font-weight, line-height, and letter-spacing. Notice the relationships.
Then, start small in your own projects. Implement a base system like the one above. Tweak just the line-height on your body text and see how the feeling of the entire page changes. For the ultimate deep dive on every property, the MDN CSS Fonts module is your go-to reference.
When you approach typography in CSS as a set of connected systems rather than a list of styles, you stop just decorating pages and start crafting real reading experiences. That is when your work starts to feel genuinely professional.
New to HTML? Start Here: HTML Tutorial for Beginners: Your Complete Introduction to HTML Basics
New to CSS? Start Here: CSS Introduction: Master 5 Core Concepts Easily
[INSERT_ELEMENTOR id=”122″]

