header-logo

CODE WITH FAHIM

CSS Typography

What's CSS Typography?

Typography in CSS controls how text appears on a webpageβ€”its font, size, spacing, alignment, and overall readability.

1.font-family:

font-family lets you define which font the browser should use for an element.
It accepts one or more fonts in a priority-ordered list (the browser uses the first available).

#Syntax:
body { font-family: Arial, sans-serif; }

If Arial isn't available, the browser uses the fallback sans-serif.

#Custom Font Names Must Be Quoted
h1 { font-family: "Open Sans", sans-serif; }

Use quotes when a font name contains spaces or special characters.

#Generic Font Families:
These are "catch-all" backups the browser always supports:

Generic family Examples
serif Times,Georgia
sans-serif Arial,Helvetica
monospace consolas,courier
cursive Brush Script, Comic Sans
fantasy Impact (rarely used)
system-ui Local OS UI font


Always end your list with a generic font.

#🎨 How to Use Google Fonts:

Google Fonts gives you free web fonts you can load directly into your website.

There are two main ways to use Google Fonts:
i.Using the <link> Tag (Recommended):

Step 1 β€” Copy Google Fonts link:
Example for Inter with multiple weights:
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap" rel="stylesheet">

Place the link tag inside the <head> tag of your HTML File.

Step 2 β€” Use in CSS:
body { font-family: "Inter", sans-serif; }



ii.Using @import in CSS (Not recommended, but possible)
@import url('https://fonts.googleapis.com/css2? family=Poppins:wght@300;400;600&
display=swap'); body { font-family: "Poppins", sans-serif; }

❗ This method slows down page rendering β€” use <link> tag instead.





2.font-size: font-size controls how large or small text appears.
CSS supports many units (px, rem, em, %, vw…), each suited for different situations.

#Basic Syntax:
p { font-size: 16px; }

Recommended Units for Modern Web Design

βœ” rem (most recommended):
Scales relative to the root font size (usually 16px).
body { font-size: 1rem; /* 16px (browser-default)*/ } h1 { font-size: 2.5rem; /* 40px */ }

Advantages:

βœ” em
Relative to parent element's font size.

Use for spacing or component-based design:
button { font-size: 1em; /* same as parent */ padding: 0.5em 1.2em; }


βœ” px

Fixed size.
Useful for small UI elements, but less responsive.
.small-text { font-size: 12px; }


βœ” Fluid units (vw, vh)

Size based on viewport width/height.
.hero-title { font-size: 8vw; }


Relative Percentages
Useful for scaling entire containers.
section { font-size: 120%; /* 1.2rem if parent is 1rem */ }



#Responsive Font Sizes

βœ” Using clamp() β€” BEST modern technique
Automatically scales between min and max values.
h1 { font-size: clamp(2rem, 5vw, 4rem); }

Meaning:




3.font-weight:
Basic syntax:
p { font-weight: 400; }
Using keywords:
p{ font-weight: normal; /*400*/ font-weight: bold; /*700*/ }





4.text-align:
Syntax:
p{ text-align: left; text-align: right; text-align: center; text-align: justify; /* stretched edges */ text-align: start; /* depends on writing direction */ text-align: end; /* depends on writing direction */ }




5.text-transform:
h2 { text-transform: uppercase; }


Values Examples
uppercase "hello" β†’ "HELLO"
lowercase "HELLO" β†’ "hello"
capitalize "hello world" β†’ "Hello World"
none Removes transformations




6.text-decoration:

Syntax:
text-decoration-line: underline; text-decoration-color: red; text-decoration-style: wavy; text-decoration-thickness: 3px;


You can use shorthand:
text-decoration: underline red wavy 3px;


Order doesn't matter.

Longhand Properties:

1.text-decoration-line:
text-decoration-line: underline; /* underline */ text-decoration-line: overline; /* top line */ text-decoration-line: line-through; /* strike-through */ text-decoration-line: underline overline;


text-decoration-color:
text-decoration-color: #ff0055;


text-decoration-style:
text-decoration-style: solid; /* default */ text-decoration-style: double; text-decoration-style: dotted; text-decoration-style: dashed; text-decoration-style: wavy;


text-decoration-thickness:
text-decoration-thickness: 2px;


Advanced typographies: