Background color utilities let you quickly change the background color of elements. This helps make your webpage look nice and organized.
Background color utilities in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
bg-{color}-{shade}Replace {color} with a color name like red, blue, or green.
Replace {shade} with a number like 50, 100, 500, or 900 to pick the shade.
bg-red-500
bg-blue-200
bg-green-900
This example shows three elements with different background colors using Tailwind's background color utilities. The section has a light blue background, the button is green, and the warning message has a red background. All use padding and rounded corners for better look and accessibility labels for screen readers.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Background Color Utilities Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <section class="bg-blue-100 p-4 rounded" aria-label="Light blue background section"> <h1 class="text-blue-900 font-bold">Hello with Blue Background</h1> <p>This section uses <code>bg-blue-100</code> for a soft blue background.</p> </section> <button class="bg-green-500 text-white px-4 py-2 rounded mt-6 hover:bg-green-700" aria-label="Submit button"> Submit </button> <div class="bg-red-300 p-3 mt-6 rounded" role="region" aria-label="Warning message"> <strong>Warning:</strong> This is an important message with a red background. </div> </body> </html>
Use hover:bg-{color}-{shade} to change background color when hovering over an element.
Background colors should have enough contrast with text for readability and accessibility.
Tailwind colors come in many shades from 50 (lightest) to 900 (darkest).
Background color utilities let you quickly add colors behind elements.
Use bg-{color}-{shade} classes to pick colors and shades.
Combine with padding, rounded corners, and accessibility labels for good design.
Practice
bg-blue-500 do to an element?Solution
Step 1: Understand the class prefix
The prefixbg-in Tailwind CSS means background color.Step 2: Interpret the color and shade
blue-500is a medium shade of blue in Tailwind's color palette.Final Answer:
Sets the background color to a medium blue shade -> Option AQuick Check:
bg- means background color, blue-500 is medium blue [OK]
- Confusing background color with text color
- Thinking it adds a border instead of background
- Assuming it makes element transparent
Solution
Step 1: Recall Tailwind background color syntax
The correct format isbg-{color}-{shade}with dashes.Step 2: Check each option
Onlybg-red-300matches the correct syntax exactly.Final Answer:
bg-red-300 -> Option DQuick Check:
Correct syntax uses dashes between parts [OK]
- Using underscores or no dash between color and shade
- Writing full words like 'background' instead of 'bg'
- Missing dash between bg and color
<div class="bg-green-700 p-4">Hello</div>
Solution
Step 1: Identify the background color class
The classbg-green-700sets the background color to green with shade 700.Step 2: Understand shade meaning
Shade 700 is a darker shade in Tailwind's green palette, so the background is dark green.Final Answer:
A dark green background -> Option CQuick Check:
Shade 700 means dark color in Tailwind [OK]
- Confusing padding with background color
- Thinking shade 700 is light instead of dark
- Assuming no background color without explicit style
<div class="bg-yellow400 p-2">Warning</div>
Solution
Step 1: Check the background color class syntax
The classbg-yellow400is missing a dash between color and shade.Step 2: Understand impact of syntax error
Without the dash, Tailwind does not recognize the class, so no background color is applied.Final Answer:
bg-yellow400 (missing dash) -> Option AQuick Check:
Background color classes need dashes between color and shade [OK]
- Forgetting dash between color and shade
- Blaming padding for missing background
- Ignoring class syntax errors
<button class="?">Click me</button>
Solution
Step 1: Understand the base and hover colors
The base background should be darker blue (shade 600), and hover should be lighter (shade 400) for visible change.Step 2: Check each option's hover effect
bg-blue-600 hover:bg-blue-400 text-white rounded p-3 usesbg-blue-600andhover:bg-blue-400, which means background lightens on hover.Step 3: Confirm other styles
Text color white, rounded corners, and padding are good for button style.Final Answer:
bg-blue-600 hover:bg-blue-400 text-white rounded p-3 -> Option BQuick Check:
Hover lighter than base for visible background change [OK]
- Using same shade for base and hover (no visible change)
- Making hover darker instead of lighter
- Forgetting rounded corners or padding for button style
