Are there any good reasons for using hex over decimal for RGB colour values in CSS?

前端 未结 12 2675
庸人自扰
庸人自扰 2020-12-01 07:17

rgb(255,255,255) notation has been available since CSS1. But #ffffff seems to be vastly more popular.

Obviously it\'s slightly more compact

相关标签:
12条回答
  • 2020-12-01 07:53

    I think it's what you're used to. If you're used to HTML, you'll probably use HEX since it's just been used a lot in HTML. If you're from a design background, using Photoshop/Corel/PaintShopPro etc., then you're likely used to the RGB notation - though, a lot of programs these days incorporate a HEX value field too.

    As said, RGBA might be a reason to just go with the RGB notation - consistency.

    Though, I think it also depends on the scenario. If you're comfortable with both, you might just switch between them: #fff is a lot easier to type than rgb(255,255,255).

    Another question is why people will say #fff instead of White (assuming most browsers support this keyword).

    It's all a matter of preference and legibility - if you're maintaining a huge CSS file, being able to look at the colour value and know what colour it is, is a really good advantage. Even more advantageous is using something like LESS or Sass to add a kind of programmability to CSS - allowing constants for example. So instead of saying:

    #title { color: #abcdef; }
    

    You might instead do the following with LESS:

    @base-color: #abcdef;
    
    #title { color: @base-color; }
    

    Maintaining the CSS becomes less of an issue.

    If you're worried about the performance of the browser rendering it's result, then that could also be another factor to your choice.

    So in summary, it boils down to:

    • Familiarity
    • Preference
    • Maintainability
    • Performance
    0 讨论(0)
  • 2020-12-01 07:53

    The main reason is probably compactness, as you mentioned. #ffffff can even be further shortened to the #fff shorthand notation.

    Another possible reason is that there's a perceived performance increase by saving the browser the trouble of converting the rgb notation.

    0 讨论(0)
  • 2020-12-01 07:53

    no valid reason, other than personal preference.

    0 讨论(0)
  • 2020-12-01 07:54

    Probably a touch of speed when the color is interpreted by a browser. Otherwise some people from design background may know how to compose colors from RGB components when they write code, and some others from programming background are probably more inclined to use HEX values.

    0 讨论(0)
  • 2020-12-01 07:56

    Traditionally HTML has always used hex colours, so that has carried forward into CSS. Think <font color="#ffffff">

    0 讨论(0)
  • 2020-12-01 07:58

    CSS was invented by software developers, not designers. Software developers live and breathe hex. From my old C64 days, I can still read most hex numbers without thinking. A9, anyone?

    0 讨论(0)
提交回复
热议问题