Skip to main content

How to Write a Hex Color in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A hex color is a # followed by hexadecimal digits: two for red, two for green and two for blue, as in #0f6cbd. Three digits are the short form, and eight add transparency.

Syntax​

Syntax
#<red><green><blue>
#<red><green><blue><alpha>
PartWhat it does
#Starts a hex color. It's required.
<red>, <green>, <blue>How much of each color, from 00, none, to ff, the most, in hexadecimal: the digits 0 to 9 and the letters a to f. With one digit each, the digit is doubled: #06c is #0066cc.
<alpha>Optional. How opaque the color is, from 00, fully transparent, to ff, fully opaque.

MDN's hex-color page lists the four forms: three, four, six and eight digits.

The Example​

The example is three blue bars with white text, each colored with a different form: six digits, three digits and eight digits.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element giving paragraphs white text, the class six the background 0f6cbd, the class three the background 06c and the class eight the background 0f6cbd80, and three paragraphs, one with each class.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hex colors</title>
<style>
p {
color: #ffffff;
}

.six {
background: #0f6cbd;
}

.three {
background: #06c;
}

.eight {
background: #0f6cbd80;
}
</style>
</head>
<body>
<p class="six">#0f6cbd</p>
<p class="three">#06c</p>
<p class="eight">#0f6cbd80</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: three bars with white text, #0f6cbd in medium blue, #06c in a brighter blue, and #0f6cbd80 in a pale blue.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • #0f6cbd became rgb(15, 108, 189): 0f is 15 in hexadecimal, 6c is 108 and bd is 189.
  • #06c became rgb(0, 102, 204), the same as #0066cc: each digit was doubled.
  • #0f6cbd80 became rgba(15, 108, 189, 0.5): the same blue, half transparent, so the white page shows through and the bar looks pale. 80 is 128, about half of 255.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the #: color: 0f6cbdThe value is invalid and ignored: the text stayed blackStart with #
Using five digits: #0f6cbOnly three, four, six or eight digits are valid, so the value was ignored and the text stayed blackUse one of the four lengths

Troubleshooting​

SymptomLikely causeFix
The color doesn't applyThe # is missing, or the code has the wrong number of digitsCheck both
The color looks paler than expectedThe code has eight digits, and the last two make it transparentUse six digits for a solid color

FAQ​

Do uppercase letters work?​

Yes. #0F6CBD gave the same rgb(15, 108, 189). MDN's page says the syntax is case-insensitive.

What does a four-digit hex color do?​

It's the short form with transparency: #06c8 gave rgba(0, 102, 204, 0.533), the color of #06c at about half opacity.

Conclusion​

Write hex colors with a # and three, four, six or eight digits. Six digits give a solid color, and eight add transparency.

Additional Resources​