Metadata
- Date :: 13-04-2025
- Tags :: web-dev
Notes
✍️ CSS Font Properties – Ultimate Guide & Notes
📌 Why Font Properties Matter in Web Design
Fonts are more than just letters—they define your website’s tone, readability, brand identity, and visual hierarchy. With CSS, we can finely control how our text looks and behaves using several font-related properties.
Let’s dive deep into the most common and powerful ones:
🧱 1. font-size – Controlling Text Size
🔹 What it does:
Sets the size of the font. The size can be defined in several units.
📏 Common Units for font-size
| Unit | Meaning | Relative/Absolute | Notes |
|---|---|---|---|
px | Pixels | Absolute | Most commonly used; 1px = 1/96 of an inch (~0.26mm) |
pt | Points | Absolute | 1pt = 1/72 of an inch (~0.35mm); familiar from Word Docs |
em | Relative to parent | Relative | 1em = 100% of parent font-size |
rem | Relative to root (html) | Relative | 1rem = 100% of root font-size |
% | Percentage of parent | Relative | 100% = parent size |
x-large, small, etc. | Keyword sizes | Relative | Predefined by browser styles |
🧪 Example: font-size with different units
/* Absolute */
h1 {
font-size: 24px;
}
p {
font-size: 12pt;
}
/* Relative */
h2 {
font-size: 2em; /* 2x parent font size */
}
h3 {
font-size: 1.5rem; /* 1.5x root font size */
}💡 EM vs REM: Key Difference
| Feature | em | rem |
|---|---|---|
| Based on | Parent element | Root (usually <html>) |
| Flexible | Yes, but can get confusing with deep nesting | Yes, consistent and predictable |
| Best For | Nested components needing proportional scaling | Consistent, site-wide typography |
🔧 Why developers prefer rem:
Using rem ensures consistent font sizing across a website regardless of nesting, avoiding unexpected results caused by changing parent sizes.
💪 2. font-weight – Boldness of Text
🔹 What it does:
Controls how thick or light the text appears.
🔢 Values for font-weight:
| Keyword | Numeric Value |
|---|---|
normal | 400 |
bold | 700 |
lighter, bolder | Relative to parent |
100 to 900 | Fine-tuned control (100 = thin, 900 = very bold) |
🧪 Example:
p {
font-weight: normal; /* or 400 */
}
h1 {
font-weight: 700; /* bold */
}
.light-text {
font-weight: 200;
}💡 Not all fonts support every weight from 100–900. Check the available weights on Google Fonts.
✍️ 3. font-family – Choosing the Typeface
🔹 What it does:
Defines the font style (e.g., Arial, Times New Roman, Roboto).
📚 Syntax:
font-family: "Font Name", fallback-font;⚠️ Multi-word font names need quotes:
font-family: "Times New Roman", serif;🧬 Font Stack Example:
font-family: "Helvetica", Arial, sans-serif;-
If the user’s system doesn’t have Helvetica → fallback to Arial
-
If Arial isn’t available → fallback to any generic sans-serif
📖 Serif vs Sans-serif
| Type | Description | Example Fonts |
|---|---|---|
| Serif | Has “feet” or decorative strokes | Times New Roman, Georgia |
| Sans-serif | Clean, no strokes | Arial, Helvetica, Roboto |
| Monospace | Equal-width letters | Courier, Consolas |
| Cursive | Handwritten style | Brush Script, Pacifico |
| Fantasy | Decorative/stylized | Impact, Jokerman |
🌍 Using Custom Fonts (Google Fonts)
-
Visit: https://fonts.google.com
-
Select a font and weights you want (e.g., Regular 400, Bold 700)
-
Click “</> Get Embed Code”
-
Copy the
<link>tag into your HTML<head>(outside the<style>tag!) -
Copy the corresponding
font-familydeclaration into your CSS
🧪 Example:
<!-- In the HTML head -->
<link href="https://fonts.googleapis.com/css2?family=Caveat&display=swap" rel="stylesheet">
<style>
h1 {
font-family: 'Caveat', cursive;
}
</style>🧠 Always provide a generic fallback like
cursive,serif, orsans-serif.
📐 4. text-align – Text Alignment
🔹 What it does:
Controls the horizontal alignment of text within its container.
| Value | Effect |
|---|---|
left | Aligns text to the left (default for LTR) |
right | Aligns text to the right |
center | Centers the text |
justify | Stretches text to align with both edges |
start / end | Relative to the reading direction |
🧪 Example:
p {
text-align: right;
}
h1 {
text-align: center;
}🧪 Final Exercise Review (From the Lesson)
✅ Skills Practiced:
-
Changing text color using named color (
coral) -
Setting font-size with rem (
2rem) -
Applying font-weight numerically (
900) -
Using Google Fonts via
<link>andfont-family -
Applying
text-align: right -
Adjusting the root font-size in the
<html>tag (e.g.,30px)
✅ Why it’s useful:
-
Reinforces the difference between
emandrem -
Shows how to use external fonts correctly
-
Demonstrates relative text sizing using the root context
-
Encourages accessibility through fallback fonts
🧠 Summary: Font Styling Properties Cheat Sheet
| Property | Purpose | Example |
|---|---|---|
font-size | Size of text | font-size: 18px; |
font-weight | Thickness of text | font-weight: 700; |
font-family | Font style | font-family: Arial, sans-serif; |
text-align | Text alignment | text-align: center; |
🧰 Pro Developer Tips
-
Use
remfor scalable and consistent sizing across your site. -
Always provide fallback fonts in your
font-family. -
Use Google Fonts to bring in beautiful typography.
-
Combine
font-weight,font-size, andfont-familyfor a polished UI. -
Test your fonts across browsers and devices.