Why do certain random strings produce colors when entered as background colors in HTML?
For example, bgcolor="chucknorris" produces a red background:
<body bgcolor="chucknorris"> test </body>
Conversely, bgcolor="chucknorr" produces a yellow background:
<body bgcolor="chucknorr"> test </body>
This holds true across various browsers and platforms. What’s going on here?
It’s a holdover from the Netscape days:
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:
Replace all nonvalid hexadecimal characters with 0’s:
Pad out to the next total number of characters divisible by 3 (11 → 12):
Split into three equal groups, with each component representing the corresponding colour component of an RGB colour:
Truncate each of the arguments from the right down to two characters.
Which, finally, gives the following result:
Here’s an example demonstrating the
bgcolorattribute in action, to produce this “amazing” colour swatch: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: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 that you could do
color="crap"and, well, it comes out brown.