Color utilities help you quickly change text and background colors without writing custom CSS. They make your website look nice and clear.
Color utilities (text, background) in Bootsrap
text-{color}
bg-{color}Replace {color} with Bootstrap color names like primary, success, danger, warning, info, light, dark, or white.
Use text- classes to change text color and bg- classes to change background color.
<p class="text-primary">This text is blue.</p>
<div class="bg-success text-white p-3">Green background with white text.</div>
<span class="text-danger">Warning message in red.</span>
<section class="bg-light text-dark p-4">Light background with dark text.</section>
This example shows different Bootstrap color utility classes applied to text and backgrounds. It uses text-primary for blue text, text-success for green text, bg-warning for a yellow background, bg-danger for a red button background, and bg-info for a light blue section background with white text. Padding and margin classes add spacing.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Color Utilities Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container my-5"> <h1 class="text-primary">Hello, Bootstrap Colors!</h1> <p class="text-success">This text is green to show success.</p> <div class="bg-warning text-dark p-3 mb-3">Warning background with dark text.</div> <button class="btn bg-danger text-white">Danger Button</button> <section class="bg-info text-white p-4 mt-4 rounded"> <p>This section has an info background and white text.</p> </section> </main> </body> </html>
Bootstrap color utilities are responsive and accessible by default.
Use padding (p-*) and margin (m-*) classes to add space around colored elements.
For better accessibility, ensure sufficient contrast between text and background colors.
Use text-{color} to change text color quickly.
Use bg-{color} to change background color easily.
Bootstrap provides many color options to keep your design consistent and simple.
Practice
Solution
Step 1: Understand Bootstrap text color classes
Bootstrap usestext-{color}classes to change text color, where {color} is a predefined color name.Step 2: Identify the correct class for red text
The classtext-dangerapplies a red color to text. The other options either apply background color or use incorrect class names.Final Answer:
text-danger -> Option BQuick Check:
Text color red = text-danger [OK]
- Confusing text and background classes
- Using non-existent classes like text-red
- Using bg-danger for text color
Solution
Step 1: Recall Bootstrap background color classes
Bootstrap usesbg-{color}classes to set background colors, where {color} is a predefined color name.Step 2: Identify the correct class for blue background
The classbg-primarysets a blue background. The other options either set text color or use invalid class names.Final Answer:
bg-primary -> Option DQuick Check:
Background blue = bg-primary [OK]
- Using text-primary for background
- Using non-Bootstrap class names like bg-blue
- Confusing text and background prefixes
<p class="text-success">Success message</p>
Solution
Step 1: Identify the Bootstrap text color class
The classtext-successis used on the paragraph tag.Step 2: Recall the color meaning of text-success
In Bootstrap,text-successapplies a green color to the text.Final Answer:
Green -> Option AQuick Check:
text-success = Green [OK]
- Confusing success with danger (red)
- Mixing text and background colors
- Guessing blue or yellow incorrectly
bg-warningg. What will happen?Solution
Step 1: Check the class name spelling
The correct class for yellow background isbg-warning, butbg-warningghas an extra 'g' and is invalid.Step 2: Understand how browsers handle invalid classes
Invalid classes are ignored by the browser, so no background color will be applied, resulting in transparent background.Final Answer:
The background will be transparent (no color) -> Option CQuick Check:
Invalid class = no style applied [OK]
- Assuming a typo still applies the color
- Expecting an error message in browser
- Confusing background and text color effects
Solution
Step 1: Identify the class for red background
The classbg-dangersets a red background color in Bootstrap.Step 2: Identify the class for white text
The classtext-whitesets the text color to white.Step 3: Combine classes for desired effect
Usingbtn bg-danger text-whiteapplies a red background and white text on the button.Final Answer:
class="btn bg-danger text-white" -> Option AQuick Check:
Red background + white text = bg-danger + text-white [OK]
- Mixing text-danger (red text) with bg-danger (red background)
- Using bg-white for background when red is needed
- Using same color for text and background causing unreadable text
Ready to prove it?
Pass the test and this pattern moves unknown
