<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="https://stackoverflow.com/questions/30170/avoiding-repeated-constants-in-css">Avoiding repeated constants in CSS</a>  </p>
</blockquote>



<p>We have some "theme colors" that are reused in our CSS sheet.</p>

<p>Is there a way to set a variable and then reuse it?</p>

<p>E.g.</p>

<pre><code>.css
OurColor: Blue

H1 { 
 color:OurColor;
}
</code></pre>

[{'answer_id': 47490, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': "<p>CSS doesn't offer any such thing. The only solution is to write a preprocessing script that is either run manually to produce static CSS output based on some dynamic pseudo-CSS, or that is hooked up to the web server and preprocesses the CSS prior to sending it to the client.</p>\n"}, {'answer_id': 47494, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 2, 'selected': False, 'text': '<p>Since CSS does not have that (yet, I believe the next version will), follow Konrad Rudolphs advice for preprocesing. You probably want to use one that allready exists: m4</p>\n\n<p><a href="http://www.gnu.org/software/m4/m4.html" rel="nofollow noreferrer">http://www.gnu.org/software/m4/m4.html</a></p>\n'}, {'answer_id': 47497, 'author': 'sblundy', 'author_id': 4893, 'author_profile': 'https://Stackoverflow.com/users/4893', 'pm_score': 3, 'selected': False, 'text': '<p>You\'re not the first to wonder and the answer is no. Elliotte has a nice rant on it: <a href="http://cafe.elharo.com/web/css-repeats-itself/" rel="noreferrer">http://cafe.elharo.com/web/css-repeats-itself/</a>. You could use JSP, or its equivalent, to generate the CSS at runtime.</p>\n'}, {'answer_id': 47505, 'author': 'Tim Sullivan', 'author_id': 722, 'author_profile': 'https://Stackoverflow.com/users/722', 'pm_score': 3, 'selected': False, 'text': '<p>No, but <a href="http://sass-lang.com" rel="nofollow noreferrer">Sass</a> does this. It\'s a CSS preprocessor, allowing you to use a lot of shortcuts to reduce the amount of CSS you need to write. </p>\n\n<p>For example:</p>\n\n<pre><code>$blue: #3bbfce;\n$margin: 16px;\n\n.content-navigation {\n  border-color: $blue;\n  color:\n    darken($blue, 9%);\n}\n\n.border {\n  padding: $margin / 2;\n  margin: $margin / 2;\n  border-color: $blue;\n}\n</code></pre>\n\n<p>Beyond variables, it provides the ability to nest selectors, keeping things logically grouped:</p>\n\n<pre><code>table.hl {\n  margin: 2em 0;\n  td.ln {\n    text-align: right;\n  }\n}\n\nli {\n  font: {\n    family: serif;\n    weight: bold;\n    size: 1.2em;\n  }\n}\n</code></pre>\n\n<p>There\'s more: mixins that act kind of like functions, and the ability to inherit one selector from another. It\'s very clever and very useful.</p>\n\n<p>If you\'re coding in Ruby on Rails, it\'ll even automatically compile it to CSS for you, but there\'s also a general purpose compiler that can do it for you on-demand.</p>\n'}, {'answer_id': 47508, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 8, 'selected': True, 'text': '<p>There\'s no requirement that all styles for a selector reside in a single rule, and a single rule can apply to multiple selectors... so <em>flip it around</em>:</p>\n\n<pre><code>/* Theme color: text */\nH1, P, TABLE, UL\n{ color: blue; }\n\n/* Theme color: emphasis */\nB, I, STRONG, EM\n{ color: #00006F; }\n\n/* ... */\n\n/* Theme font: header */\nH1, H2, H3, H4, H5, H6\n{ font-family: Comic Sans MS; }\n\n/* ... */\n\n/* H1-specific styles */\nH1\n{ \n   font-size: 2em; \n   margin-bottom: 1em;\n}\n</code></pre>\n\n<p>This way, you avoid repeating styles that are <em>conceptually</em> the same, while also making it clear which parts of the document they affect. </p>\n\n<p>Note the emphasis on "conceptually" in that last sentence... This just came up in the comments, so I\'m gonna expand on it a bit, since I\'ve seen people making this same mistake over and over again for years - predating even the existence of CSS: <strong>two attributes sharing the same value does not necessarily mean they represent the same <em>concept</em></strong>. The sky may appear red in the evening, and so do tomatoes - but the sky and the tomato are not red for the same reason, and their colors <em>will</em> vary over time independently. By the same token, just because you happen to have two elements in your stylesheet that are given the same color, or size or positioning does not mean they will <em>always</em> share these values. A naive designer who uses grouping (as described here) or a variable processor such as SASS or LESS to avoid <em>value</em> repetition risks making future changes to styling incredibly error-prone; always focus on the <em>contextual meaning</em> of styles when looking to reduce repetition, ignoring their <em>current values</em>. </p>\n'}, {'answer_id': 47513, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>You\'re making it too complicated. This is the reason the cascade exists. Simply provide your element selectors and class your color:</p>\n\n<pre><code>h1 {\n   color: #000;\n}\n.a-theme-color {\n   color: #333;\n}\n</code></pre>\n\n<p>Then apply it to the elements in the HTML, overriding when you need to use your theme colors.</p>\n\n<pre><code>&lt;h1&gt;This is my heading.&lt;/h1&gt;\n&lt;h1 class="a-theme-color"&gt;This is my theme heading.&lt;/h1&gt;\n</code></pre>\n'}, {'answer_id': 47528, 'author': 'Farinha', 'author_id': 2841, 'author_profile': 'https://Stackoverflow.com/users/2841', 'pm_score': 2, 'selected': False, 'text': '<p>That\'s not supported at the moment unless you use some script to produce the CSS based on some variables defined by you.</p>\n\n<p>It seems, though, that at least some people from the browser world are <a href="http://ajaxian.com/archives/css-variables-are-here" rel="nofollow noreferrer">working on it</a>.  So, if it really becomes a standard sometime in the future, then we\'ll have to wait until it is implemented in all the browsers (it will be unusable until then).</p>\n'}, {'answer_id': 47564, 'author': 'Sören Kuklau', 'author_id': 1600, 'author_profile': 'https://Stackoverflow.com/users/1600', 'pm_score': 1, 'selected': False, 'text': '<p>See also <a href="https://stackoverflow.com/questions/30170/avoiding-repeated-constants-in-css">Avoiding repeated constants in CSS</a>. As Farinha said, a CSS Variables proposal has been made, but for the time being, you want to use a preprocessor.</p>\n'}, {'answer_id': 47785, 'author': 'Simon Forrest', 'author_id': 4733, 'author_profile': 'https://Stackoverflow.com/users/4733', 'pm_score': 1, 'selected': False, 'text': '<p>You can use mutliple classes in the HTML element\'s class attribute, each providing part of the styling. So you could define your CSS as:</p>\n\n<pre><code>.ourColor { color: blue; }\n.ourBorder { border: 1px solid blue; }\n.bigText { font-size: 1.5em; }\n</code></pre>\n\n<p>and then combine the classes as required:</p>\n\n<pre><code>&lt;h1 class="ourColor"&gt;Blue Header&lt;/h1&gt;\n&lt;div class="ourColor bigText"&gt;Some big blue text.&lt;/div&gt;\n&lt;div class="ourColor ourBorder"&gt;Some blue text with blue border.&lt;/div&gt;\n</code></pre>\n\n<p>That allows you to reuse the ourColor class without having to define the colour mulitple times in your CSS. If you change the theme, simply change the rule for ourColour.</p>\n'}, {'answer_id': 47816, 'author': 'Matt', 'author_id': 2338, 'author_profile': 'https://Stackoverflow.com/users/2338', 'pm_score': 1, 'selected': False, 'text': '<p>This may sound like insanity, but if you are using NAnt (or Ant or some other automated build system), you can use NAnt properties as CSS variables in a hacky way. Start with a CSS template file (maybe styles.css.template or something) containing something like this:</p>\n\n<pre><code>a {\n    color: ${colors.blue};\n}\n\n    a:hover {\n        color: ${colors.blue.light};\n    }\n\np {\n    padding: ${padding.normal};\n}\n</code></pre>\n\n<p>And then add a step to your build that assigns all the property values (I use external buildfiles and &lt;include&gt; them) and uses the &lt;expandproperties&gt; filter to generate the actual CSS:</p>\n\n<pre><code>&lt;property name="colors.blue" value="#0066FF" /&gt;\n&lt;property name="colors.blue.light" value="#0099FF" /&gt;\n&lt;property name="padding.normal" value="0.5em" /&gt;\n\n&lt;copy file="styles.css.template" tofile="styles.css" overwrite="true"&gt;\n    &lt;filterchain&gt;\n        &lt;expandproperties/&gt;\n    &lt;/filterchain&gt;\n&lt;/copy&gt;\n</code></pre>\n\n<p>The downside, of course, is that you have to run the css generation target before you can check what it looks like in the browser.  And it probably would restrict you to generating all your css by hand.</p>\n\n<p>However, you can write NAnt functions to do all sorts of cool things beyond just property expansion (like generating gradient image files dynamically), so for me it\'s been worth the headaches.</p>\n'}, {'answer_id': 69558, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>CSS does not (yet) employ variables, which is understandable for its age and it being a declarative language.</p>\n\n<p>Here are two major approaches to achieve more dynamic style handling:</p>\n\n<ul>\n<li>Server-side variables in inline css<br>\nExample (using PHP):  \n\n<blockquote>\n  <p><code>&lt;style&gt; .myclass{color:&lt;?php echo $color; ?&gt;;} &lt;/style&gt;</code></p>\n</blockquote></li>\n</ul>\n\n<p>&nbsp;</p>\n\n<ul>\n<li><p>DOM manipulation with javascript to change css client-side<br>\nExamples (using jQuery library):</p>\n\n<blockquote>\n  <p><code>$('.myclass').css('color', 'blue');</code>  </p>\n  \n  <p>OR</p>\n  \n  <p><code>//The jsvarColor could be set with the original page response javascript<br>\n  // in the DOM or retrieved on demand (AJAX) based on user action.\n  $('.myclass').css('color', jsvarColor);</code></p>\n</blockquote></li>\n</ul>\n"}, {'answer_id': 112781, 'author': 'Herb Caudill', 'author_id': 239663, 'author_profile': 'https://Stackoverflow.com/users/239663', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve written a macro (in Visual Studio) that allows me to not only code CSS for named colors but to easily calculate shades or blends of those colors. It also handles fonts. It fires on save and outputs a separate version of the CSS file. This is in line with <a href="http://www.w3.org/People/Bos/CSS-variables#implementation" rel="nofollow noreferrer">Bert Bos\'s argument</a> that any symbol processing in CSS take place at the point of authoring, not not at the point of interpretation. </p>\n\n<p>The full setup along with all the code would be a bit too complicated to post here, but might be appropriate for a blog post down the road. Here\'s the comment section from the macro which should be enough to get started. </p>\n\n<hr>\n\n<p>The goals of this approach are as follows:</p>\n\n<ol>\n<li><p>Allow base colors, fonts, etc. to be defined in a central location, so that an entire pallete or typographical treatment can be easily tweaked without having to use search/replace</p></li>\n<li><p>Avoid having to map the .CSS extension in IIS</p></li>\n<li><p>Generate garden-variety text CSS files that can be used, for example, by VisualStudio\'s design mode</p></li>\n<li><p>Generate these files once at authoring time, rather than recalculating them every time the CSS file is requested</p></li>\n<li><p>Generate these files instantly and transparently, without adding extra steps to the tweak-save-test workflow </p></li>\n</ol>\n\n<p>With this approach, colors, shades of colors, and font families are all represented with shorthand tokens that refer to a list of values in an XML file.</p>\n\n<p>The XML file containing the color and font definitions must be called Constants.xml and must reside in the same folder as the CSS files. </p>\n\n<p>The ProcessCSS method is fired by EnvironmentEvents whenever VisualStudio saves a CSS file. The CSS file is expanded, and the expanded, static version of the file is saved in the /css/static/ folder. (All HTML pages should reference the /css/static/ versions of the CSS files). </p>\n\n<p>The Constants.xml file might look something like this: </p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;\n&lt;cssconstants&gt;\n  &lt;colors&gt;\n    &lt;color name="Red" value="BE1E2D" /&gt;\n    &lt;color name="Orange" value="E36F1E" /&gt;\n    ...\n  &lt;/colors&gt;\n  &lt;fonts&gt;\n    &lt;font name="Text" value="\'Segoe UI\',Verdana,Arial,Helvetica,Geneva,sans-serif" /&gt;\n    &lt;font name="Serif" value="Georgia,\'Times New Roman\',Times,serif" /&gt;\n    ...\n  &lt;/fonts&gt;\n&lt;/cssconstants&gt;\n</code></pre>\n\n<p>In the CSS file, you can then have definitions like:</p>\n\n<pre><code>   font-family:[[f:Text]];\n   background:[[c:Background]]; \n   border-top:1px solid [[c:Red+.5]]; /* 50% white tint of red */\n</code></pre>\n'}, {'answer_id': 2571142, 'author': 'Giorgi', 'author_id': 239438, 'author_profile': 'https://Stackoverflow.com/users/239438', 'pm_score': 4, 'selected': False, 'text': '<p>You can achieve it and much more by using <a href="http://lesscss.org/" rel="noreferrer">Less CSS</a>.</p>\n'}]
['https://Stackoverflow.com/questions/47487', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4906/']