Highlighting text is a powerful visual cue used to draw attention to important information on a webpage—like a keyword, warning message, or special note. In CSS, you can highlight text by changing its background color and/or text color.
In this guide, we’ll show you how to highlight text color using CSS with simple, practical examples.
🎯 What Does “Highlighting Text” Mean?
In the context of CSS, highlighting usually means:
- Changing the background color of text
- Optionally, adjusting the text color for better contrast
Let’s explore how to do both.
🔹 1. Highlight Text Using background-color
This is the most common method to highlight text in CSS.
✅ Example:
<p class="highlight">Important: This action cannot be undone.</p>
.highlight {
background-color: yellow;
}
🟡 This makes the text appear with a yellow background—similar to using a highlighter pen.
🔹 2. Highlight with Background and Text Color
For better accessibility and contrast, it’s often best to change both the background and the text color.
✅ Example:
<p class="alert">Warning: You are about to delete a file.</p>
.alert {
background-color: #ffeb3b; /* light yellow */
color: #d32f2f; /* dark red */
font-weight: bold;
padding: 4px;
}
📌 This method improves visibility and helps users easily spot important content.
🔹 3. Highlight Specific Words with <span>
You don’t need to highlight an entire paragraph—you can style just a word or phrase using a <span>
tag.
✅ Example:
<p>Our new <span class="highlight-word">premium plan</span> is now available!</p>
.highlight-word {
background-color: #c8e6c9;
color: #2e7d32;
padding: 2px 4px;
border-radius: 4px;
}
✅ The result is a neatly highlighted phrase that stands out without being distracting.
🔹 4. Highlight on Hover (Interactive Highlight)
You can create dynamic highlights that appear when users hover over the text.
✅ Example:
<p class="hover-highlight">Hover over this text to highlight it.</p>
.hover-highlight:hover {
background-color: #f0f0f0;
color: #1976d2;
}
🖱️ This is great for navigation, tooltips, or interactive user experiences.
🧠 Accessibility Tip
When highlighting text:
- Always ensure there is sufficient contrast between text and background.
- Use tools like WebAIM Color Contrast Checker to verify accessibility.
💡 Summary
Method | Use Case | Example |
---|---|---|
background-color only | Simple highlight | background-color: yellow; |
With color + background-color | Clear and accessible emphasis | color: red; background-color: yellow; |
Using <span> | Highlight part of text | <span class="highlight">word</span> |
On Hover | Interactive effect | .hover-highlight:hover {} |
📌 Final Thoughts
Highlighting text with CSS is easy, flexible, and impactful. Whether you’re drawing attention to an important note or improving user interaction, CSS provides multiple ways to do it effectively.
Experiment with colors and styles, and always keep usability in mind.