Panduan lengkap CSS Typography untuk styling teks web modern
CSS Typography mengontrol tampilan teks pada halaman web. Memahami property typography penting untuk desain yang readable dan estetik.
/* Font Properties */
body {
font-family: 'Segoe UI', sans-serif;
font-size: 1rem;
font-weight: 400;
font-style: normal;
}
/* Text Properties */
h1 {
color: #9333ea;
text-align: center;
text-transform: capitalize;
text-decoration: underline;
}
/* Spacing Properties */
p {
line-height: 1.6;
letter-spacing: 0.5px;
word-spacing: 2px;
}
đ Penjelasan Detail:
âĸ font-family
- menentukan jenis font
âĸ font-size
- ukuran teks (px, em, rem, %)
âĸ font-weight
- ketebalan font (100-900 atau normal/bold)
âĸ text-align
- perataan teks (left, center, right, justify)
âĸ line-height
- spasi antar baris (penting untuk readability)
/* Web Fonts */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
body {
font-family: 'Roboto', sans-serif;
}
/* Text Effects */
.fancy-text {
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
/* Custom Selection */
::selection {
background: #9333ea;
color: white;
}
Semua basic typography properties didukung penuh oleh semua browser modern. Web fonts (@font-face) didukung Chrome 4+, Firefox 3.5+, Safari 3.1+, IE 9+.
/* Font Weight Values */
100 /* Thin */
400 /* Normal */
700 /* Bold */
900 /* Black */
/* Text Transform Values */
none /* Default */
capitalize /* Huruf pertama kapital */
uppercase /* SEMUA HURUF BESAR */
lowercase /* semua huruf kecil */
/* Text Decoration Values */
none /* Default */
underline /* Garis bawah */
overline /* Garis atas */
line-through /* Coret tengah */
đ Penjelasan Detail:
âĸ Font weight menentukan ketebalan font (100-900)
âĸ Text transform mengontrol kapitalisasi teks
âĸ Text decoration menambahkan garis dekoratif
âĸ rem units lebih baik untuk aksesibilitas (relatif ke root font-size)
âĸ Line-height tanpa unit lebih fleksibel (1.5 lebih baik dari 24px)
/* Variable Fonts */
@font-face {
font-family: 'Inter var';
src: url('Inter.var.woff2') format('woff2-variations');
font-weight: 100 900;
font-style: oblique 0deg 10deg;
}
/* Text Rendering */
body {
text-rendering: optimizeLegibility;
font-kerning: normal;
font-variant-ligatures: common-ligatures;
}
/* Vertical Rhythm */
h1, h2, h3, p {
margin-bottom: 1.5rem;
line-height: 1.3;
}
Variable fonts: Chrome 62+, Firefox 62+, Safari 11+, Edge 17+. Text-rendering: Chrome 4+, Firefox 1+, Safari 5+, IE 9+. Font-kerning: Chrome 33+, Firefox 32+, Safari 9.1+, Edge 12+.
Gunakan system font stack untuk performance, set base line-height 1.5 untuk readability, dan gunakan clamp() untuk responsive typography.
/* â
Good - System font stack */
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
/* â
Good - Responsive typography */
h1 {
font-size: clamp(1.75rem, 5vw, 2.5rem);
}
/* â Avoid - Fixed pixel values */
p {
font-size: 16px;
line-height: 24px;
}
/* â Avoid - Too many web fonts */
body {
font-family: 'Poppins', 'Montserrat', 'Raleway', sans-serif;
}
đ Penjelasan Detail:
âĸ System fonts - load lebih cepat dari web fonts
âĸ clamp() - buat font-size responsive dengan minimum dan maximum
âĸ Relative units - gunakan rem/em untuk aksesibilitas
âĸ Limit font variations - 2-3 font family maksimal
âĸ Vertical rhythm - pertahankan konsistensi spasi vertikal
âĸ Variable fonts - single file dengan banyak variasi