HTML HEX Colors
In this page:
<tagname style="color: #rrggbb;">content</tagname>
Reading a HEX Code
A HEX color like #FF5733 breaks into three pairs of digits: FF (red), 57 (green), and 33 (blue), where each pair is a hexadecimal number from 00 to FF -- equivalent to a decimal 0-255 rgb() value written in base 16 instead of base 10.
Example: Reading a HEX Code
<p style="color: #FF5733;">Custom hex color text</p>
The 3-Digit Shorthand
When each pair of digits repeats itself, like #FFFFFF or #112233, CSS allows a shorter 3-digit form -- #FFF or #123 -- where each single digit is automatically doubled by the browser to reconstruct the full 6-digit code.
Example: The 3-Digit Shorthand
<p style="color: #f00;">Shorthand red</p>
<p style="color: #FF0000;">Full form red</p>
Grayscale HEX Values
Just like RGB, a HEX code with all three pairs identical produces a shade of gray -- #000000 is black, #FFFFFF is white, and #808080 is a mid-tone gray, since 80 in hexadecimal equals 128 in decimal, right in the middle of the 0-255 range.
Example: Grayscale HEX Values
<p style="color: #808080;">Mid-tone gray text</p>
Adding Transparency with 8-Digit HEX
Appending two extra hex digits to a 6-digit code adds an alpha transparency channel, ranging from 00 (fully transparent) to FF (fully opaque) -- #FF000080 is red at roughly 50% opacity, since 80 in hex is about half of FF.
Example: Adding Transparency with 8-Digit HEX
<div style="background-color: #FF000080;">Semi-transparent red</div>
Using HEX Across HTML and CSS
HEX codes work anywhere a color value is accepted, whether written inline on an HTML element's style attribute or inside a linked CSS stylesheet, and are the format most design tools (like Figma or Photoshop) export by default, making them the easiest format to copy directly from a design file into code.
Example: Using HEX Across HTML and CSS
<p style="color: #FF5733;">Inline hex color</p>
<style>
.box { background-color: #FF5733; }
</style>
<div class="box">Internal CSS hex color</div>
- Forgetting the leading # symbol, without which the browser cannot tell the value apart from a plain word or CSS keyword.
- Mixing up HEX digit pairs, not realizing each pair (RR, GG, BB) represents one channel from 00 to FF, exactly like 0-255 in RGB.
- Using a 3-digit shorthand like #f00 and assuming it works everywhere identically to 6-digit HEX, when some tools do not accept the short form.
- HEX codes use six hexadecimal digits, in pairs, to represent the red, green, and blue channels: #RRGGBB.
- A 3-digit shorthand like #abc expands to #aabbcc when each digit is simply doubled.
- An optional 4th or 8th pair of digits adds an alpha transparency channel, just like the alpha value in rgba().
HEX color notation has been supported since the earliest versions of HTML and CSS; the 8-digit alpha-channel HEX format is supported in all modern browsers.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- HTML Favicon
- HTML Page Title
- HTML Tables
- HTML Lists
- HTML Block & Inline
- HTML Comments
- HTML Colors
- HTML CSS
- HTML Links
- HTML Images
- HTML RGB Colors
- HTML HEX Colors
- HTML HSL Colors
- HTML Link Colors
- HTML Link Bookmarks
- HTML Background Images
- HTML Table Borders
- HTML Table Sizes
- HTML Table Headers
- HTML Table Padding & Spacing
- HTML Colspan & Rowspan
- HTML Table Styling
- HTML Table Colgroup
- HTML Unordered Lists
- HTML Ordered Lists
- HTML Other Lists / Description Lists