How can I use 3-digit color codes rather than 6-digit color codes in CSS?

后端 未结 9 1544
余生分开走
余生分开走 2020-12-02 12:22

I recently went through my CSS file and switched all my six-digit hexadecimal codes to simple three-digit codes (for example, my #FDFEFF got shortened to

相关标签:
9条回答
  • 2020-12-02 12:37

    It is not possible. Please go through how the hexadecimal color code works. For a few color codes, we can reduce it to three digits, however, for the many hexadecimal color codes we cannot turn that down to three digits. Please check the below links for the further clarification.

    • CSS 3 Digit Hex Colors
    • CSS 6 Digit Hex Colors
    0 讨论(0)
  • 2020-12-02 12:40

    I always use the shorthand. The best advantage is that I can easily remember the codes.

    You still have 163 = 4,096 colors to choose from, should be enough.

    However if you save 300 bytes in shorthand color codes it means you have 100 colors decleared in your CSS. Unless your page is very diverse, or all rainbows and flowers it seems like a lot. You might be good at systematic CSS, but I often see unneccesary css rules. EX: if you're setting the same rule to many child elements that could have been replaced with setting the rule on the grandparent and in one exception element instead.

    0 讨论(0)
  • 2020-12-02 12:49

    If you use this in a table in IE 7 8 or 9 (unfortunately this is relevant as of the date of this response)

    http://www.w3schools.com/html/tryit.asp?filename=tryhtml_tables

    6 digit codes work fine but 3 digit codes render as black

    <table border="1" bgcolor="#ff0000">  vs.    <table border="1" bgcolor="#ff0">
    
    0 讨论(0)
提交回复
热议问题