Color Highlighting with React Component

Oct 6, 2025
reacthighlightstylingcolorstutorial
5   Minutes
821   Words

Complete Guide to React Highlight Component

Master the React Highlight component for beautiful text styling, color highlighting, and advanced typography in your Astro MDX files.

🚀 Quick Start

Import the Component

import Highlight from "@/react/Highlight/Highlight.jsx";

or

import Highlight from "../../react/Highlight/Highlight.jsx";

Important: Use .mdx extension (not .md) when working with React components.

Basic Usage

<Highlight> Your highlighted text </Highlight>
Your highlighted text

🎨 Color Modes Explained

Background Mode (Default - highlight='bg')

How it works: Uses the color prop for background color with white text. If no highlight mode is specified, it defaults to background mode.

<Highlight color='#ff6b6b'> Red background with white text </Highlight>
Red background with white text
<Highlight color='#4299e1'> Blue background with white text </Highlight>
Blue background with white text

Foreground Mode (highlight='fg')

How it works: Uses the color prop for text color only. No background styling.

<Highlight color='#800031' highlight='fg'> Red text color </Highlight>
Red text color
<Highlight color='#004080' highlight='fg'> Navy text color </Highlight>
Navy text color

Light Background Mode (highlight='lightBg')

How it works: Uses the color prop for light background with purple text (#4B0082).

<Highlight highlight='lightBg'> Light background with purple text </Highlight>
Light background with purple text
<Highlight highlight='lightBg' color='#D1FFBD'> Custom light background with purple text </Highlight>
Custom light background with purple text

🎯 Typography Controls

Font Weight

<Highlight color='#ff6b6b' highlight='fg' fontWeight='bold'> Bold text </Highlight>
Bold text
<Highlight color='#ff6b6b' highlight='fg' fontWeight='lighter'> Light text </Highlight>
Light text

Font Style

<Highlight color='#4299e1' highlight='fg' fontStyle='italic'> Italic text </Highlight>
Italic text
<Highlight color='#4299e1' highlight='fg' fontStyle='oblique'> Oblique text </Highlight>
Oblique text

📐 Text Alignment

Block Display Alignment

<Highlight color='#2d5aa0' highlight='format' textAlign='left' display='block'> Left aligned </Highlight>
Left aligned
<Highlight color='#c41e3a' highlight='format' textAlign='center' display='block'> Center aligned </Highlight>
Center aligned
<Highlight color='#0d7f3f' highlight='format' textAlign='right' display='block'> Right aligned </Highlight>
Right aligned

Justified Text

<Highlight color='#8b5a3c' highlight='format' textAlign='justify' display='block'>
This is justified text that stretches to fill the full width of the container, <br></br> creating even margins on both sides.
</Highlight>

This is justified text that stretches to fill the full width of the container,
creating even margins on both sides.

🎨 Advanced Examples

Headers with Highlight

# <Highlight color='#004080' highlight='fg' fontWeight='bold'> Foreground Blue Header </Highlight>

Foreground Blue Header

## <Highlight color='#800031' highlight='fg' fontWeight='bold'> Foreground Brown Header </Highlight>

Foreground Brown Header

Combined Styling

<Highlight color='#ffc107' highlight='fg' fontWeight='bold' fontStyle='italic'> Bold italic warning </Highlight>
Bold italic warning
<Highlight color='#28a745' highlight='fg' fontWeight='bold'> Bold success message </Highlight>
Bold success message

Custom Format Mode

<Highlight highlight='format' color='red' textAlign='right' display='block' fontWeight='bolder' textDecoration='underline wavy red' fontStyle='oblique' fontSize='22px'> Advanced styling </Highlight>
Advanced styling

🎨 Mixed Markdown Examples

Bold Text with Highlight

You can combine <Highlight color='#ff6b6b'> **bold** markdown</Highlight> with the Highlight component for emphasis.

You can combine bold markdown with the Highlight component for emphasis.

Headers and Lists

Important Notice

  • This is a highlighted list item using markdown
  • Another item with italic emphasis and highlighting
  • Warning: Always test your combinations

Check out this <Highlight color='#6f42c1'>inline code</Highlight> example and visit our docs for more information.

📋 Component Props Reference

PropTypeDescriptionSupported ModesDefault
colorstringHex color codeAll modes-
highlightstringMode: ‘bg’, ‘fg’, ‘lightBg’, ‘format’All’bg’
fontWeightstring’normal’, ‘bold’, ‘lighter’, ‘bolder''fg’, ‘format''normal’
fontStylestring’normal’, ‘italic’, ‘oblique’All modes’normal’
textAlignstring’left’, ‘center’, ‘right’, ‘justify''fg’, ‘format''left’
displaystring’inline’, ‘block''fg’, ‘format''inline’
textDecorationstringCSS text-decoration’fg’, ‘format''none’
fontSizestringSize in pixels’format’ only’inherit’

🎯 Mode-Specific Behavior

Background Mode (highlight='bg')

  • Uses color for background color
  • Fixed white text color (#fff)
  • Supports fontStyle only
  • Rounded corners and padding applied automatically
  • Default mode when no highlight prop is provided

Foreground Mode (highlight='fg')

  • Uses color for text color only
  • Supports all typography props: fontWeight, fontStyle, textAlign, display, textDecoration
  • No background styling

Light Background Mode (highlight='lightBg')

  • Uses color for light background color
  • Fixed purple text color (#4B0082)
  • Supports fontStyle only
  • Subtle padding and rounded corners

Format Mode (highlight='format')

  • Uses color for text color
  • Supports all props including fontSize
  • Block-level display by default
  • Advanced styling capabilities

🎨 Color Suggestions

For Background Mode (with white text)

  • #ff6b6b - Vibrant red
  • #4299e1 - Professional blue
  • #28a745 - Success green
  • #ffc107 - Warning yellow
  • #dc3545 - Danger red
  • #6f42c1 - Purple
  • #20c997 - Teal
  • #fd7e14 - Orange

For Foreground Mode (text colors)

  • #800031 - Burgundy
  • #004080 - Navy blue
  • #0d7f3f - Forest green
  • #c41e3a - Crimson
  • #8b5a3c - Brown
  • #2d5aa0 - Royal blue
  • #1877F2 - Facebook blue
  • #ff6b6b - Coral

For Light Background Mode (with purple text)

  • #D1FFBD - Light green
  • #FFE4B5 - Light orange
  • #E6E6FA - Lavender
  • #F0FFF0 - Honeydew
  • #FFFACD - Lemon chiffon
  • #F5DEB3 - Wheat

🎯 Best Practices

Do’s

  • Use meaningful color combinations for accessibility
  • Combine multiple props for rich styling
  • Use display='block' for text alignment
  • Test in both light and dark themes
  • Choose appropriate colors from the suggestions above

Don’ts

  • Avoid using too many props on single elements
  • Don’t rely on default colors without theme testing
  • Avoid very small font sizes with complex styling
  • Don’t use light colors for background mode (use foreground mode instead)

🚀 Ready to Use Examples

Error Messages

<Highlight color='#f44336'> Error message in red background </Highlight>
Error message in red background

Success Messages

<Highlight color='#28a745'> Success message in green background </Highlight>
Success message in green background

Muted Text

<Highlight color='#C0C0C0'> Muted text in silver background </Highlight>
Muted text in silver background

Highlighted Notes

<Highlight highlight='lightBg' color='#D1FFBD'> Highlighted note with light background </Highlight>
Highlighted note with light background

Important Text

<Highlight color='#800031' highlight='fg' fontWeight='bold'> Important bold text </Highlight>
Important bold text

Emphasized Text

<Highlight color='#1877F2' highlight='fg' fontStyle='italic'> Emphasized italic text </Highlight>
Emphasized italic text

Default Highlight

<Highlight> Default highlight - works in any theme </Highlight>
Default highlight - works in any theme

Perfect for creating beautiful, accessible highlighted text in your Astro MDX content! 🎨⚛️

Article title Color Highlighting with React Component
Article author Anand Raja
Release time Oct 6, 2025
Copyright 2025
RSS Feed
Sitemap