✍️ Text & Fonts

Typography Fundamentals

CSS Typography

Typography is one of the most important aspects of web design. Learn how to style text effectively with CSS.

🔤 Font Properties

Font Family

/* Single font */
body {
    font-family: Arial;
}

/* Font stack (fallbacks) */
body {
    font-family: Arial, Helvetica, sans-serif;
}

/* Common font stacks */
.serif {
    font-family: Georgia, 'Times New Roman', Times, serif;
}

.sans-serif {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.monospace {
    font-family: 'Courier New', Courier, monospace;
}

Font Size

/* Absolute units */
h1 {
    font-size: 32px;
}

/* Relative units (recommended) */
h1 {
    font-size: 2rem;    /* Relative to root */
}

p {
    font-size: 1em;     /* Relative to parent */
}

/* Percentage */
small {
    font-size: 80%;
}

/* Keywords */
.large {
    font-size: larger;
}

.small {
    font-size: smaller;
}

Font Weight

/* Keywords */
p {
    font-weight: normal;    /* 400 */
}

strong {
    font-weight: bold;      /* 700 */
}

/* Numeric values (100-900) */
.light {
    font-weight: 300;
}

.regular {
    font-weight: 400;
}

.medium {
    font-weight: 500;
}

.semibold {
    font-weight: 600;
}

.bold {
    font-weight: 700;
}

.black {
    font-weight: 900;
}

Font Style

em {
    font-style: italic;
}

.normal {
    font-style: normal;
}

.oblique {
    font-style: oblique;
}

Font Variant

.small-caps {
    font-variant: small-caps;
}

/* Normal text */
.normal {
    font-variant: normal;
}

Font Shorthand

/* font: [style] [variant] [weight] size/line-height family */
p {
    font: italic small-caps bold 16px/1.6 Arial, sans-serif;
}

/* Minimum required: size and family */
h1 {
    font: 2rem Georgia, serif;
}

📝 Text Properties

Text Color

p {
    color: #333;
}

a {
    color: #007bff;
}

.error {
    color: rgb(220, 53, 69);
}

Text Alignment

h1 {
    text-align: center;
}

p {
    text-align: left;      /* Default */
}

.right {
    text-align: right;
}

.justify {
    text-align: justify;   /* Stretch lines */
}

Text Decoration

/* Remove underline from links */
a {
    text-decoration: none;
}

/* Underline */
.underline {
    text-decoration: underline;
}

/* Line through */
.strikethrough {
    text-decoration: line-through;
}

/* Overline */
.overline {
    text-decoration: overline;
}

/* Multiple decorations */
.fancy {
    text-decoration: underline overline;
}

/* Styled decoration */
.styled {
    text-decoration: underline wavy red;
}

Text Transform

/* Uppercase */
.uppercase {
    text-transform: uppercase;
}

/* Lowercase */
.lowercase {
    text-transform: lowercase;
}

/* Capitalize first letter of each word */
.capitalize {
    text-transform: capitalize;
}

/* No transformation */
.normal {
    text-transform: none;
}

Text Indent

/* Indent first line */
p {
    text-indent: 2em;
}

/* Hanging indent (negative) */
.list-item {
    text-indent: -1em;
    padding-left: 1em;
}

Line Height

/* Unitless (recommended) - multiplier of font size */
p {
    line-height: 1.6;
}

/* Fixed height */
h1 {
    line-height: 48px;
}

/* Percentage */
.tight {
    line-height: 120%;
}

/* Better readability */
body {
    line-height: 1.6;
}

h1, h2, h3 {
    line-height: 1.2;
}

Letter Spacing

/* Increase spacing */
h1 {
    letter-spacing: 2px;
}

/* Decrease spacing (tighter) */
.tight {
    letter-spacing: -0.5px;
}

/* Normal */
p {
    letter-spacing: normal;
}

/* Great for headings */
.heading {
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

Word Spacing

/* Increase word spacing */
.spaced {
    word-spacing: 5px;
}

/* Normal */
p {
    word-spacing: normal;
}

Text Shadow

/* text-shadow: x y blur color */
h1 {
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}

/* Multiple shadows */
.neon {
    color: white;
    text-shadow: 
        0 0 10px #fff,
        0 0 20px #fff,
        0 0 30px #fff,
        0 0 40px #0ff,
        0 0 70px #0ff;
}

/* Embossed effect */
.embossed {
    color: #333;
    text-shadow: 1px 1px 0px #fff;
}

White Space

/* Normal - collapse whitespace */
p {
    white-space: normal;
}

/* Preserve spaces and line breaks */
pre {
    white-space: pre;
}

/* Prevent wrapping */
.nowrap {
    white-space: nowrap;
}

/* Allow wrapping but preserve spaces */
.pre-wrap {
    white-space: pre-wrap;
}

Text Overflow

/* Ellipsis for overflow text */
.truncate {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Multi-line ellipsis */
.clamp {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

🌐 Web Fonts

Google Fonts

<!-- In HTML head -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap" rel="stylesheet">

/* In CSS */
body {
    font-family: 'Roboto', sans-serif;
}

@font-face

/* Custom font */
@font-face {
    font-family: 'MyCustomFont';
    src: url('fonts/custom-font.woff2') format('woff2'),
         url('fonts/custom-font.woff') format('woff');
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

/* Use the font */
h1 {
    font-family: 'MyCustomFont', sans-serif;
}

🎯 Practical Examples

/* Article typography */
article {
    font-family: Georgia, serif;
    font-size: 18px;
    line-height: 1.8;
    color: #333;
    max-width: 650px;
    margin: 0 auto;
}

article h1 {
    font-size: 2.5rem;
    line-height: 1.2;
    margin-bottom: 1rem;
    color: #111;
}

article h2 {
    font-size: 1.8rem;
    line-height: 1.3;
    margin-top: 2rem;
    margin-bottom: 0.5rem;
}

article p {
    margin-bottom: 1.5rem;
}

/* Button text */
.button {
    font-family: -apple-system, sans-serif;
    font-size: 16px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    text-decoration: none;
}

/* Quote styling */
blockquote {
    font-style: italic;
    font-size: 1.2em;
    border-left: 4px solid #ccc;
    padding-left: 1.5rem;
    margin: 2rem 0;
    color: #666;
}

/* Code text */
code {
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
    background: #f4f4f4;
    padding: 2px 6px;
    border-radius: 3px;
}

/* Heading with subtitle */
.heading-group h1 {
    font-size: 3rem;
    font-weight: 700;
    line-height: 1;
    margin-bottom: 0.5rem;
}

.heading-group .subtitle {
    font-size: 1.2rem;
    font-weight: 400;
    color: #666;
    letter-spacing: 1px;
}

🎯 Key Takeaways