Introduction to line-height in CSS π§΅
The line-height property controls the vertical spacing between lines of text. It's one of the most important typography-related properties in CSS because it directly affects readability, aesthetics, and layout structure. Whether youβre working with paragraphs, headings, or UI text β mastering line-height is essential! π₯
Why Use line-height? π€
- Improves readability in paragraphs π
- Prevents text from looking crowded or tight π§
- Makes designs more visually balanced π¨
- Controls spacing in UI elements like buttons and inputs π
Syntax π
Basic Syntax
selector {
line-height: normal | <number> | <length> | <percentage>;
}Note
Available Values π§©
1. normal
The browserβs default line height. Usually between 1.1 and 1.4, depending on the font.
2. <number> (Recommended) π
A multiplier of the font size. Example: line-height: 1.5; means 150% of the font size.
3. <length>
A fixed value such as 20px, 1.5rem, etc. Best for precise control, but not responsive.
4. <percentage>
Calculated based on the elementβs font size. Example: 150%.
Examples π₯
1. Using Number (Most Common) β¨
Example: number value
p {
line-height: 1.6;
}2. Using Pixels (Fixed spacing) π
Example: pixel value
p {
line-height: 24px;
}3. Using Percentage (%)
Example: percentage value
p {
line-height: 150%;
}4. Tight vs Loose Line Height π
Tight and Loose Styles
.tight {
line-height: 1.1;
}
.loose {
line-height: 2;
}Comparison Table π
| Value Type | Example | Usage |
|---|---|---|
| normal | normal | Default browser handling |
| number | 1.5 | Recommended for responsive layouts |
| length | 20px | Precise control, but not flexible |
| percentage | 150% | Similar to number, but expressed as % |
Visual Example πΌοΈ
How different line-heights look:
Further details can be found at: https://dummyimage.com/800x220/eeeeee/000000&text=Line-height+visual+sample
Conclusion π
The line-height property is crucial for improving text readability and visual structure. By adjusting spacing between lines, you can create clean, accessible, and beautifully designed content. Start experimenting with 1.4β1.8 for paragraphs β your readers will thank you! π