- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS HEX Colors
CSS Colors and Backgrounds
CSS HEX Colors
Write hexadecimal colors using short, long, and alpha forms, and understand how each pair of digits represents an sRGB channel.
#ff0000 is red. Once you see that the six characters are really three pairs, one each for red, green and blue, every HEX color becomes readable.
How a HEX color works
A HEX color starts with # and is followed by three pairs of characters: #RRGGBB. Each pair is a number from 00 (none of that color) to ff (as much as possible). Hexadecimal counts 0 to 9 and then a to f, so ff is 255 in normal numbers. HEX is just another way of writing the same red, green and blue values you saw in CSS RGB Colors. For the bigger picture, see CSS Colors.
| HEX | Red, green, blue | You get |
|---|---|---|
| #ff0000 | 255, 0, 0 | Red |
| #00ff00 | 0, 255, 0 | Green |
| #0000ff | 0, 0, 255 | Blue |
| #ffffff | 255, 255, 255 | White |
| #000000 | 0, 0, 0 | Black |
| #808080 | 128, 128, 128 | Grey |
When all three pairs are the same, the color is a grey. Higher numbers mean lighter.
Syntax
css
selector {
color: #RRGGBB;
}Example
css
p {
color: #ff0000;
}That paragraph turns red.
Where you can use it
| Property | What it colors | Example |
|---|---|---|
| color | Text | color: #0000ff; |
| background-color | The background | background-color: #f0f0f0; |
| border-color | The border | border-color: #ff0000; |
| outline-color | The focus outline | outline-color: #00ff00; |
Short HEX and transparent HEX
When each pair is a doubled character, you can write just one of each: #3a7 means #33aa77, and #fff means #ffffff. Add a fourth pair for transparency: #RRGGBBAA. #00000080 is black at about half opacity. Note that the alpha comes last here, which is different from some other tools.
Example
CSS Hex Colors Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>CSS HEX Colors</title>
<style>
body {
background-color: #f2f2f2;
}
h1 {
color: #0000ff;
}
p {
color: #008000;
}
div {
border: 2px solid #ff0000;
padding: 10px;
}
</style>
</head>
<body>
<h1>HEX Color Example</h1>
<p>This paragraph uses HEX color.</p>
<div>This box has a red border using HEX.</div>
</body>
</html>The page gets a light grey background, a blue heading, green text and a box with a red border.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| Hexadecimal colors | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
Good to know
- Upper and lower case mean the same thing:
#FF0000and#ff0000are identical. - A missing
#makes the whole declaration invalid, and the color is silently ignored. - HEX is great for copying exact colors from a design tool. When you need to adjust lightness or transparency by hand, HSL and modern color formats are easier to reason about.
CSS HEX Colors exercises
Six digits
Make the title red with the hex color #ff0000.
The HTML your code works on
<h2 class="title">Sale</h2>
<p class="text">All shoes half price.</p>
<div class="box">Box</div>Result
