How come certain random strings produce colors when entered as background colors in HTML? For example:

<body bgcolor="chucknorris"> test </body>

...produces a document with a red background across all browsers and platforms.

Interestingly, while chucknorri produces a red background as well, chucknorr produces a yellow background.

What's going on here?

share|improve this question
188  
jsfiddle.net/rBpVD – biziclop Nov 30 '11 at 21:53
122  
Related to this: In the Legend of zelda games, chickens are called "occoo's". This comes from the hexadecimal color code 00CC00 which was the color of Link's tunic in the first LoZ game. ..Just saying. – poepje Mar 20 '15 at 18:18
85  
I want to know how you stumbled upon this in the first place! – Danny Buonocore Jul 28 '16 at 20:13
17  
As a side note: don't use bgcolor. Use CSS background. – Jan Dvorak Sep 4 '16 at 12:40
28  
If 'onomatopoeia' is where a word sounds like the thing it represents (e.g. 'pop'), perhaps we should define 'chromapoeia' for where a word is automatically rendered in the colour of the thing it represents? – Richard Pawson Sep 27 '16 at 21:23
up vote 5111 down vote accepted
+50

It's a holdover from the Netscape days:

Missing digits are treated as 0[...]. An incorrect digit is simply interpreted as 0. For example the values #F0F0F0, F0F0F0, F0F0F, #FxFxFx and FxFxFx are all the same.

It is from the blog post A little rant about Microsoft Internet Explorer's color parsing which covers it in great detail, including varying lengths of color values, etc.

If we apply the rules in turn from the blog post, we get the following:

  1. Replace all nonvalid hexadecimal characters with 0's

    chucknorris becomes c00c0000000
    
  2. Pad out to the next total number of characters divisible by 3 (11 -> 12)

    c00c 0000 0000
    
  3. Split into three equal groups, with each component representing the corresponding colour component of an RGB colour:

    RGB (c00c, 0000, 0000)
    
  4. Truncate each of the arguments from the right down to two characters

Which gives the following result:

RGB (c0, 00, 00) = #C00000 or RGB(192, 0, 0)

Here's an example demonstrating the bgcolor attribute in action, to produce this "amazing" colour swatch:

<table>
  <tr>
    <td bgcolor="chucknorris" cellpadding="8" width="100" align="center">chuck norris</td>
    <td bgcolor="mrt"         cellpadding="8" width="100" align="center" style="color:#ffffff">Mr T</td>
    <td bgcolor="ninjaturtle" cellpadding="8" width="100" align="center" style="color:#ffffff">ninjaturtle</td>
  </tr>
  <tr>
    <td bgcolor="sick"  cellpadding="8" width="100" align="center">sick</td>
    <td bgcolor="crap"  cellpadding="8" width="100" align="center">crap</td>
    <td bgcolor="grass" cellpadding="8" width="100" align="center">grass</td>
  </tr>
</table>

This also answers the other part of the question; why does bgcolor="chucknorr" produce a yellow colour? Well, if we apply the rules, the string is:

c00c00000 => c00 c00 000 => c0 c0 00 [RGB(192, 192, 0)]

Which gives a light yellow gold colour. As the string starts off as 9 characters, we keep the second C this time around hence it ends up in the final colour value.

I originally encountered this when someone pointed out you could do color="crap" and, well, it comes out brown.

share|improve this answer
351  
I first encountered this when playing with HTML color attributes on Neopets a decade ago... imagine my childlike delight when entering the species of one of my pets as a color returned the very same color that pet was :) – BoltClock Feb 28 '12 at 20:31
96  
Note that, despite what that blog post says, when you get to handling 3-char strings, you duplicate each character, rather than prepending 0. i.e. 0F6 becomes #00FF66, not #000F06. – Aaron Dufour Feb 5 '13 at 20:32
499  
@usr: HTML is built around intentionally ignoring malformed input ;) – Kevin Ballard Feb 5 '13 at 23:43
65  
It's really amazing how appropriate a lot of these colors are. It's like HTML gematria or something. – Michael Jul 23 '14 at 19:12
40  
Neat coincidence: "grass" is green. – DanJAB Sep 15 '14 at 6:40

I'm sorry to disagree, but according to the rules for parsing a legacy color value posted by @Yuhong Bao, chucknorris DOES NOT equate to #CC0000, but rather to #C00000, a very similar but slightly different hue of red. I used the Firefox ColorZilla add-on to verify this.

The rules state:

  • make the string a length that is a multiple of 3 by adding 0s: chucknorris0
  • separate the string into 3 equal length strings: chuc knor ris0
  • truncate each string to 2 characters: ch kn ri
  • keep the hex values, and add 0's where necessary: C0 00 00

I was able to use these rules to correctly interpret the following strings:

  • LuckyCharms
  • Luck
  • LuckBeALady
  • LuckBeALadyTonight
  • GangnamStyle

Unfortunately I have not yet been able to determine why this doesn't seem to work for adamlevine which should be ADE0E0 but it's actually AD0E0E.


Edit:

adamlevine does work as per http://jsfiddle.net/LdyZ8/2959/ but the letters are blocked into ada00e000e which is padded to ada00e000e00 but then reduced down to the typical HEX 6 digit value of [ad]a0[0e]00[0e]00 thus making ad0e0e which appears in the jsfiddle above.


UPDATE: The original answerers who said the color was #CC0000 have since edited their answers to include the correction.

share|improve this answer
69  
I figured it out, I had misinterpreted some of the parsing instructions: "adamlevine" = "ada00e000e" = "ada00e000e00" = "ada0 0e00 0e00" = "ad 0e 0e" -- Perfect!! – Jeremy Goodell Oct 17 '12 at 18:11
13  
In case you're interested, I posted the 5-step algorithm as an UPDATE on a similar question I posted today: stackoverflow.com/questions/12939234/… – Jeremy Goodell Oct 17 '12 at 18:50
13  
@TimPietrusky created this freaking incredible demo tool for random color names. Just go here: randomstringtocsscolor.com and click in the box and type "chucknorris". – Jeremy Goodell Feb 6 '14 at 19:01
15  
much more interesting would be one where I enter an RGB color and you emit a list of nouns that result in said color. – Mooing Duck Apr 27 '15 at 18:55
3  
adamlevine does work as per jsfiddle.net/LdyZ8/2959 but the letters are blocked into ada00e000e which is padded to ada00e000e00 but then reduced down to the typical HEX 6 digit value of [ad]a0[0e]00[0e]00 thus making ad0e0e which appears in the jsfiddle above. – Martin Jan 26 '16 at 22:26

Most browsers will simply ignore any NON-hex values in your color string, substituting non-hex digits with zeros.

ChuCknorris translates to c00c0000000. At this point, the browser will divide the string into three equal sections, indicating Red, Green and Blue values: c00c 0000 0000. Extra bits in each section will be ignored, which makes the final result #c00000 which is a reddish color.

Note, this does not apply to CSS color parsing, which follow the CSS standard.

<p><font color='chucknorris'>Redish</font></p>
<p><font color='#c00000'>Same as above</font></p>
<p><span style="color: chucknorris">Black</span></p>

share|improve this answer
6  
Though I'm still curious as to why OP said "in CSS" and not "in HTML" - Maybe they're using a super old browser, or just mistaken? – Mike Christensen Nov 29 '11 at 23:19
5  
So then he is more than likely using the deprecated bgcolor attribute. – animuson Nov 29 '11 at 23:20
10  
Perhaps he will be so kind as to post this magical HTML that produces a Chuck Norris red across "all browsers and platforms." – Mike Christensen Nov 29 '11 at 23:22
10  
Invalid characters are not skipped, they are treated as 0. – phyzome Sep 28 '12 at 3:30
    
(WHile this answer may be dead) suggestion: Use background color instead to demonstrate teh colors. Text color is over such a small relative area it is hard to see differences or similarities – LunarWatcher Jun 7 at 11:37

The WHATWG HTML spec has the exact algorithm for parsing a legacy color value: https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing-a-legacy-colour-value

The code Netscape Classic used for parsing color strings is open source: https://dxr.mozilla.org/classic/source/lib/layout/layimage.c#155

For example, notice that each character is parsed as a hex digit and then is shifted into a 32-bit integer without checking for overflow. Only eight hex digits fit into a 32-bit integer, which is why only the last 8 characters are considered. After parsing the hex digits into 32-bit integers, they are then truncated into 8-bit integers by dividing them by 16 until they fit into 8-bit, which is why leading zeros are ignored.

Update: this code does not exactly match what is defined in the spec, but the only difference there is a few lines of code. I think it is these lines that was added (in Netscape 4):

if (bytes_per_val > 4)
{
      bytes_per_val = 4;
}
share|improve this answer

The browser is trying to convert chucknorris into hex colour code, because it's not a valid value.

  1. In chucknorris, everything except c is not a valid hex value.
  2. So it gets converted to c00c00000000.
  3. Which becomes #c00000, a shade of red.

This seems to be an issue primarily with Internet Explorer and Opera (12) as both Chrome (31) and Firefox (26) just ignore this.

P.S. The numbers in brackets are the browser versions I tested on.

.

On a lighter note

Chuck Norris doesn't conform to web standards. Web standards conform to him. #BADA55

share|improve this answer
77  
Didn't downvote, but it seems like this answer is already very well covered in the accepted answer and doesn't really contribute anything beyond that. – KyleMit Nov 30 '13 at 21:55
10  
The answer is essentially right. I don't get it why eight people had to downvote this. – Renan Jan 27 '14 at 14:36
11  
@ColeJohnson shouldn't that just be a suggested edit, then? – Ash Menon May 9 '15 at 9:29
7  
This is ridiculous. The existing answers already cover the subject good enough and are not actually that long. – Léo Lam Dec 18 '15 at 21:08
5  
@LéoLam But those answers don't have lighter notes to make us laugh. Also, #BADA55 is such a nice color. jsfiddle.net/rBpVD/8302 – Vael Victus May 27 '16 at 17:40

Answer:

  • The browser will try to convert chucknorris into a hexadecimal value.
  • Since c is the only valid hex character in chucknorris, the value turns into: c00c00000000(0 for all values that were invalid).
  • The browser then divides the result into 3 groupds: Red = c00c, Green = 0000, Blue = 0000.
  • Since valid hex values for html backgrounds only contain 2 digits for each color type (r, g, b), the last 2 digits are truncated from each group, leaving an rgb value of c00000 which is a brick-reddish toned color.

Legend has it:

  • Chucknorris leaves blood-red backgrounds where ever he goes, including websites.
share|improve this answer
17  
a downvote only because of a joke at the end? War on fun at it's finest. Here, take my upvote. – thepanuto Jun 7 '16 at 13:58
12  
One up-vote for the inevitable joke. – AntonK Jun 8 '16 at 2:02
8  
It makes me smile to see there are smart people in the world with a fun sense of humor :) – Webeng Jun 8 '16 at 4:38
17  
It's 2016 people. Jokes offend people. Evolution of the human species. – Redhart Aug 15 '16 at 13:47
10  
The downvotes are not due to the joke. The answer is not useful, because it is a repetition of previous answers. Similar comments were also posted previously, see e.g. aWebDeveloper's answer. – Aziraphale Sep 15 '16 at 13:40

The reason is the browser can not understand it and try to somehow translate it to what it can understand, in this case into a hexadecimal value!

chucknorris starts with c which is recognised character in hexadecimal, also it's converting all unrecognised characters into 0!

So chucknorris in hexadecimal format becomes: c00c00000000, all other characters become 0 and c remains where they are...

Now they get divided by 3 for RGB(red, green, blue)... R: c00c, G: 0000, B:0000...

But we know valid hexadecimal for RGB is just 2 characters, means R: c0, G: 00, B:00

So the real result is:

bgcolor="#c00000";

I also did the steps in the image as a quick reference:

enter image description here

share|improve this answer
    
I suppose the graphic does present some added value, even though I personally find emojis a bit tacky. +1. – Dragomok yesterday

protected by BoltClock Feb 6 '13 at 14:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.