CSS <hex-color> Data Type
The <hex-color> data type is a color written as # followed by 3, 4, 6 or 8 hexadecimal digits, such as #ff0000 or #f008 with transparency.
Description
The <hex-color> CSS data type represents a color written in hexadecimal notation: a hash symbol (#) followed by 3, 4, 6 or 8 hexadecimal digits. It is one of the ways to write a <color>, and probably the most widely used, appearing in properties such as color, background-color, border-color and box-shadow, as well as in gradients.
Each pair of digits in the six-digit form describes one channel of an sRGB color: red, green and blue, in that order. Hexadecimal digits run from 0 to 9 and then a to f, so each pair ranges from 00 (none of that channel, 0 in decimal) to ff (the full amount, 255 in decimal). For example, #ff0000 is pure red, #000000 is black and #ffffff is white. The three-digit form is a shorthand in which each digit is doubled, so #f80 is the same as #ff8800.
Adding a fourth or eighth digit adds an alpha (transparency) channel. In #RRGGBBAA, the last pair sets opacity from 00 (fully transparent) to ff (fully opaque), so #00000080 is black at about 50% opacity. The four-digit form, #RGBA, is the shorthand version. Hex colors are case-insensitive, so #FFCC00 and #ffcc00 are identical. The alpha forms are supported in modern browsers (Chrome 62, Firefox 49 and Safari 10 onwards).
Examples:
/* Six-digit form */
h1 {
color: #1e90ff;
}
/* Three-digit shorthand: same as #ff8800 */
.warning {
background-color: #f80;
}
/* Eight-digit form with 50% opacity */
.overlay {
background-color: #00000080;
}
/* Four-digit shorthand with alpha: same as #ffffffcc */
.glass {
background-color: #fffc;
}
Hex colors are compact and easy to copy from design tools, which is why they remain the default choice for many style guides. For colors that are easier to adjust by hand, consider hsl() or oklch().
Interactive Demo
Syntax
<hex-color> = #RGB | #RGBA | #RRGGBB | #RRGGBBAA
Values
- #RGBThree-digit shorthand. Each digit is repeated to form the full value, so #09c is the same as #0099cc.
- #RGBAFour-digit shorthand with an alpha channel. Each digit is repeated, so #09c8 is the same as #0099cc88.
- #RRGGBBSix-digit form. Each pair of digits gives the red, green and blue channels from 00 (0) to ff (255).
- #RRGGBBAAEight-digit form. The first three pairs give the red, green and blue channels, and the last pair gives the alpha channel from 00 (fully transparent) to ff (fully opaque).
Example
Browser Support
The following information will show you the current browser support for the CSS hex-color data type. Hover over a browser icon to see the version that first introduced support for this CSS data type.
Desktop





Tablets & Mobile





