CSS Fonts

In web design, typography is more than just making text look pretty; it’s about readability, brand identity, and user experience. CSS provides a robust set of properties to control every aspect of how your text appears on the screen. Below, we’ll break down the essential font properties every developer should master.

  • Font Family (font-family): This property defines the typeface used for your text. Because you can't always guarantee a user has a specific font installed on their device, we provide a "font stack"—a comma-separated list of fallback fonts.
/* The browser tries Arial first, then Helvetica, then any default sans-serif font */
body {
    font-family: "Open Sans", Arial, Helvetica, sans-serif;
}
Developer Tip: If a font name contains spaces (like "Times New Roman"), you must wrap it in quotation marks. Single-word font names like Arial do not require them.
  • Font Size (font-size): This determines how large your text appears. While px (pixels) is common for fixed layouts, modern web development favors relative units like em or rem for better accessibility.
h1 {
    font-size: 2.5rem; /* 2.5 times the root font size */
}

p {
    font-size: 16px; /* A standard baseline for body text */
}
Best Practice: Use rem units for font sizes. This ensures that if a user changes their browser's default font size for accessibility reasons, your website's text scales proportionally.
  • Font Style (font-style): Most commonly used to turn regular text into italics.
.italic-text {
    font-style: italic;
}
  • Font Weight (font-weight): This controls the thickness of the characters. While you can use keywords like bold, using numeric values (100 to 900) gives you finer control if the font file supports multiple weights.
h2 {
    font-weight: 700; /* Bold */
}

.light-text {
    font-weight: 300; /* Light/Thin */
}
Watch Out: Not every font supports every numeric weight. If you set a weight of 600 but the font only provides 400 and 700, the browser will "fudge" it by picking the closest match, which might not look exactly as you intended.
  • Font Variant (font-variant): This is a specialized property often used for headings or UI labels to create a unique look using small capital letters.
.label {
    font-variant: small-caps;
}
  • Text Transform (text-transform): This handles capitalization automatically. It is particularly useful for buttons or navigation links where you want a consistent look regardless of how the text was typed in the HTML.
.button {
    text-transform: uppercase; /* Converts "Submit" to "SUBMIT" */
}

.heading {
    text-transform: capitalize; /* Capitalizes the first letter of each word */
}
Common Mistake: Typing your text in ALL CAPS directly in the HTML. It’s better to type naturally in the HTML and use text-transform: uppercase; in CSS. This way, screen readers read the words correctly, and you can easily change the styling later without editing the content.

Line Height (line-height): Often called "leading" in traditional print, this property controls the vertical space between lines of text. Proper line height is critical for preventing eye strain in long-form articles.

p {
    line-height: 1.6; /* A unitless value of 1.5 to 1.7 is ideal for readability */
}
Developer Tip: Use unitless values for line-height (like 1.5 instead of 24px). This makes the line height relative to the element's current font size, preventing overlapping text if the font size changes.
  • Text Decoration (text-decoration): Used to add or remove visual lines on text. It is most commonly used to style hyperlinks.
a {
    text-decoration: none; /* Removes the default underline from links */
}

.price-old {
    text-decoration: line-through; /* Useful for showing a discount */
}
  • Letter Spacing (letter-spacing): This adjusts the horizontal space between characters (tracking). It’s often used to make large headings look more sophisticated or to improve the readability of all-caps text.
h1 {
    letter-spacing: -0.5px; /* Tightens the heading for a modern look */
}

.wide-text {
    letter-spacing: 2px;
}

By combining these properties, you can transform a basic document into a professional, polished interface. Remember that typography is a balance of aesthetics and functionality—always prioritize readability for your users.