Master typography and text styling to create beautiful, readable content
Typography is the art and technique of arranging type to make written language legible, readable, and appealing. In CSS, font and text properties give you complete control over how text appears on your webpage.
The font-family property specifies the typeface for text.
Always provide fallback fonts in case the primary font isn't
available.
selector {
font-family: "Primary Font", "Fallback Font", generic-family;
}
/* Serif Fonts */
p {
font-family: Georgia, "Times New Roman", serif;
}
/* Sans-serif Fonts */
h1 {
font-family: Arial, Helvetica, sans-serif;
}
/* Monospace Fonts */
code {
font-family: "Courier New", Courier, monospace;
}
/* Cursive Fonts */
.signature {
font-family: "Brush Script MT", cursive;
}
Generic Font Families:
The font-size property sets the size of the font. You can
use various units like pixels, ems, rems, or percentages.
/* Absolute Units */
h1 { font-size: 32px; }
p { font-size: 16px; }
/* Relative Units */
h2 { font-size: 2em; } /* 2x parent size */
p { font-size: 1rem; } /* relative to root */
/* Percentage */
small { font-size: 80%; }
Best Practice: Use rem units for
better accessibility and responsiveness.
The font-weight property controls the thickness or
boldness of text.
/* Keyword Values */
p { font-weight: normal; } /* 400 */
strong { font-weight: bold; } /* 700 */
/* Numeric Values (100-900) */
h1 { font-weight: 100; } /* Thin */
h2 { font-weight: 300; } /* Light */
h3 { font-weight: 400; } /* Normal */
h4 { font-weight: 600; } /* Semi-bold */
h5 { font-weight: 700; } /* Bold */
h6 { font-weight: 900; } /* Black */
The font-style property specifies italic, oblique, or
normal text.
p {
font-style: normal; /* Default */
}
em {
font-style: italic; /* Slanted text */
}
.quote {
font-style: oblique; /* Similar to italic */
}
.left { text-align: left; }
.center { text-align: center; }
.right { text-align: right; }
.justify { text-align: justify; }
a { text-decoration: none; } /* No underline */
.underline { text-decoration: underline; }
.overline { text-decoration: overline; }
.line-through { text-decoration: line-through; }
.uppercase { text-transform: uppercase; } /* UPPERCASE */
.lowercase { text-transform: lowercase; } /* lowercase */
.capitalize { text-transform: capitalize; } /* First Letter */
p {
line-height: 1.6; /* 1.6x font size */
line-height: 24px; /* Fixed height */
}
h1 {
letter-spacing: 2px; /* Space between letters */
}
.tight {
letter-spacing: -1px; /* Closer letters */
}
p {
word-spacing: 5px; /* Space between words */
}
Add shadow effects to text for depth and emphasis.
/* Syntax: x-offset y-offset blur-radius color */
h1 {
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
/* Multiple Shadows */
.fancy {
text-shadow:
2px 2px 4px red,
-2px -2px 4px blue;
}
/* Glow Effect */
.glow {
text-shadow: 0 0 10px #fff, 0 0 20px #667eea;
}
Use custom fonts from Google Fonts to enhance your typography.
<head>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
</head>
body {
font-family: 'Roboto', sans-serif;
}
This is a sans-serif font with normal weight and justified alignment. Line height is set to 1.6 for better readability.
Monospace font with letter spacing
Italic text centered
Uppercase with letter spacing
Q1: Which property is used to change the font of an element?
Q2: What does font-weight: 700 represent?
Q3: Which property converts text to uppercase?
Q4: What is the recommended line-height for better readability?
Q5: Which property adds space between letters?