Skip to main content

CSS Colors and Backgrounds

CSS HEX Colors

Written by Updated CSS HEX Colors exercises

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.

HEXRed, green, blueYou get
#ff0000255, 0, 0Red
#00ff000, 255, 0Green
#0000ff0, 0, 255Blue
#ffffff255, 255, 255White
#0000000, 0, 0Black
#808080128, 128, 128Grey

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

PropertyWhat it colorsExample
colorTextcolor: #0000ff;
background-colorThe backgroundbackground-color: #f0f0f0;
border-colorThe borderborder-color: #ff0000;
outline-colorThe focus outlineoutline-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 colors11211

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: #FF0000 and #ff0000 are 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

Check your answer to see it here.