CSS Text & Typography - Complete Guide
CSS Text & Typography
Complete Guide to Font Properties, Text Styling, Web Fonts, and CSS Units
Introduction to CSS Typography
Typography is one of the most critical aspects of web design. CSS provides extensive control over text appearance, allowing developers to create beautiful, readable, and engaging content. This guide covers everything from basic font properties to advanced text styling and modern web font implementation.
Font Properties
Font properties control the appearance of text characters. They define the font family, size, weight, style, and variant.
1. font-family
Font Family Examples
Poppins (Sans-serif)
Modern, geometric sans-serif font
Lora (Serif)
Elegant, well-balanced serif font
Courier (Monospace)
Fixed-width font for code
font-size: 1.5rem;
font-size: 150%;
font-weight: bold;
font-weight: 600;
Weight 100 (Thin)
Weight 400 (Normal)
Weight 700 (Bold)
Weight 900 (Black)
font-style: italic;
font-style: oblique;
Normal Style
Italic Style
Oblique Style
font-variant: small-caps;
Normal Variant (ABCD)
Small Caps Variant (ABCD)
/* Individual font properties */
.heading {
font-family: 'Montserrat', 'Arial', sans-serif;
font-size: 2.5rem;
font-weight: 700;
font-style: normal;
font-variant: normal;
}
/* Shorthand font property */
.paragraph {
font: italic small-caps 500 1.1rem/1.6 'Roboto', sans-serif;
/* style variant weight size/line-height family */
}
.body-text {
font-family: 'Open Sans', sans-serif;
font-size: 1rem;
font-weight: 400;
line-height: 1.6;
}
Text Properties
Text properties control the alignment, decoration, transformation, spacing, and other visual aspects of text content.
Color and Alignment
color: rgb(44, 62, 80);
color: hsl(210, 29%, 24%);
Red Text
Green Text
Blue Text
text-align: center;
text-align: right;
text-align: justify;
Left Aligned
Center Aligned
Right Aligned
text-decoration: overline;
text-decoration: line-through;
text-decoration: none;
Underlined Text
Overlined Text
Line Through Text
Text Transformation and Effects
text-transform: lowercase;
text-transform: capitalize;
uppercase text
LOWERCASE TEXT
capitalize each word
text-shadow: 0 0 10px #3498db;
text-shadow: 1px 1px 0 #fff, 2px 2px 0 #000;
Shadow Effect
Glow Effect
3D Effect
Text Spacing and Line Control
line-height: 150%;
line-height: 24px;
Line height 1 (tight)
Second line example
Line height 1.8 (comfortable)
Second line example
letter-spacing: 2px;
letter-spacing: -1px;
Normal Spacing
Wide Spacing
Tight Spacing
word-spacing: 10px;
word-spacing: -2px;
Normal word spacing example
Wide word spacing example
Tight word spacing example
/* Text styling example */
.heading {
color: #2c3e50;
text-align: center;
text-transform: uppercase;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
letter-spacing: 1px;
}
.paragraph {
color: #34495e;
text-align: justify;
line-height: 1.8;
word-spacing: 1px;
}
.link {
color: #3498db;
text-decoration: none;
font-weight: 600;
}
.link:hover {
text-decoration: underline;
text-decoration-color: #2980b9;
}
.code {
font-family: 'Courier New', monospace;
color: #e74c3c;
font-weight: bold;
letter-spacing: 0.5px;
}
Web Fonts & Google Fonts
Web fonts allow you to use custom fonts on your website that aren't installed on users' computers. Google Fonts is a popular, free service that provides hundreds of web fonts.
Google Fonts Implementation
Poppins Bold
Modern geometric sans-serif
Lora Medium
Elegant well-balanced serif
Montserrat Bold
Modern geometric typeface
<!-- Step 1: Add link in HTML head -->
<head>
<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=Poppins:wght@300;400;500;600;700&family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
</head>
<!-- Step 2: Use in CSS -->
<style>
body {
font-family: 'Poppins', sans-serif;
font-weight: 400;
}
h1, h2, h3 {
font-family: 'Roboto', sans-serif;
font-weight: 700;
}
.special-text {
font-family: 'Poppins', sans-serif;
font-weight: 300;
font-style: italic;
}
</style>
Text Overflow & White Space
These properties control how text behaves when it exceeds its container boundaries and how whitespace is handled.
text-overflow: ellipsis;
text-overflow: "…";
white-space: nowrap;
white-space: pre;
white-space: pre-wrap;
This text won't wrap to new line
This text has extra spaces
overflow-wrap: break-word;
overflow-wrap: anywhere;
Supercalifragilisticexpialidocious
CSS Units
CSS provides various units for measuring length, size, and distance. Understanding these units is crucial for responsive design and proper sizing.
Absolute Units
Absolute units are fixed and aren't affected by other elements. They're useful for print styles but less flexible for responsive web design.
1px = 1/96th of 1 inch
Fixed size on all devices
Example:
font-size: 16px;1pt = 1/72 of 1 inch
Common in word processors
Example:
font-size: 12pt;Useful for print media
1cm = 37.8px approximately
Example:
margin: 2cm;Relative Units
Relative units are based on other elements' sizes, making them ideal for responsive design.
Example:
width: 50%; = 50% of parent widthCommon for layouts and containers
1em = current font-size
Compounds in nested elements
Example:
padding: 1.5em;1rem = root (html) font-size
Doesn't compound like em
Example:
font-size: 1.25rem;Great for responsive typography
Example:
font-size: 5vw; scales with viewportUseful for full-height elements
Example:
height: 100vh; = full viewport height| Unit Type | Best For | Responsive | Example Use Case |
|---|---|---|---|
| px | Fixed sizes, borders | No | Border widths, small fixed elements |
| % | Layouts, containers | Yes | Widths, heights relative to parent |
| rem | Font sizes, spacing | Yes | Consistent sizing across site |
| em | Component-level styles | Yes | Buttons, cards, nested components |
| vw/vh | Full-screen elements | Yes | Hero sections, full-page layouts |
/* Setting root font-size for rem calculations */
html {
font-size: 16px; /* 1rem = 16px */
}
/* Using various units */
.container {
width: 90%; /* Percentage of parent */
max-width: 1200px; /* Fixed maximum */
margin: 0 auto;
}
.heading {
font-size: 2.5rem; /* 40px (2.5 * 16px) */
margin-bottom: 1.5em; /* Relative to font-size */
}
.paragraph {
font-size: 1rem; /* 16px */
line-height: 1.6; /* Unitless - multiplies font-size */
max-width: 65ch; /* Character units */
}
.hero-section {
height: 100vh; /* Full viewport height */
width: 100vw; /* Full viewport width */
}
.responsive-text {
font-size: clamp(1rem, 2vw, 1.5rem);
/* Minimum 1rem, scales with viewport, maximum 1.5rem */
}
.button {
padding: 0.75em 1.5em; /* Relative to button's font-size */
font-size: 1.125rem; /* 18px */
}
CSS Typography Practice Compiler
Practice CSS text and typography properties in the live editor below. Try different font properties, text styling, and units to see immediate results in the preview.