Why does HTML think “chucknorris” is a color?

后端 未结 9 2171
醉梦人生
醉梦人生 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:37

    The reason is the browser can not understand it and try to somehow translate it to what it can understand and in this case into a hexadecimal value!...

    chucknorris starts with c which is recognised character in hexadecimal, also it's converting all unrecognised characters into 0!

    So chucknorris in hexadecimal format becomes: c00c00000000, all other characters become 0 and c remains where they are...

    Now they get divided by 3 for RGB(red, green, blue)... R: c00c, G: 0000, B:0000...

    But we know valid hexadecimal for RGB is just 2 characters, means R: c0, G: 00, B:00

    So the real result is:

    bgcolor="#c00000";
    

    I also added the steps in the image as a quick reference for you:

提交回复
热议问题