Menu Search
Jump to the content X X
Smashing Conf Barcelona 2016

We use ad-blockers as well, you know. We gotta keep those servers running though. Did you know that we publish useful books and run friendly conferences — crafted for pros like yourself? E.g. upcoming SmashingConf Barcelona, dedicated to smart front-end techniques and design patterns.

Hex Colors The Code Side Of Color

The trouble with a color’s name is that it never really is perceived as the exact same color to two different individuals — especially if they have a stake in a website’s emotional impact. Name a color, and you’re most likely to give a misleading impression. Even something like “blue” is uncertain. To be more precise, it could be “sky blue”, “ocean blue”, “jeans blue” or even “arc welder blue”.

Descriptions vary with personal taste and in context with other colors. We label them “indigo”, “jade”, “olive”, “tangerine”, “scarlet” or “cabaret”. What exactly is “electric lime”? Names and precise shades vary — unless you’re a computer.

Code Demands Precision Link

When computers name a color, they use a so-called hexadecimal code that most humans gloss over: 24-bit colors. That is, 16,777,216 unique combinations of exactly six characters made from ten numerals and six letters — preceded by a hash mark. Like any computer language, there’s a logical system at play. Designers who understand how hex colors work can treat them as tools rather than mysteries.

Breaking Hexadecimals Into Manageable Bytes Link

Pixels on back-lit screens are dark until lit by combinations of red, green, and blue. Hex numbers represent these combinations with a concise code. That code is easily broken. To make sense of #970515, we need to look at its structure:

The first character # declares that this “is a hex number.” The other six are really three sets of pairs: 0–9 and a–f. Each pair controls one primary additive color.

Hex Reading
The higher the numbers are, the brighter each primary color is. In the example above, 97 overwhelms the red color, 05 the green color and 15 the blue color.

Each pair can only hold two characters, but #999999 is only medium gray. To reach colors brighter than 99 with only two characters, each of the hex numbers use letters to represent 10–16. A, B, C, D, E, and F after 0–9 makes an even 16, not unlike jacks, queens, kings and aces in cards.

Diagram showing how hex colors pass above 0-9

Being mathematical, computer-friendly codes, hex numbers are strings full of patterns. For example, because 00 is a lack of primary and ff is the primary at full strength, #000000 is black (no primaries) and #ffffff is white (all primaries). We can build on these to find additive and subtractive colors. Starting with black, change each pair to ff:

  • #000000 is black, the starting point.
  • #ff0000 stands for the brightest red.
  • #00ff00 stands for the brightest green.
  • #0000ff stands for the brightest blue.

Subtractive colors start with white, i.e. with the help of #ffffff. To find subtractive primaries, change each pair to 00:

  • #ffffff is white, the starting point.
  • #00ffff stands for the brightest cyan.
  • #ff00ff stands for the brightest magenta.
  • #ffff00 stands for the brightest yellow.

Mixing additive colors to make subtractives

Shortcuts In Hex Link

Hex numbers that use only three characters, such as #fae, imply that each ones place1 should match the sixteens place. Thus #fae expands to #ffaaee and #09b really means #0099bb. These shorthand codes provides brevity in code.

In most cases, one can read a hex number by ignoring every other character, because the difference between the sixteens place tells us more than the ones place. That is, it’s hard to see the difference between 41 and 42; easier to gauge is the difference between 41 and 51.

Diagram emphasizing the first character in each pair of characters

The example above has enough difference among its sixteens place to make the color easy to guess — lots of red, some blue, no green. This would provide us with a warm violet color. Tens in the second example (9, 9 and 8) are very similar. To judge this color, we need to examine the ones (7, 0, and 5). The closer a hex color’s sixteens places are, the more neutral (i.e. less saturated) it will be.

Make Hexadecimals Work For You Link

Understanding hex colors lets designers do more than impress co-workers and clients by saying, “Ah, good shade of burgundy there.” Hex colors let designers tweak colors on the fly to improve legibility, identify elements by color in stylesheets, and develop color schemes in ways most image editors can’t.

Keep Shades In Character Link

To brighten or darken a color, one’s inclination is often to adjust its brightness. This makes a color run the gamut from murky to brilliant, but loses its character on either end of the scale. For example, below a middle green becomes decidedly black when reduced to 20% brightness. Raised to 100%, the once-neutral green gains vibrancy.

A funny thing happens when we treat hex colors as if they were increments of ten. By adding one to each of the left-hand character of each pair, we raise a color’s brightness while lowering its saturation. This prevents shades of a given color from wandering too closely to pitch black or brilliant neon. Altering hex pairs retains the essence of a color.

Diagram showing how hex affects brightness and saturation

In the example above, the top set of shades appears to gain yellow or fall to black, even though it’s technically the same green hue. By changing its hex pairs, the second set appears to keep more natural shades.

Faded Underlines Link

By default, browsers underline text to denote links. But thick underlines interfere with letters’ descenders. Designers can make underlines less obtrusive by scaling back hex colors. The idea is to make the tags closer to the background color, while the text itself gains contrast against the background.

  • For dark text on a bright background, we make the links brighter.
  • For bright text on a dark background, we make the links darker.

To make this work, every embedded link needs a <span> inside of every <a>:

a { text-decoration:underline;color:#aaaaff; }

a span { text-decoration:none;color:#0000ff; }

Example of underlines that pale compared to the clickable text

As you can see here, underlines in the same color as the text can interfere with parts of type that drop below the baseline. Changing the underline to resemble the background more closely makes descenders easier to read, even though most browsers place underlines above the letterforms.

Adding spans to every anchor tag can be problematic. A popular alternative is to remove underlines and add border-bottom:

a { text-decoration: none; border-bottom: 1px solid #aaaaff; }

Better Body Copy Link

A recurring design problem is that a specific color may be technically correct but has an unintended effect. For example, some designs call for headers and body copy to be the same color. We have to keep in mind that the thicker the strokes of large text appears, the darker the small text appears.

Example of text that, while technically correct, appears too bright

h1, p { color: #797979; }

Example of text technically darker but visually the same

h1 { color: #797979; }

p { color: #393939; }

While technically identical, the body of the copy is narrower, and more delicate letterforms make it visually brighter than the heading. Lowering the sixteens places will make the text easier to read.

How To Warm Up Or Cool Down A Background Link

Neutral backgrounds may be easy to read against, but “neutral” doesn’t have to mean “bland”. Adjusting the first and last byte can make a background subtly warmer or cooler.

Examples with slight background color variations

  • #404040 — neutral
  • #504030 — warmer
  • #304050 — cooler

Is that too much? For a more subtle shift, use the ones places instead:

Examples of very slight variations in background color

  • #404040 — neutral
  • #4f4040 — warmer
  • #40404f — cooler

Coordinate Colors With Copy-Paste Link

Recognizing the structure of a hex number’s number/letter pairs gives designers a unique tool to explore color combinations. Unlike color wheels and charts, rearranging pairs in a hex number is a simple process to change hues while keeping values similar. As a bonus, the results can be unpredictable. The simplest technique is to move one pair of characters to a different spot, which trades primary colors.

A common design technique to make text or other visual elements coordinate with a photo is to use colors from within that photo. Understanding hex colors can take that a step further, by deriving new colors that coordinate with the photo without taking directly from the photo.

Examples of how swapping primary colors can yield coordinated but interesting results

Going Forward Link

Don’t let the code intimidate you. With a little creativity, hex colors are a tool at your disposal. If nothing else, next time someone asks if you can solve a problem with code in any language, you can simply say:

“Shouldn’t be harder than parsing hexadecimal triplets in my head.”

Further Reading Link

You may be interested in the following articles and related resources:


Footnotes Link

  1. 1
  2. 2
  3. 3
SmashingConf Barcelona 2016

Hold on, Tiger! Thank you for reading the article. Did you know that we also publish printed books and run friendly conferences – crafted for pros like you? Like SmashingConf Barcelona, on October 25–26, with smart design patterns and front-end techniques.

↑ Back to top Tweet itShare on Facebook


Ben Gremillion is a Writer at ZURB. He started his career in newspaper and magazine design, saw a digital future, and learned HTML in short order. He writes tutorials and facilitates the ZURB University training courses.

  1. 1

    Kris Van Houten

    October 4, 2012 5:40 am

    Awesome article. I have often wondered how the heck hex codes are structured, this article makes it look so much more simple than I previously thought. Thank you!

    • 2

      I have done the same but never bothered to learn more about it! Reading about the RGB part really made me have an “ah-ha” moment. I always assumed #09b meant #09b09b.

      • 3

        I too was under that impression, but for the sake of ease I always use the full 6-digit code when using hex, even when all six digits are the same. It’s just slightly easier to read.

        This article explains a lot, and may require more than one read to understand fully I feel.

  2. 4

    Fantastic article! I’ve been meaning to look into how these hex values work. I’m glad I could find it here. Thanks!

  3. 5

    Excellent report. =)

  4. 6

    Wow, perfect job!

  5. 7

    Sriram Velamur

    October 4, 2012 6:30 am

    Wonderful piece. Thanks a ton for making things on the hexworld this clear.

  6. 8

    Peter Müller

    October 4, 2012 6:30 am

    There are other color spaces that are much more intuitive to do a lot of these things in. HSL or HSV are easier to understand by humans, which is also the reason why color pickers focus om visualizing those.

  7. 9

    That is a great article, Very detailed and informative. The basic structure of the hex numbers I knew but the added stuff on how to work with them was awesome.

    • 10

      This was my main takeaway from this article too. :)
      The additional tips on mixing up or dividing hex values for color-matching was handy for hex-familiar people like us.

  8. 11

    This is perfect! (i need more color theory :))
    Thank You very much,

  9. 12

    Lanny Heidbreder

    October 4, 2012 6:43 am

    Quality of information: ★★★★★

    My happiness with this article now that all my secrets are exposed to the world: ★☆☆☆☆

  10. 13

    Michael Brown

    October 4, 2012 7:02 am

    Nice article, even for someone who already knows all this stuff. But please….the most significant digit in a hex byte is NOT the “tens place”. Hex is base 16, not base 10. The “tens place” is for decimal (base 10) numbers.

  11. 14

    Nice article.
    But why don’t you use #ff0000 for Red. That would’ve been more accurate.

    • 15

      Having used full saturated colors in the graphics represented would have killed readers’ eyes.

  12. 16

    Got to agree with Peter.

    Learning to use hex code can be useful for recognizing colors in other peoples code, but there are much better ways to work with color out there.

    If your using a pre-processor you can use HSL with confidence (as it will convert to hex so everything works on older browsers). Using HSL is amazing. It’s made such a difference to my workflow. Being able to look at color value and easily recognize it and make small adjustments is a massive time saver.

    Bottom line hex/RGB are for computers – human designers shouldn’t have to bother with them.

  13. 17

    Just FYI, there is no “tens” place in a hexadecimal pair. It’s the “sixteens” place. Aside from that, good job!

  14. 18

    Thank you for this in depth look into hexadecimal colors.
    Here is my proposal for further simplified hexcodes:

  15. 19

    This is great find!
    I wonder if there are any graphic tools/add-ins that do this kind of processing.

  16. 23

    Amazing tutorial. thank you for making HEX so much easier to understand!

    PS: Hope you don’t mind that I added it to (It’s a side project of mine :)

  17. 24

    Thanks Ben, this was really well done. The Mathematician in me winced a little every time you referred to the “tens digit,” but I’ll let it slide ;)

  18. 25

    Good article, but the phrase “each ones place should match the tens place” is confusing. Since we’re talking about hexadecimal here, the left-hand digit is not the “tens place” but the “sixteens place”.

    • 26

      I thought “tens” would cause less confusion. But you’re right, technically it’s base 16. Actually, I like the way you phrased it: left-hand digit covers all the bases.

  19. 27

    Srinivas Reddy

    October 4, 2012 10:47 am

    Great article. The hexa world looks little brighter now. Thanks for share!

  20. 28

    You make a good point with color naming helping people think about it (it’s worked for J Crew).

    A nice thing to do is to use a CSS language like SASS or LESS and create and name your colors at the top of the file. It’s not semantic to have a variable called “BlueJeans” but damn, it makes it easier to remember which color you’re talking about.
    #header {

    • 29


      June 24, 2014 5:31 pm

      In a similar vein, I create classes in my CSS for the major colors used in my project. For the newbies reading, an example:

      In the css file:
      .bluejeanstext {color: #213e57;}
      .bluejeanscontainers {background: #213e57;}

      The HTML:

      Yay blue jeans!

      Not a perfect solution, but as a designer first, developer second, using class names actually helps me visualize the colors in my head as I code–and it saves me time in the long run.

      • 30


        June 24, 2014 5:33 pm

        Oops, it hid the HTML!

        Basically, I assigned the .bluejeanstext class to a paragraph tag, and the .bluejeanscontainers class to the surrounding div.


↑ Back to top