Favorites

CSS <hex-color> Data Type

<hex-color>
CSS Data TypeThis data type is supported by all modern browsers.
Updated 21st November 2026

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.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

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

Colour swatch
Demo Code
HTML Code
<div class="demo-checker">
	<div class="demo-element">Colour swatch</div>
</div>
CSS Code
.demo-checker {
	width: 296px;
	padding: 18px;
	box-sizing: border-box;
	background: repeating-conic-gradient(#ddd 0 25%, #fff 0 50%) 0 0 / 20px 20px;
}
.demo-element {
	width: 260px;
	padding: 18px;
	border-radius: 8px;
	font-size: 16px;
	font-weight: bold;
	color: #fff;
	text-shadow: 0 1px 2px rgb(0 0 0 / 40%);
	background: #4361ee;
}

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

<div class="wrap">
<h1>CSS &lt;hex-color&gt; examples</h1>
<div class="swatches">
<div class="swatch rgb">
<div class="chip"></div>
<div class="meta"><strong>#RGB</strong><code>#f80</code></div>
</div>
<div class="swatch rgba">
<div class="chip"></div>
<div class="meta"><strong>#RGBA</strong><code>#f808</code></div>
</div>
<div class="swatch rrggbb">
<div class="chip"></div>
<div class="meta"><strong>#RRGGBB</strong><code>#ff8800</code></div>
</div>
<div class="swatch rrggbbaa">
<div class="chip"></div>
<div class="meta"><strong>#RRGGBBAA</strong><code>#ff880080</code></div>
</div>
</div>
</div>
/* Demonstrates the four  forms */
* { box-sizing: border-box; }

body {
  margin: 24px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
  background: #f5f7fa;
  color: #111827;
}

.wrap {
  max-width: 860px;
  margin: 0 auto;
}

h1 {
  font-size: 20px;
  margin: 0 0 16px;
}

.swatches {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.swatch {
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.06);
}

/* checkerboard so transparency is visible */
.chip {
  height: 90px;
  background-image:
    linear-gradient(45deg, #ddd 25%, transparent 25%),
    linear-gradient(-45deg, #ddd 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #ddd 75%),
    linear-gradient(-45deg, transparent 75%, #ddd 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}

.chip::after {
  content: '';
  display: block;
  height: 100%;
}

.rgb .chip::after { background: #f80; }
.rgba .chip::after { background: #f808; }
.rrggbb .chip::after { background: #ff8800; }
.rrggbbaa .chip::after { background: #ff880080; }

.meta {
  padding: 10px 12px;
}

.meta strong {
  display: block;
  font-size: 13px;
}

.meta code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-size: 12px;
  color: #4b5563;
}
Open in editor

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.

This data type is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.