简体   繁体   中英

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. 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?

I'd like to be able to say (somewhere, preferably CSS)

myblue = #33CC99

in CSS say...

background-color:myblue;

in HTML say...

<td color="myblue"/>

and in 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 . It needs 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.

You would define constants in PHP like so:

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

and then outputting the value where needed using <?=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. 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. The big downside is that you would have to output the colorcode on your whole site with PHP and your scripts would look like

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

and likewise in 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 . Though you cannot define variables for all three languages at once, these two can make writing HTML+CSS much easier.

How I would approach this is to make a class in my CSS.

.color_class {color: #33CC99;}

Then call it in my HTML

<td class="color_class" />

You can assign multiple classes to an HTML element.

In the JS, just name the class

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.

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'.

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

Then you can use a JavaScript file to write out styles based on the themes.

However, if you are able to use a CSS pre-processor, go with that. 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.

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.

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).

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. This may be more difficult to support if you are not familiar with configuring Apache (or are using another web server).

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).

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).

我是在构建时通过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. 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. there's no good reason that you can't define all your style information in your CSS sheet as CSS classes. 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.

Second, remember that you can define more than one CSS class for an element, eg

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

You can then define those independantly in CSS, a 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.

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