Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Text Colors in Bootstrap 5

Text Colors: Colorize text with color utilities using Bootstrap's theme colors.

Theme Text Colors

All Theme Colors
Primary
.text-primary
Secondary
.text-secondary
Success
.text-success
Danger
.text-danger
Warning
.text-warning
Info
.text-info
Light
.text-light
Dark
.text-dark
Body
.text-body
Muted
.text-muted
White
.text-white
Black 50%
.text-black-50
White 50%
.text-white-50
Note: Text color utilities are generated from the theme colors map, so they work with any color scheme you set up.

Color Usage Examples

Headings with Colors

Primary Heading

Success Heading

Danger Heading

Warning Heading

<h1 class="text-primary">Primary Heading</h1>
            <h2 class="text-success">Success Heading</h2>
            <h3 class="text-danger">Danger Heading</h3>
            <h4 class="text-warning">Warning Heading</h4>
Paragraphs with Colors

This is a primary paragraph. Use for important information.

This is a success paragraph. Use for positive messages.

This is a danger paragraph. Use for errors or warnings.

This is muted text. Use for less important information.

<p class="text-primary">Primary text</p>
            <p class="text-success">Success text</p>
            <p class="text-danger">Danger text</p>
            <p class="text-muted">Muted text</p>

Links with Contextual Colors

Colored Links
Note: Colored links have :hover and :focus states with opacity, unlike regular text.

Opacity Text Colors

Opacity Utilities
Black with Opacity

Black (100% opacity)

Black (75% opacity)

Black (50% opacity)

Black (25% opacity)

White with Opacity

White (100% opacity)

White (75% opacity)

White (50% opacity)

White (25% opacity)

ClassColorOpacityCSS Equivalent
text-blackBlack100%color: #000
text-black-75Black75%color: rgba(0,0,0,.75)
text-black-50Black50%color: rgba(0,0,0,.5)
text-black-25Black25%color: rgba(0,0,0,.25)
text-whiteWhite100%color: #fff
text-white-75White75%color: rgba(255,255,255,.75)
text-white-50White50%color: rgba(255,255,255,.5)
text-white-25White25%color: rgba(255,255,255,.25)

Responsive Text Colors

Breakpoint-based Text Colors
Example: Different Colors on Different Screens

This text is primary on mobile, success on tablet, and danger on desktop.

Screen SizeClass AppliedColorUse Case
Mobile (<768px)text-primaryPrimary (Blue)Default mobile color
Tablet (≥768px)text-md-successSuccess (Green)Highlight on tablet
Desktop (≥992px)text-lg-dangerDanger (Red)Attention on desktop
More Examples

Danger on mobile, warning on small+, success on medium+.

Muted on mobile, primary on large screens.

<!-- Responsive text colors -->
            <p class="text-primary text-md-success text-lg-danger">
            Changes color at different breakpoints
            </p>

            <p class="text-muted text-lg-primary">
            Muted on mobile, primary on desktop
            </p>

            <p class="text-danger text-sm-warning">
            Danger on mobile, warning on tablet+
            </p>

Color Meanings and Use Cases

When to Use Each Color
ColorClassMeaningUse CaseExample
Primarytext-primaryMain brand colorHeadings, important textMain title
Successtext-successPositive, successfulConfirmation messages"Order completed"
Dangertext-dangerError, dangerError messages, warnings"Invalid password"
Warningtext-warningWarning, cautionWarning messages"Action required"
Infotext-infoInformationalInformation messages"New feature available"
Mutedtext-mutedSecondary, less importantSupporting text, captionsImage captions
Secondarytext-secondarySecondary contentLess important headingsSubheadings

Text Colors with Other Utilities

Combined with Text Alignment

Centered Primary Heading

Right-aligned success text

<h3 class="text-primary text-center">
            Centered Primary Heading
            </h3>

            <p class="text-success text-end">
            Right-aligned success text
            </p>
Combined with Font Weight

Bold danger text for emphasis

Light warning text for subtlety

<p class="text-danger fw-bold">
            Bold danger text
            </p>

            <p class="text-warning fw-light">
            Light warning text
            </p>

Accessibility Considerations

Color Contrast Requirements
  • WCAG AA: Minimum contrast ratio of 4.5:1 for normal text
  • WCAG AAA: Minimum contrast ratio of 7:1 for normal text
  • Large text: 3:1 ratio for text 18pt+ or 14pt+ bold
  • Color blindness: Don't rely on color alone to convey meaning
  • Testing tools: Use browser extensions to check contrast
Common Issues
  • ❌ Using light colors on light backgrounds
  • ❌ Using low opacity text that reduces contrast
  • ❌ Relying only on color to indicate status
  • ❌ Not testing with color blindness simulators
  • ❌ Using similar colors that are hard to distinguish

Customizing Text Colors

SCSS Customization

Customize text colors in your SCSS variables:

// Custom theme colors
            $primary:   #007bff;
            $secondary: #6c757d;
            $success:   #28a745;
            $danger:    #dc3545;
            $warning:   #ffc107;
            $info:      #17a2b8;
            $light:     #f8f9fa;
            $dark:      #343a40;

            // Custom text colors
            $theme-colors: (
            "primary":    $primary,
            "secondary":  $secondary,
            "success":    $success,
            "danger":     $danger,
            "warning":    $warning,
            "info":       $info,
            "light":      $light,
            "dark":       $dark,
            "custom":     #8a2be2 // Add custom color
            );

            // Generate text color utilities
            @import "bootstrap/scss/bootstrap";

            // Custom CSS class
            .text-custom {
            color: #8a2be2 !important;
            }
Note: After adding custom colors to $theme-colors map, Bootstrap will automatically generate text color utilities like .text-custom.

Practical Examples

Example 1: Status Messages
Order Status

✅ Order #12345 has been shipped

⚠️ Order #12346 is processing

❌ Order #12347 was cancelled

ℹ️ All orders ship within 24 hours

<div class="alert alert-light">
            <h5>Order Status</h5>
            <p class="text-success">
                ✅ Order shipped
            </p>
            <p class="text-warning">
                ⚠️ Order processing
            </p>
            <p class="text-danger">
                ❌ Order cancelled
            </p>
            </div>
Example 2: Form Validation
✓ Valid email format
✗ Password must be at least 8 characters
⚠️ Username already taken

Best Practices

Text Color Best Practices
  • ✅ Use consistent color meanings throughout your site
  • ✅ Ensure sufficient contrast for readability
  • ✅ Use semantic colors (success for positive, danger for errors)
  • ✅ Combine with icons for colorblind users
  • ✅ Use muted colors for less important information
  • ✅ Test colors on different devices and screens
  • ✅ Consider dark mode compatibility