Why does HTML think “chucknorris” is a color?

后端 未结 9 2041
醉梦人生
醉梦人生 2020-11-22 05:07

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

9条回答
  •  独厮守ぢ
    2020-11-22 05:31

    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.

提交回复
热议问题