Hex colors let you pick exact colors for your website using a simple code. They help make your site look nice and match your style.
Hex colors in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
color: #RRGGBB;
Each pair (RR, GG, BB) is a two-digit hexadecimal number from 00 to FF.
Hex colors start with a # followed by 6 hex digits (0-9, A-F).
color: #FF0000;
background-color: #00FF00;
border-color: #0000FF;
color: #333333;
This webpage shows how hex colors change text and background colors. The background is a soft blue, the heading is blue, normal text is dark gray, and a highlighted box uses bright yellow.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Hex Colors Example</title> <style> body { background-color: #f0f8ff; color: #333333; font-family: Arial, sans-serif; padding: 2rem; } h1 { color: #0077cc; } p.highlight { background-color: #ffeb3b; color: #000000; padding: 1rem; border-radius: 0.5rem; max-width: 20rem; } </style> </head> <body> <h1>Hex Colors in CSS</h1> <p>This text uses a dark gray color <code>#333333</code>.</p> <p class="highlight">This highlighted box uses a bright yellow background <code>#ffeb3b</code>.</p> </body> </html>
Hex colors are case-insensitive, so #FF0000 and #ff0000 are the same.
You can use shorthand hex colors like #F00 for #FF0000, but 6 digits are clearer for beginners.
Use browser DevTools (right-click > Inspect) to try changing hex colors live and see results immediately.
Hex colors use 6-digit codes starting with # to define colors in CSS.
Each pair of digits controls red, green, and blue parts of the color.
They help you pick exact colors for text, backgrounds, borders, and more.
Practice
#FF0000 represent in CSS?Solution
Step 1: Understand hex color format
Hex colors start with#followed by 6 digits: two for red, two for green, two for blue.Step 2: Analyze the code
The first two digits#FF0000FFmean full red, the next two00mean no green, and the last two00mean no blue.Final Answer:
Bright red color -> Option CQuick Check:
Red = FF, Green = 00, Blue = 00 [OK]
- Confusing red with green or blue
- Ignoring the order of RGB in hex
- Thinking #FF0000 is black
Solution
Step 1: Check hex color syntax rules
A valid hex color starts with#followed by exactly 6 hexadecimal digits (0-9, A-F).Step 2: Validate each option
#12345G has a 'G' which is invalid. 123456 misses the#. #1A2B3C has valid digits and#. #XYZ123 has invalid letters 'X' and 'Z'.Final Answer:
#1A2B3C -> Option DQuick Check:
Valid hex = # + 6 hex digits [OK]
- Using letters outside A-F
- Omitting the # symbol
- Using fewer or more than 6 digits
color: #00FF00; display on the webpage?Solution
Step 1: Decode the hex color #00FF00
The first two digits00mean no red, the next twoFFmean full green, and the last two00mean no blue.Step 2: Identify the color from RGB values
Full green with no red or blue results in bright green color.Final Answer:
Green -> Option AQuick Check:
Green = FF, Red & Blue = 00 [OK]
- Mixing up red and green positions
- Assuming #00FF00 is blue
- Ignoring hex digit order
background-color: #12345; in your CSS. What is the problem?Solution
Step 1: Count digits in the hex code
The code#12345has only 5 digits after the#, but hex colors require exactly 6 digits.Step 2: Check other options
Letters are valid digits (1,2,3,4,5). Hex colors do not start with '0x' in CSS. So the only issue is missing one digit.Final Answer:
Hex code is missing one digit -> Option AQuick Check:
Hex colors need 6 digits after # [OK]
- Using 3, 5, or 7 digits instead of 6
- Confusing hex with other number formats
- Thinking '0x' prefix is needed
Solution
Step 1: Understand RGB parts in hex
Hex colors are#RRGGBB. Equal red and blue means red and blue digits are the same, green digits are zero.Step 2: Check each option
#FF00FF:#FF00FFhas red = FF, green = 00, blue = FF (equal red and blue, no green). #00FFFF has green = FF. #FFFF00 has green = FF. #0000FF has no red.Final Answer:
#FF00FF -> Option BQuick Check:
Red = Blue, Green = 00 means magenta [OK]
- Mixing green with blue or red
- Choosing colors with green included
- Not knowing hex digit order
