CSS

CSS (Cascading Style Sheets) is the language that brings web pages to life with colors, layouts, and animations. It works alongside HTML to create visually stunning and responsive designs. Let's explore the power of CSS.

CSS Fonts

31 March 2025 | Category:

CSS allows you to control the appearance of text using font properties. This guide will cover everything about fonts in CSS, including font types, sizes, weights, styles, and advanced techniques.


1️⃣ Font Types in CSS

There are five major font families:

Font TypeDescriptionExample Fonts
SerifHas small strokes at the ends of lettersTimes New Roman, Georgia
Sans-serifNo strokes; clean and modernArial, Helvetica, Verdana
MonospaceEvery letter takes up equal spaceCourier New, Consolas
CursiveResembles handwritingBrush Script MT, Comic Sans MS
FantasyDecorative and stylizedPapyrus, Impact

Example

p {
    font-family: "Georgia", serif;
}

📌 Tip: Always provide a fallback font in case the primary font isn’t available.


2️⃣ font-family – Defining the Font

The font-family property specifies which font should be used.

Example

p {
    font-family: "Arial", sans-serif;
}

📌 If “Arial” is unavailable, the browser will use any available sans-serif font.


3️⃣ font-size – Adjusting Text Size

Controls the size of the text. You can use:

  • Pixels (px) – Fixed size
  • Em (em) – Relative to the parent element
  • Rem (rem) – Relative to the root (html) element
  • Percentage (%) – Relative to the parent element
  • View Width (vw) – Relative to the screen size

Example

p {
    font-size: 20px;
}

h1 {
    font-size: 2em; /* Twice the size of the parent element */
}

📌 1em = 100% of the parent font size
📌 1rem = 100% of the root font size


4️⃣ font-weight – Controlling Boldness

Defines how thick or thin the text appears.

ValueEffect
normalDefault text weight
boldMakes text bold
lighterLighter than normal
bolderBolder than normal
100-900Numeric values (100 = thin, 900 = extra bold)

Example

p {
    font-weight: bold;
}

h1 {
    font-weight: 700;
}

📌 Numeric values (100-900) offer more precise control.


5️⃣ font-style – Italic, Oblique, or Normal

Controls the appearance of the text.

ValueEffect
normalDefault text style
italicSlanted text
obliqueSimilar to italic (less common)

Example

p {
    font-style: italic;
}

6️⃣ font-variant – Small Caps Text

Defines small-caps formatting, where lowercase letters appear as smaller uppercase letters.

ValueEffect
normalDefault text
small-capsConverts lowercase letters to small uppercase letters

Example

p {
    font-variant: small-caps;
}

📌 This is useful for titles and headings.


7️⃣ font – Shorthand Property

Instead of writing multiple properties, you can use font shorthand.

Syntax

font: font-style font-variant font-weight font-size/line-height font-family;

Example

p {
    font: italic small-caps bold 16px/1.5 "Arial", sans-serif;
}

📌 Order matters! Always follow the correct sequence.


8️⃣ @font-face – Custom Fonts

You can use custom fonts in CSS using @font-face.

Example

@font-face {
    font-family: "CustomFont";
    src: url("custom-font.woff2") format("woff2");
}

p {
    font-family: "CustomFont", sans-serif;
}

📌 Font Formats

FormatDescription
woff2Recommended for modern browsers
woffWorks on most browsers
ttfTrueType Font (large file size)
otfOpenType Font

9️⃣ Google Fonts – Importing Web Fonts

Google Fonts provides free fonts that can be used directly in CSS.

Steps

  1. Go to Google Fonts
  2. Select a font and copy the @import or <link> code.
  3. Use it in your CSS.

Example

@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap");

p {
    font-family: "Roboto", sans-serif;
}

📌 display=swap ensures text is shown even before the font fully loads.


🔟 Font Accessibility & Best Practices

✅ Use web-safe fonts for better compatibility.
✅ Provide fallback fonts to ensure proper rendering.
✅ Avoid using too many different fonts on a page.
✅ Use rem or em instead of px for scalability.
✅ Always import custom fonts asynchronously to prevent layout shifts.


🎯 Summary Cheat Sheet

PropertyEffect
font-familyDefines font type
font-sizeSets text size
font-weightControls boldness
font-styleItalic, oblique, or normal text
font-variantSmall caps text
@font-faceLoads custom fonts
fontShorthand property for all font styles

🔥 Final Example

@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap");

body {
    font-family: "Roboto", sans-serif;
    font-size: 16px;
    font-weight: 400;
    font-style: normal;
    line-height: 1.6;
}

🚀 Conclusion

✅ Fonts play a crucial role in web design.
✅ Use Google Fonts or @font-face for custom typography.
Avoid excessive font weights to optimize performance.
Test fonts on different devices for better readability.