简体   繁体   English

我可以制作一个我可以在CSS,JS和HTML之间共享的自定义颜色定义吗?

[英]Can I make a custom colour definitions that I can share between CSS, JS and HTML?

I have a blue-ish colour that I want to use in many places in my app and at the moment I am copying and pasting it between styles in my CSS. 我有一个蓝色的颜色,我想在我的应用程序的许多地方使用,目前我正在我的CSS中的样式之间复制和粘贴它。 Is there a way of defining a constant like the standard colours, 'blue', 'red' etc. that I could share between my CSS, my HTML and my JS? 有没有一种方法可以定义一个常量,如标准颜色,'蓝色','红色'等,我可以在我的CSS,我的HTML和我的JS之间分享?

I'd like to be able to say (somewhere, preferably CSS) 我想能够说(某处,最好是CSS)

myblue = #33CC99

in CSS say... 在CSS说...

background-color:myblue;

in HTML say... 在HTML中说...

<td color="myblue"/>

and in JavaScript 并在JavaScript中

tag.style.backgroundColor = myblue;

I'm guessing this is impossible and google turned nothing up, so has anyone got any ideas? 我猜这是不可能的,谷歌什么都没有,所以有没有人有任何想法? I doubt I am the only person to come across this. 我怀疑我是唯一遇到这种情况的人。

A very promising product that "compiles" higher-level expressions like variables into CSS is LESS . 一个非常有前途的产品,将变量等高级表达式“编译”到CSS中是很少的 It needs Ruby. 它需要Ruby。 I haven't used it yet but it's definitely worth a look. 我还没用过,但绝对值得一看。

A more primitive way to do this would be using a server-side scripting language like PHP. 更原始的方法是使用像PHP这样的服务器端脚本语言。

You would define constants in PHP like so: 您可以在PHP中定义常量,如下所示:

define ("MYBLUE", "#33CC99");

and then outputting the value where needed using <?=MYBLUE;?> 然后使用<?=MYBLUE;?>输出所需的值

Big downside: To do this in external css and js files, you would obviously have to have them parsed by the PHP interpreter, which is not good performance wise if you have a lot of visitors. 大缺点:要在外部css和js文件中执行此操作,您显然必须由PHP解释器解析它们,如果您有大量访问者,这不是很好的性能。 For a low-traffic site, it probably doesn't matter. 对于低流量站点,它可能无关紧要。

Yes, this is impossible. 是的,这是不可能的。 You could, however, write your own CSS preprocessor (or use one of the existing ones out there), for instance with PHP. 但是,您可以编写自己的CSS预处理器(或使用其中一个现有的预处理器),例如使用PHP。 The big downside is that you would have to output the colorcode on your whole site with PHP and your scripts would look like 最大的缺点是您必须使用PHP在整个站点上输出颜色代码,并且您的脚本看起来像

tag.style.backgroundColor = <? echo $myblue; ?>

and likewise in CSS 同样在CSS中

.someClass {
  background-color: <? echo $myblue ?>
}

or something similar. 或类似的东西。 And that isn't really nice either. 这也不是很好。 Of course you could use any server sided script language of your choice. 当然,您可以使用您选择的任何服务器端脚本语言。 As far as I can judge, this is the only possibility to use a color-constant throughout a whole website. 据我所知,这是在整个网站中使用颜色常数的唯一可能性。

You could have a look at some processors: 你可以看看一些处理器:

You may look at HAML + SASS . 您可以查看HAML + SASS Though you cannot define variables for all three languages at once, these two can make writing HTML+CSS much easier. 虽然您不能同时为所有三种语言定义变量,但这两种语言可以使编写HTML + CSS变得更加容易。

How I would approach this is to make a class in my CSS. 我如何处理这个问题就是在我的CSS中创建一个类。

.color_class {color: #33CC99;}

Then call it in my HTML 然后在我的HTML中调用它

<td class="color_class" />

You can assign multiple classes to an HTML element. 您可以为HTML元素分配多个类。

In the JS, just name the class 在JS中,只需命名该类

document.getElementById('id').className = 'color_class';

Of course you can play with how you want to select your element. 当然,你可以玩你想要如何选择你的元素。 A JS library probably has even easier methods for assigning CSS classes. JS库可能有更简单的方法来分配CSS类。

To achieve this with out using any dynamic CSS (eg serving a PHP file with content-type text/css ), your best bet is to separate out the places where you define the 'theme'. 要在不使用任何动态CSS的情况下实现这一点(例如,使用内容类型text/css提供PHP文件),最好的办法是将您定义“主题”的地方分开。

<script type="text/javascript">
   var theme = '#003366';
 </script>

Then you can use a JavaScript file to write out styles based on the themes. 然后,您可以使用JavaScript文件根据主题编写样式。

However, if you are able to use a CSS pre-processor, go with that. 但是,如果您能够使用CSS预处理器,请使用它。 You'll have much more flexibility and the code will be easier to maintain. 您将拥有更大的灵活性,代码将更易于维护。 I almost always use a PHP or JSP page for CSS. 我几乎总是使用PHP的PHP或JSP页面。

As other users have noted you can't do this in straight HTML, JS or CSS unless you pass all HTML, JS and CSS content via a CGI/PHP/ASP script - which isn't actually a bad approach as it's easy to maintain. 正如其他用户已经注意到的那样,除非你通过CGI / PHP / ASP脚本传递所有HTML,JS和CSS内容,否则你不能用直接的HTML,JS或CSS做到这一点 - 这实际上并不是一个糟糕的方法,因为它很容易维护。

If you use a query string in the reference to included CSS / JS files - eg '/css/stylesheet.php?timestamp=2010-01-01+00:00:00', then almost all clients will aggressively cache your CSS/JS files, negating any impact on load parsing them in a scripting language may have (though unless the site is likely to be particularly busy I wouldn't be too connected about that). 如果你在包含CSS / JS文件的引用中使用查询字符串 - 例如'/css/stylesheet.php?timestamp=2010-01-01+00:00:00',那么几乎所有客户端都会积极地缓存你的CSS / JS文件,否定对用脚本语言解析它们的负载的任何影响可能有(虽然除非站点可能特别繁忙,否则我不会太过关联)。

If you are using Apache (which is likely), an alternative approach would be do use something like mod_set to re-write all HTML, JS and CSS files on the fly for you. 如果您正在使用Apache(很可能),另一种方法是使用类似mod_set的东西来为您重新编写所有HTML,JS和CSS文件。 This may be more difficult to support if you are not familiar with configuring Apache (or are using another web server). 如果您不熟悉配置Apache(或正在使用其他Web服务器),则可能更难以支持。

With regard to tag naming: 关于标签命名:

With either approach I strong suggest using a clear tagging system to denote your dynamic variables (eg %MyBlue%) and to consider having variables names be representative (eg %HeadingBackgroundColor%, %FontColor%, even if both are set to %MyBlue%), as that can prevent things getting hairy later on (when you discover that changing one value has intended consequences). 无论采用哪种方法,我强烈建议使用清晰的标记系统来表示您的动态变量(例如%MyBlue%)并考虑使变量名称具有代表性(例如%HeadingBackgroundColor%,%FontColor%,即使两者都设置为%MyBlue%)因为这可以防止事情在以后变得毛茸茸(当你发现改变一个值有预期后果时)。

Using more representative names may seem unnecessarily verbose at first glance, but it causes problems in many cases because colours end up clash in unintended ways when they are significantly different from the original scheme (this is true of a lot of mainstream software which is skinnable - because the author made the assumption that %value1% and %value2% would always go together and so would %value1% and %value3% - meaning in effect the options for themeing are severely limited by an unintended dependancy). 使用更具代表性的名称乍一看似乎不必要的冗长,但在许多情况下会导致问题,因为当颜色与原始方案明显不同时,颜色最终会以意想不到的方式发生冲突(许多主流软件都可以使用 - 因为作者假设%value1%和%value2%总是在一起,所以%value1%和%value3% - 这意味着主题的选项受到非预期的依赖性的严重限制。

我是在构建时通过Freemarker运行我的CSS文件来完成的。

I am not sure of your end goal. 我不确定你的最终目标。 If it is to allow selection of one of several themes for a web page, you could simply have multiple CSS files, and a cookie/session variable/database store of the user's prefered style sheet. 如果要允许为网页选择几个主题之一,您可以简单地拥有多个CSS文件,以及用户首选样式表的cookie /会话变量/数据库存储。 Then you could just do 那你就可以做

 <link rel=<? echo stylepref; ?> type='text/css'>

or something along those lines 或类似的规定

If you want to be able to completely customize the site, then one of the above answers would be needed. 如果您希望能够完全自定义网站,则需要上述答案之一。

在纯客户端CSS分组选择器允许您在许多不同的元素上放置相同的颜色:

p, .red, #sub, div a:link { color: #f00; }

There's no concept of 'variables' in CSS, but I'd strongly recommend not doing what you're doing. CSS中没有“变量”的概念,但我强烈建议不要做你正在做的事情。 there's no good reason that you can't define all your style information in your CSS sheet as CSS classes. 没有充分的理由不能将CSS表格中的所有样式信息定义为CSS类。 If you do that, and then just apply said classes in html and javascript, that's one major load off in terms of copying and pasting. 如果你这样做,然后只是在html和javascript中应用所述类,那么在复制和粘贴方面这是一个主要的负担。

Second, remember that you can define more than one CSS class for an element, eg 其次,请记住,您可以为元素定义多个CSS类,例如

<div class='blue-text white-background'>my content</div>

You can then define those independantly in CSS, a la: 然后你可以在CSS中独立定义那些,la:

.blue-text { color : Blue; }
.white-background { background-color: white;}

and you can even create rules that only take effect when both are applyed: 您甚至可以创建仅在应用两者时生效的规则:

.blue-text.white-background { color : AliceBlue; }

If you want to have the color choices dynamically generated, the only real way to do that is as others have suggested, which is either preprocess your files before deployment, or have the CSS dynamically generated and served by your language of choice. 如果您想要动态生成颜色选择,唯一真正的方法就像其他人建议的那样,既可以在部署之前预处理文件,也可以使用您选择的语言动态生成和提供CSS。

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

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