简体   繁体   English

用特定背景色做div的最佳方法?

[英]Best way to do a div with specific background-color?

I need to create a div with specific background-color depending on variable object. 我需要根据变量对象创建具有特定背景颜色的div。

My options are: 我的选择是:

  1. Create an infinite css for each color. 为每种颜色创建一个无限的CSS。
  2. Style in-line. 内联样式。

I know style should be separated from html. 我知道样式应该与html分开。 But, in this case, what is more efficient? 但是,在这种情况下,什么效率更高?

Do you need a different color based on dynamic content ("variable object")? 您是否需要基于动态内容的其他颜色(“可变对象”)? In that case you should go for an inline-style. 在这种情况下,您应该采用内联样式。 It would be easy for php/whatever backend you have to serve it conditionally. 对于php /任何后端,您必须有条件地提供服务将很容易。

While it's best to keep most styles on a stylesheet, there are exceptions to that rule. 虽然最好将大多数样式保留在样式表中,但该规则也有例外。 If your particular case asks for it, feel free to add your styles inline. 如果您的特殊情况需要,请随时内联添加样式。 For example, you need a gallery of things and you need the elements with a background-image instead of an img. 例如,您需要一个画廊,并且您需要具有背景图像而不是img的元素。

No, you cannot . 不,你不能 The options you provided are the only way... You can do a complex/advance JavaScript / jQuery but it will always boil down to on how you will provide the selectors . 您提供的选项是唯一的方法...您可以执行复杂/高级的JavaScript / jQuery但始终取决于您如何提供selectors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM