# 可以使用 CSS 变量来普遍设置内联样式吗？

**URL:** <https://meta.discourse.org/t/could-inline-styles-be-generally-set-with-css-variables/368612>\
**Category:** Development\
**Created:** [2025年六月2日 22:05 UTC](https://meta.discourse.org/t/could-inline-styles-be-generally-set-with-css-variables/368612 "2025-06-02T22:05:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manuel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/manuel/32/468169_2.png) [@manuel](https://meta.discourse.org/u/manuel)\
**Post date:** [2025年六月2日 22:05 UTC](https://meta.discourse.org/t/could-inline-styles-be-generally-set-with-css-variables/368612/1 "2025-06-02T22:05:00Z")

</div>

内联样式有时使用 CSS 变量设置，但有时直接为属性声明。我认为如果 CSS 变量的模式被普遍采用会很好。这似乎完全向后兼容？

例如，像类别横幅上的这样：

 ![Screenshot 2025-06-02 at 22.53.12](https://global.discourse-cdn.com/meta/original/4X/e/0/9/e09ddb5240baebc9f16d5723a240aec0d552cb40.png)

---

<div class="post-metadata">

**Author:** ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)\
**Post date:** [2025年六月26日 20:10 UTC](https://meta.discourse.org/t/could-inline-styles-be-generally-set-with-css-variables/368612/2 "2025-06-26T20:10:15Z")

</div>

当然，我们通常希望在更多地方使用 CSS 变量。如果您知道其他目前没有 CSS 变量但使用起来会很方便的地方，请告诉我们！

这使用了 `--category-banner-background` 和 `--category-banner-color`

> <https://github.com/discourse/discourse-category-banners/pull/90>
>
> Requested here: https://meta.discourse.org/t/could-inline-styles-be-generally-se…t-with-css-variables/368612
> 
> This replaces the inline colors with variables that are assigned the colors. The variables are then applied to the \`div\[class^="category-title-header"\]\`. This shouldn't result in any changes other than how the colors are set, overrides in themes will still apply.

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2025年七月26日 20:10 UTC](https://meta.discourse.org/t/could-inline-styles-be-generally-set-with-css-variables/368612/3 "2025-07-26T20:10:48Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
