简体   繁体   中英

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

I need to create a div with specific background-color depending on variable object.

My options are:

  1. Create an infinite css for each color.
  2. Style in-line.

I know style should be separated from 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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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