Why does HTML accept color with random names/strings in the <body> tag?

Asked

Viewed 166 times

15

Some time ago I read somewhere about it, but I never understood how it works: Take the example with the word 'chucknorris': jsfiddle

Not only with the word 'chucknorris' but any random word always produces different results:

Does anyone have any explanation for this behavior?

  • 2
  • 3

    It must have something to do (see) with hexadecimal, because I realized that putting 3 random letters the result is always black for me, and the color projection only comes with some letter between [a, b, c, d, e, f]

1 answer

24


This is because all the missing characters are interpreted as 0. That is to say, #CC0000 is equal to #CCXXXX, the result will be the same, a red.

So if we do this with the word "Earendul" the result is EA0E0D00, which is then divided into 3 plots, being RGB(Red, Green, Blue). EA0E0D00 has 8 characters, to be divisible by 3 has to be 9. As I said earlier, when a character is missing an add 0. The end result would be: (EA0, E0D, 000)

That is, in the end just remove the excess, getting only 2 characters per plot:

RGB( 0xEA, 0xE0, 0x00) = #EAE000

See the example on jsfiddle

  • 2

    It really makes sense. I thought it might be something like this. Thanks for the answer.

  • 2

    I did not know this rsrs, it makes total sense, test a word that has no character #HEX, like putim, it interprets as if it were 00000 and the color turns black

  • 2

    Hello, @Rafael. Good your performance on the site! First answer and already has an Accept and 21 positive votes (22 now :), deservedly, for the detailed explanation). I made a small edit in your reply, taking care of the formatting. Learn more about the syntax of Sopt’s answers clicking here.

  • Thanks @Bacco, any improvement is always well accepted :D

  • 1

    @Rafaelmoraisdossantos We are trying to create the new website (Sopt’s brother) for IT matters that are not programming. Do the commit (do not forget to confirm the email). If you have the minimum number of interested, the site will be created. And if you can indicate (on Share This) to other people as well, it would be even cooler. http://area51.stackexchange.com/proposals/84282/super-user-em-portugues?referrer=iQLJgvwwKWiaSZMVw5WkVIw2. If you would like to know more or have a few questions, check out: http://meta.pt.stackoverflow.com/q/2482/101 Log in here to let me know that you have read it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.