CSS Typography
Learn how to control text appearance and create beautiful typography on the web
What is CSS Typography?
CSS Typography refers to the styling of text content on web pages using CSS properties. It encompasses everything from font selection and sizing to spacing, alignment, and decorative effects. Good typography is essential for readability, accessibility, and creating visually appealing designs.
Typography Categories:
- Font Properties - family, size, weight, style
- Text Properties - alignment, decoration, transformation
- Spacing Properties - line height, letter spacing, word spacing
- Advanced Features - OpenType features, variable fonts
- Web Fonts - custom fonts using @font-face
CSS Typography Properties Reference
Property | Description | Values |
---|---|---|
font-family | Specifies the font for text | family-name, generic-family, inherit |
font-size | Sets the size of the font | px, em, rem, %, smaller, larger |
font-weight | Sets the thickness of the font | normal, bold, bolder, lighter, 100-900 |
font-style | Sets the style of the font | normal, italic, oblique |
font-variant | Specifies variant representations of the font | normal, small-caps |
line-height | Sets the space between lines of text | normal, number, length, percentage |
letter-spacing | Sets the space between characters | normal, length |
word-spacing | Sets the space between words | normal, length |
text-align | Sets the horizontal alignment of text | left, right, center, justify |
text-decoration | Adds decoration to text | none, underline, overline, line-through |
text-transform | Controls the capitalization of text | none, capitalize, uppercase, lowercase |
text-indent | Indents the first line of text | length, percentage |
text-shadow | Adds shadow to text | h-shadow v-shadow blur color |
white-space | Controls how whitespace is handled | normal, nowrap, pre, pre-wrap, pre-line |
word-break | Controls line breaking within words | normal, break-all, keep-all |
overflow-wrap | Controls whether words can break | normal, break-word, anywhere |
CSS Typography in Action
Example Code
/* CSS Typography Properties */ /* Font Family */ .font-family { font-family: Arial, sans-serif; /* Generic family as fallback */ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; /* Font stack */ font-family: 'Roboto', sans-serif; /* Web font */ } /* Font Size */ .font-size { font-size: 16px; /* Absolute size in pixels */ font-size: 1rem; /* Relative to root element */ font-size: 1em; /* Relative to parent element */ font-size: 100%; /* Percentage of parent's font size */ font-size: larger; /* Relative keywords */ font-size: smaller; } /* Font Weight */ .font-weight { font-weight: normal; /* Normal weight */ font-weight: bold; /* Bold weight */ font-weight: 700; /* Numeric value between 100-900 */ font-weight: lighter; /* Lighter than parent */ font-weight: bolder; /* Bolder than parent */ } /* Font Style */ .font-style { font-style: normal; /* Normal style */ font-style: italic; /* Italic style */ font-style: oblique; /* Oblique style (similar to italic) */ } /* Font Variant */ .font-variant { font-variant: normal; /* Normal variant */ font-variant: small-caps; /* Small caps variant */ } /* Line Height */ .line-height { line-height: normal; /* Browser default */ line-height: 1.5; /* Unitless multiplier */ line-height: 1.5em; /* With units */ line-height: 150%; /* Percentage */ } /* Letter Spacing */ .letter-spacing { letter-spacing: normal; /* Default spacing */ letter-spacing: 2px; /* Positive values increase spacing */ letter-spacing: -1px; /* Negative values decrease spacing */ } /* Word Spacing */ .word-spacing { word-spacing: normal; /* Default spacing */ word-spacing: 5px; /* Positive values increase spacing */ word-spacing: -2px; /* Negative values decrease spacing */ } /* Text Transform */ .text-transform { text-transform: none; /* No transformation */ text-transform: capitalize; /* Capitalizes first letter of each word */ text-transform: uppercase; /* Transforms all text to uppercase */ text-transform: lowercase; /* Transforms all text to lowercase */ text-transform: full-width; /* Transforms to full-width characters */ } /* Text Decoration */ .text-decoration { text-decoration: none; /* No decoration */ text-decoration: underline; /* Underlined text */ text-decoration: overline; /* Overlined text */ text-decoration: line-through; /* Line through text */ text-decoration: underline overline; /* Multiple decorations */ } /* Text Align */ .text-align { text-align: left; /* Aligns text to the left */ text-align: right; /* Aligns text to the right */ text-align: center; /* Centers text */ text-align: justify; /* Justifies text */ text-align: justify-all; /* Justifies all lines including the last */ text-align: start; /* Similar to left in LTR languages */ text-align: end; /* Similar to right in LTR languages */ } /* Text Indent */ .text-indent { text-indent: 0; /* No indentation */ text-indent: 20px; /* Indentation with fixed units */ text-indent: 5%; /* Percentage indentation */ text-indent: 2em; /* Relative to font size */ text-indent: -20px; /* Negative indentation (hanging indent) */ } /* Vertical Align */ .vertical-align { vertical-align: baseline; /* Aligns with baseline */ vertical-align: sub; /* Aligns as subscript */ vertical-align: super; /* Aligns as superscript */ vertical-align: top; /* Aligns with top of line */ vertical-align: text-top; /* Aligns with top of parent's font */ vertical-align: middle; /* Aligns with middle of line */ vertical-align: bottom; /* Aligns with bottom of line */ vertical-align: text-bottom; /* Aligns with bottom of parent's font */ } /* White Space */ .white-space { white-space: normal; /* Normal white space handling */ white-space: nowrap; /* Prevents text wrapping */ white-space: pre; /* Preserves white space */ white-space: pre-wrap; /* Preserves white space but wraps */ white-space: pre-line; /* Collapses white space but preserves line breaks */ white-space: break-spaces; /* Similar to pre-wrap but breaks spaces */ } /* Word Break */ .word-break { word-break: normal; /* Default line break rules */ word-break: break-all; /* Break words between any characters */ word-break: keep-all; /* Don't break words in CJK text */ word-break: break-word; /* Break long words (deprecated, use overflow-wrap) */ } /* Overflow Wrap */ .overflow-wrap { overflow-wrap: normal; /* Default line breaking behavior */ overflow-wrap: anywhere; /* Break words at any point if needed */ overflow-wrap: break-word; /* Break words only if they would overflow */ } /* Text Shadow */ .text-shadow { text-shadow: none; /* No shadow */ text-shadow: 2px 2px 4px rgba(0,0,0,0.5); /* horizontal vertical blur color */ text-shadow: 0 0 5px #ff0000; /* Glow effect */ text-shadow: 1px 1px 0 #000, -1px -1px 0 #000; /* Multiple shadows */ } /* Font Kerning */ .font-kerning { font-kerning: auto; /* Browser decides */ font-kerning: normal; /* Kerning applied */ font-kerning: none; /* No kerning */ } /* Font Variant Ligatures */ .font-variant-ligatures { font-variant-ligatures: normal; /* Normal ligatures */ font-variant-ligatures: none; /* No ligatures */ font-variant-ligatures: common-ligatures; /* Common ligatures like fi, fl */ font-variant-ligatures: no-common-ligatures; /* Disable common ligatures */ font-variant-ligatures: discretionary-ligatures; /* Discretionary ligatures */ font-variant-ligatures: historical-ligatures; /* Historical ligatures */ } /* Font Feature Settings */ .font-feature-settings { font-feature-settings: normal; /* Default settings */ font-feature-settings: "liga" 1; /* Enable ligatures */ font-feature-settings: "kern" 1; /* Enable kerning */ font-feature-settings: "smcp" 1; /* Enable small caps */ font-feature-settings: "liga" 1, "kern" 1; /* Multiple features */ } /* Using CSS variables for typography */ :root { --font-primary: 'Helvetica Neue', Helvetica, Arial, sans-serif; --font-secondary: Georgia, serif; --font-size-base: 16px; --line-height-base: 1.5; --text-color: #333; --heading-color: #222; } .typography-with-vars { font-family: var(--font-primary); font-size: var(--font-size-base); line-height: var(--line-height-base); color: var(--text-color); } .heading-with-vars { font-family: var(--font-secondary); color: var(--heading-color); }
Typography Properties in Detail
Font Family
The font-family
property specifies the font for an element. It's recommended to provide a font stack with fallbacks.
Font Size
The font-size
property sets the size of the font. Use relative units like rem for accessibility.
Font Weight
The font-weight
property sets the thickness or boldness of the font.
Line Height
The line-height
property sets the space between lines of text. Unitless values are recommended.
Letter Spacing
The letter-spacing
property sets the space between characters.
Text Alignment
The text-align
property sets the horizontal alignment of text.
Best Practices for Typography
Readability Tips
- Use a font size of at least 16px for body text
- Limit line length to 50-75 characters for optimal reading
- Use a line height of 1.5-1.6 for body text
- Choose fonts with good readability characteristics
- Maintain sufficient contrast between text and background
- Use proper hierarchy with heading sizes
Common Mistakes to Avoid
- Using too many different fonts on one page
- Using absolute units (px) instead of relative units (rem/em)
- Not providing fallback fonts in font stacks
- Using justified text for short content
- Using all caps for long passages of text
- Not testing typography on different devices and screen sizes
Accessibility Considerations
- Ensure text has a minimum contrast ratio of 4.5:1 for normal text
- Use relative units (rem, em) to allow users to resize text
- Avoid using only color to convey information
- Provide sufficient spacing between lines and letters
- Test your typography with screen readers
- Consider users with dyslexia and other reading disabilities
Working with Web Fonts
Using @font-face
The @font-face
rule allows you to use custom fonts on your website.
@font-face { font-family: 'MyCustomFont'; src: url('mycustomfont.woff2') format('woff2'), url('mycustomfont.woff') format('woff'); font-weight: 400; font-style: normal; font-display: swap; } body { font-family: 'MyCustomFont', sans-serif; }
Google Fonts
Google Fonts provides a large library of free web fonts that are easy to implement.
<!-- Add to HTML head --> <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet"> /* Use in CSS */ body { font-family: 'Roboto', sans-serif; }
Font Loading Strategies
Optimize font loading to prevent layout shifts and improve performance.
- Use
font-display: swap
to prevent FOIT (Flash of Invisible Text) - Preload critical fonts
- Subset fonts to include only necessary characters
- Use modern formats like WOFF2 for better compression
- Consider using system fonts for better performance
Variable Fonts
What are Variable Fonts?
Variable fonts are a single font file that contains multiple variations of a typeface, allowing you to access any weight, width, or style without loading multiple font files.
@font-face { font-family: 'VariableFont'; src: url('variablefont.woff2') format('woff2-variations'); font-weight: 100 900; font-stretch: 75% 125%; } .element { font-family: 'VariableFont', sans-serif; font-weight: 650; /* Any value between 100-900 */ font-stretch: 110%; /* Any value between 75%-125% */ }
Benefits
- Reduced HTTP requests
- Smaller overall file size
- Fine-grained control over typography
- Better performance
Browser Support
Variable fonts are supported in all modern browsers, including:
- Chrome 66+
- Firefox 62+
- Safari 11+
- Edge 17+
Ready to Try It Yourself?
Experiment with CSS typography properties in our interactive editor. See your changes in real-time and build your understanding through practice.