Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Line Height in Bootstrap 5

Line Height: Controls the vertical spacing between lines of text for better readability.

What is Line Height?

Line height (leading) is the vertical space between lines of text. Proper line height improves readability, especially for large blocks of text.

Default Line Heights

Bootstrap's Default Line Heights
Element TypeClassValueCSS EquivalentPurpose
Display HeadingsDefault1.2line-height: 1.2Tight spacing for large text
Regular HeadingsDefault1.2line-height: 1.2Standard for headings
Body TextDefault1.5line-height: 1.5Optimal for reading
Small TextDefault1.5line-height: 1.5Maintains readability

Line Height Utility Classes

Available Line Height Classes
lh-1 (Tight)

This text has tight line height (1). Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

lh-sm (Small)

This text has small line height (1.25). Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

lh-base (Default)

This text has base line height (1.5). Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

lh-lg (Large)

This text has large line height (2). Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

<!-- Line Height Utility Classes -->
<p class="lh-1">Tight line height (1)</p>
<p class="lh-sm">Small line height (1.25)</p>
<p class="lh-base">Base line height (1.5) - Default</p>
<p class="lh-lg">Large line height (2)</p>

<!-- Using with other text utilities -->
<h2 class="lh-1 text-primary">Tight Heading</h2>
<p class="lh-lg text-muted">Large line height paragraph</p>

Line Height Comparison

Tight vs Loose
Too Tight (lh-1)

This line height is too tight for long text. It makes reading difficult and can cause eye strain. Avoid this for paragraphs of text.

Use for: Headings, short text
Optimal (lh-base)

This is the optimal line height for body text. It provides good readability and comfortable reading experience for users.

Use for: Body text, paragraphs
Too Loose (lh-lg)

This line height might be too loose for some contexts. It can make text appear disconnected and take up unnecessary space.

Use for: Poetry, special effects
Line Height Values
ClassValuePercentageBest For
.lh-11100%Headings, buttons
.lh-sm1.25125%Short paragraphs
.lh-base1.5150%Body text (default)
.lh-lg2200%Accessibility, poetry
Tip: For optimal readability, use 1.5-2.0 line height for body text. Adjust based on font size and line length.

Line Height with Different Font Sizes

Font Size and Line Height Relationship
Small Text (0.875rem)

Small text requires careful line height adjustment for readability.

.fs-6 .lh-sm
Body Text (1rem)

Standard body text works best with base line height for comfortable reading.

.fs-5 .lh-base
Large Text (1.25rem)

Large text benefits from increased line height for better visual separation.

.fs-3 .lh-lg
Golden Ratio for Line Height

Rule of thumb: Line height should be 1.5x font size for optimal readability. For example, 16px font size × 1.5 = 24px line height.

Practical Examples

Example 1: Article Content

Article Title

Published on January 15, 2024

This is the introduction paragraph with standard line height for optimal readability. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Subsequent paragraphs maintain consistent line height throughout the article for comfortable reading experience. Sed do eiusmod tempor incididunt.

<article>
  <h3 class="lh-1">Article Title</h3>
  <p class="lh-base text-muted">Published date</p>
  <p class="lh-base">First paragraph...</p>
  <p class="lh-base">Second paragraph...</p>
</article>
Example 2: Code Display
function calculateLineHeight(fontSize) { // Golden ratio for readability const goldenRatio = 1.618; return fontSize * goldenRatio; } // Usage const baseFontSize = 16; const optimalLineHeight = calculateLineHeight(baseFontSize); console.log(`Optimal line height: ${optimalLineHeight}px`);

Note: Code blocks often use tighter line height (.lh-sm) to display more lines in limited space while maintaining readability.

Responsive Line Height

Mobile Devices

On mobile: Large line height for touch-friendly spacing.
On tablet: Base line height.
On desktop: Base line height.

.lh-lg .lh-md-base .lh-sm-base
Responsive Approach
/* Custom responsive line heights */
@media (max-width: 768px) {
  .responsive-text {
    line-height: 2; /* Larger on mobile */
  }
}

@media (min-width: 769px) {
  .responsive-text {
    line-height: 1.5; /* Standard on larger screens */
  }
}

Accessibility Considerations

WCAG Guidelines for Line Height
  • Minimum: 1.5 times font size for body text
  • Spacing: At least 0.5 times spacing between paragraphs
  • Headings: Can use tighter spacing (1.2-1.3)
  • Long text: Consider 1.75-2.0 for better readability
  • Zoom: Line height should scale with text zoom
  • Contrast: Ensure sufficient contrast with background

Customizing Line Height in SCSS

SCSS Customization
// Customize line height variables
$line-height-base: 1.6; // Changed from 1.5
$line-height-sm: 1.35;  // Changed from 1.25
$line-height-lg: 2.1;   // Changed from 2

// Add custom line heights
$line-heights: (
  "xs": 1,
  "sm": 1.35,
  "base": 1.6,
  "lg": 2.1,
  "xl": 2.5
);

// Create custom utility
.lh-xl {
  line-height: 2.5 !important;
}

// Import Bootstrap
@import "bootstrap/scss/bootstrap";
Note: When customizing line heights, maintain consistency across your design system and ensure readability standards are met.