如何从代码隐藏中更改我的css文件中使用的颜色?

时间:2021-10-02 18:56:33

Many of my css files (including bootstrap) use specified colors. I want to be able to change the main colors in one central place (such as the config pages I'm adding).

我的许多css文件(包括bootstrap)都使用指定的颜色。我希望能够在一个中心位置更改主要颜色(例如我正在添加的配置页面)。

I have seen that css var's might be a thing soon but are currently not well supported.

我已经看到css var可能很快就会出现问题,但目前还没有得到很好的支持。

Is there a more elegant way I can do this that my current idea of using the css files as templates which I will load with the color config on page load?

有没有更优雅的方式,我可以这样做,我目前的想法使用css文件作为模板,我将加载页面加载的颜色配置?

1 个解决方案

#1


2  

You could use LESS instead of CSS to have a variables and much more advantages.

您可以使用LESS而不是CSS来获得变量和更多优势。

If you combine using LESS with themes separated to different CSS files (as @Thorarins suggested), you'll get more powerful and maintainable.

如果您将LESS与分隔到不同CSS文件的主题结合使用(如@Thorarins建议的那样),您将获得更强大和可维护性。

LESS features:

较少的功能:

1) Variables

1)变量

LESS

@link-color: #CC0000;

a {
    color: @link-color;
}

"compiles" to

“编译”到

CSS

a {
    color: #CC0000;
}

2) Nesting

2)嵌套

LESS

.article {
   a {
     text-decoration: underline;
   }

   p {
     margin: .5ex .5em;
   }
}

"compiles" to

“编译”到

CSS

.article a {
  text-decoration: underline;
}

.article p {
  margin: .5ex .5em;
}

3) Mixins

3)Mixins

LESS

.box-shadow(@style, @color)
{
  -webkit-box-shadow: @style @color;
  box-shadow:         @style @color;
}

div
{
  .box-shadow(0 0 5px, 30%);
}

"compiles" to

“编译”到

CSS

div {
  -webkit-box-shadow: 0 0 5px 30%;
  box-shadow: 0 0 5px 30%;
}

4) Functions

4)功能

LESS

@text-color: #000;

.article {
   p {
     color: lighten(@text-color);
   }
}

"compiles" to

“编译”到

CSS

.article p {
  color: #4d4d4d;
}

#1


2  

You could use LESS instead of CSS to have a variables and much more advantages.

您可以使用LESS而不是CSS来获得变量和更多优势。

If you combine using LESS with themes separated to different CSS files (as @Thorarins suggested), you'll get more powerful and maintainable.

如果您将LESS与分隔到不同CSS文件的主题结合使用(如@Thorarins建议的那样),您将获得更强大和可维护性。

LESS features:

较少的功能:

1) Variables

1)变量

LESS

@link-color: #CC0000;

a {
    color: @link-color;
}

"compiles" to

“编译”到

CSS

a {
    color: #CC0000;
}

2) Nesting

2)嵌套

LESS

.article {
   a {
     text-decoration: underline;
   }

   p {
     margin: .5ex .5em;
   }
}

"compiles" to

“编译”到

CSS

.article a {
  text-decoration: underline;
}

.article p {
  margin: .5ex .5em;
}

3) Mixins

3)Mixins

LESS

.box-shadow(@style, @color)
{
  -webkit-box-shadow: @style @color;
  box-shadow:         @style @color;
}

div
{
  .box-shadow(0 0 5px, 30%);
}

"compiles" to

“编译”到

CSS

div {
  -webkit-box-shadow: 0 0 5px 30%;
  box-shadow: 0 0 5px 30%;
}

4) Functions

4)功能

LESS

@text-color: #000;

.article {
   p {
     color: lighten(@text-color);
   }
}

"compiles" to

“编译”到

CSS

.article p {
  color: #4d4d4d;
}