📐 CSS Units = Perfect Sizing
Wrong units break designs. CSS units matter. px, em, rem, vh, vw — choose wisely.
📝 Unit Types
/* Absolute Units */ px → Pixels (fixed) pt → Points (print) pc → Picas (print) /* Relative Units (Font) */ em → Relative to parent font-size rem → Relative to root font-size /* Relative Units (Viewport) */ vh → Viewport height (1% of viewport) vw → Viewport width (1% of viewport) vmin → Smaller of vh/vw vmax → Larger of vh/vw /* Relative Units (Container) */ % → Percentage of parent /* Relative Units (Other) */ ch → Width of '0' character ex → Height of 'x' character
🎯 When to Use Which
/* px - Fixed sizes */
- Use for borders, shadows
- Use for small elements
- Not responsive
/* rem - Root relative */
- Use for font sizes
- Use for spacing
- Responsive
- Consistent
/* em - Parent relative */
- Use for component scaling
- Use for nested elements
- Can be tricky
/* % - Parent relative */
- Use for widths
- Use for layouts
- Use for padding
/* vh/vw - Viewport relative */
- Use for fullscreen sections
- Use for hero images
- Use for responsive typography
/* Ch - Character width */
- Use for max-width of text
- Use for readability
/* Examples */
html {
font-size: 16px; /* Base */
}
body {
font-size: 1rem; /* 16px */
}
h1 {
font-size: 2rem; /* 32px */
}
.container {
max-width: 80ch; /* Readable width */
padding: 1rem; /* 16px */
}
.hero {
height: 100vh; /* Full viewport */
}
💡 Unit Tips
- Use rem for font sizes
- Use px for borders and shadows
- Use % for widths and layouts
- Use vh/vw for fullscreen
- Use ch for text width
“Choosing the right CSS units is essential. px, rem, em, vh — know when to use each.”
