Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does HTML think “chucknorris” is a color?

Why do certain random strings produce colors when entered as background colors in HTML?

For example, the following code produces a page with a red background across all browsers and platforms:

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

On the other hand, the value chucknorr produces a yellow background!

What’s going on here?

like image 745
user456584 Avatar asked Nov 29 '11 22:11

user456584


People also ask

What colors does HTML recognize?

HTML recognizes the following 16 color names: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow. In addition to these HTML-defined colors, Microsoft Internet Explorer and Netscape Navigator recognize 140 standard color names.

Does HTML use RGB?

HTML colors are specified with predefined color names, or with RGB, HEX, HSL, RGBA, or HSLA values.

Is there a color tag in HTML?

Colors are very important to give a good look and feel to your website. You can specify colors on page level using <body> tag or you can set colors for individual tags using bgcolor attribute.

What does purple mean in HTML?

As an HTML color code, purple is represented as #800080, which is hexadecimal for 128 red, 0 green, and 128 blue. Tip. If a visited link color is not defined, a browser defaults the color to purple.


2 Answers

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, finally, 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 that you could do color="crap" and, well, it comes out brown.

like image 155
dash Avatar answered Sep 17 '22 23:09

dash


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

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

like image 44
Jeremy Goodell Avatar answered Sep 21 '22 23:09

Jeremy Goodell