DevAcademy
LearnCSSText & Fonts
BeginnerCSS

Text & Fonts

Style text with font family, size, weight, spacing, alignment, and line height.

Reading Time

16 min

Lesson

Lesson 8 of 30

Font Properties

CSS provides several properties to control typography: font-family for the typeface, font-size for scale, font-weight for boldness, and more.

Basic Font Styling

<style>
  p {
    font-family: 'Segoe UI', Arial, sans-serif;
    font-size: 18px;
    font-weight: 600;
    line-height: 1.6;
  }
</style>

<p>This text is styled with a custom font family, size, and weight.</p>
Output

Common Text Properties

PropertyPurpose
font-familyThe typeface, with fallbacks
font-sizeThe size of the text
font-weightBoldness, from 100 (thin) to 900 (black)
font-styleitalic or normal
line-heightVertical spacing between lines
letter-spacingHorizontal space between characters
text-alignleft, center, right, or justify
text-decorationunderline, line-through, or none
text-transformuppercase, lowercase, or capitalize

Text Alignment and Decoration

<style>
  .centered {
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 2px;
  }
  .link {
    text-decoration: none;
    color: steelblue;
  }
  .link:hover {
    text-decoration: underline;
  }
</style>

<h2 class="centered">Section Title</h2>
<a href="#" class="link">Hover this link</a>
Output

Font Fallbacks

font-family accepts a comma-separated list. The browser uses the first font available on the user’s system, falling back to the next, ending in a generic family like sans-serif or serif as a safety net.

Web Fonts

Custom web fonts (like Google Fonts) are loaded with @font-face or a <link> tag, then referenced by name in font-family just like a system font.

Loading a Web Font

<link
  href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap"
  rel="stylesheet"
/>

<style>
  body {
    font-family: 'Inter', sans-serif;
  }
</style>
Output

Best Practice

Always end a font-family list with a generic fallback (sans-serif or serif), and keep line-height around 1.4–1.6 for comfortable reading on body text.

Interview Questions

Quick Quiz

1. Which property sets the boldness of text?

2. Why should font-family end with a generic fallback like sans-serif?

3. Which property controls spacing between lines of text?