The line-height
property in CSS controls the vertical spacing between lines of text. It’s an essential property for improving the readability and aesthetics of text content on a webpage.
Syntax
line-height: value;
The value
can be one of the following:
- Normal:
- Default value.
- The browser calculates a reasonable line height based on the font size.
line-height: normal;
- Number:
- A unitless multiplier of the font size.
- Example:
1.5
means 1.5 times the font size.line-height: 1.5;
- Length:
- Specifies the exact line height using units like
px
,em
,rem
, etc. - Example:
20px
,1.5em
.line-height: 20px;
- Specifies the exact line height using units like
- Percentage:
- Specifies the line height as a percentage of the font size.
- Example:
150%
means 150% of the font size.line-height: 150%;
How It Works
The line-height
property sets the total height of a line box. It includes the height of the text and the space above and below it. This space is distributed equally above and below the text.
Practical Examples
Default Line Height
<p style="line-height: normal;">This is a paragraph with normal line-height.</p>
Unitless Line Height
<p style="line-height: 1.5;">This is a paragraph with 1.5 times the font size as line-height.</p>
Fixed Line Height
<p style="line-height: 24px;">This is a paragraph with a fixed line-height of 24px.</p>
Percentage Line Height
<p style="line-height: 150%;">This is a paragraph with 150% line-height.</p>
Best Practices
- Use Unitless Values:
- Unitless values like
1.5
are preferred because they scale better with the font size. - Example:
p { font-size: 16px; line-height: 1.5; /* 16px × 1.5 = 24px */ }
- Unitless values like
- Readable Spacing:For body text, a line height between
1.4
and1.8
is often ideal. - Consistent Design:Use relative units (
em
,%
, or unitless) for responsive designs.
Common Use Cases
- Improving Readability:
body { font-family: Arial, sans-serif; font-size: 16px; line-height: 1.6; }
- Tightening or Loosening Text Spacing:
h1 { font-size: 32px; line-height: 1.2; /* Tight spacing */ } p { font-size: 16px; line-height: 1.8; /* Loose spacing */ }
Visual Example
Imagine text with different line-height
values:
- Line Height: Normal
This is a paragraph with normal spacing.
- Line Height: 1.5
This is a paragraph with slightly more spacing between lines.
- Line Height: 2
This is a paragraph with much more spacing between lines.
Experiment
Try adjusting line-height
values in this example:
<!DOCTYPE html> <html lang="en"> <head> <style> p { font-size: 16px; line-height: 1.5; /* Adjust this value to see changes */ } </style> </head> <body> <p>This is an example paragraph to test line-height.</p> </body> </html>