Running a page and setting the background color value to an arbitrary string such as "chucknorris"
the background turns red. Why does this happen?
<body bgcolor="chucknorris">test</body>
Original question: Why does HTML think “chucknorris” is a color?
Reference: Why does HTML think “chucknorris” is a color?
If you have invalid characters in a color code that is written in hexadecimal notation, these characters are treated as 0. A hexadecimal number can contain the characters
[0-9a-f]
, any other characters found are replaced with0
.For
chucknorris
:chucknorris
→c00c0000000
0
until the number of characters is divisible by 3:c00c0000000
(11) →c00c 0000 0000
(12)rgb(c00c, 0000, 0000)
rgb(c0, 00, 00)
rgb(192, 0, 0) [ == #C00000]
The resulting color: red.
Other similar colors:
crap
,ninjaturtle
,sick
,grass
.It's a holdover from the days of Netscape:
As of this blog post , which covers it in great detail, including different lengths of color values, etc.
If we apply the rules mentioned in the blog post, we get the following:
Replace all invalid hex characters with 0's
Fill in the following total number of characters divisible by 3 (11 -> 12)
Divide into three equal groups, with each component representing the corresponding color component of an RGB color:
Truncate each of the arguments from the right to 2 characters
We get the following result:
Here's an example that demonstrates the bgcolor attribute in action, to produce this "incredible" color swatch:
This also answers the other part of the question; why does bgcolor = "chucknorr" produce a yellow color? Well, if we apply the rules, the value of the string is:
Which gives a light golden yellow color. Since the string starts with 9 characters, we keep the second C this time, so it ends at the final color value.
I originally came across this when someone pointed out that you could do color="garbage" and, well, you get brown.
from @dash's original answer on SOen .