0 votes
1 view
in Web Technology by (14.7k points)

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?

1 Answer

0 votes
by (33.1k points)

The reason why HTML think “chucknorris” is a color because it starts with c, and the browser reads it into a hexadecimal value.

We know that A, B, C, D, E, and F are characters in hexadecimal.

So the browser converts chucknorris to a hexadecimal value, C00C00000000. Then that hexadecimal value is converted to RGB format (after dividing by 3).

C00C00000000 => R:C00C, G:0000, B:0000

For indicating the color the browser needs only two digits to indicate the colour:-

R:C00C, G:0000, B:0000 => R:C0, G:00, B:00 => C00000

At last, it shows bgcolor = C00000 in the web browser.