coding help
Example hex codes and what the colors look like

In HTML and CSS, hex codes are used to represent colors. The numbers and letters in the code correspond to RGB/RGBA color values. For example, #000000 is black and #ffffff is white.

"Hex" stands for hexadecimal. This means a base 16 number system (as opposed to decimal, which is base 10, or binary, which is base 2). The digits 0-9 in hexadecimal mean the same thing as they do in base 10 (our usual number system). Then, for digits 10-15, we use the letters a-f. Thus, a means 10, b means 11, and so on.

Hex codes are not case-sensitive. For example, both #FFFFFF and #ffffff are white.

How hex codes work

Hex codes translate to RGB/RGBA values (but they're easy to copy-paste). For each digit, a value of 0 means no color, a value of f is maximum color, and the values 1-e represent values in between.

 
#000000 = black
 
#ffffff = white
 
#808080 = medium gray

The first two digits represent how much red is in the color. The second two represent how much green is in it, and the last two represent how much blue is in it.

 
#ff0000 = bright red
 
#00ff00 = bright green
 
#0000ff = bright blue

You can use hex codes to represent any color you'd like.

 
#4090BD = teal
 
#E88BA0 = pink
 
#F9F7CF = pale yellow

When you look at a hex code, you can sometimes guess what a color might look like. For example, #4090BD has not much red (40), a medium amount of green (90), and a lot of blue (BD). That color is teal.

Three-digit hex codes

You can also use three-digit hex codes. These codes use one digit for each R, G, and B value instead of two. They expand to the same digit being repeated twice. (For example, #abc = #aabbcc.)

 
#ff0000 = #f00
 
#00ff00 = #0f0
 
#0000ff = #00f

Adding an alpha channel

You can represent an "alpha channel" in hex code. "Alpha" means transparency. You represent this by adding an extra value at the end. If your hex code has 2 digits per channel (r, g, and b), then you'll use 2 digits for alpha. If it has 1 digit per channel, then you'll use 1 digit.

Let's show how that looks.

 
#abc0 = No color (alpha is 0)
 
#abc8 = Some color (alpha is 8)
 
#abcf = All color (alpha is f)

Getting a hex code

Hex color picker

There are different ways to get the hex code you want:

As you work on your web design skills, you will eventually come up with a favorite way to get colors.

Using hex codes

Hex codes can be used anywhere colors are used. Here are some examples of hex colors used in CSS:

Background & text color
background-color: #1A3069; color: #77BDD0;
Border & box shadow
border: 1px solid #4090BD; box-shadow: 2px 2px 2px #77BDD0;
Gradient
background-color: #F9F7CF; background: linear-gradient(to right, #E88BA0, #F9F7CF); color: #000;

Best practices

See also: Color theory and Accessibility

Consider color theory and accessibility as you choose your colors. These will help you choose colors that are appealing and won't clash or cause headaches.

Here are some things to consider:

Colors should never overwhelm, distract, or confuse users. Instead, they should create an appealing experience.

See also

External links

You can also go to W3schools for a list of color names and their hex codes.

References

  1. Frankowska-Takhari, Sylwia; Hassell, Jonathan; Autism Accessibility Guidelines research project (PDF), Hassell inclusion and National Autistic Society
  2. Accessibility tool audit, AlphaGov.GitHub.io
  3. Evans, Darrielle. A web accessibility checklist to make your content 100% compliant, HubSpot blog
  4. How to Meet WCAG (Quick Reference), w3 Web Accessibility Initiative