tags:

views:

337

answers:

12

Hi,

i have a stylesheet and a lot of styles with the same border color (#CCCCCC to be precise).

Is there a way to specify some kind of variable and reuse that, so in stead of typing #CCCCCC over and over, i can type

border: 1px solid $bordercolor;

ps i'm using asp.net mvc

A: 

You could set the border style for a parent tag. For example if every element in your #content uses this border properties you could apply the desired border style to #content itself.

Ham
A: 

Extend the style within the color is defined

LE: see the sample from KennyTM

thelost
-1: no real content.
ANeves
+8  A: 

Not that I'm aware of, but you could generate your CSS from a .NET page.

Then call it with

And StyleSheet.aspx would look something like

<%@ Page Language="C#" %> 
H1 
{ 
  background-color:<%= MyColourVariable %>; 
}
Chad
Does dynamically creating a css file prevent it from being cached?
Jens
Jens, no. Caching is dependent upon mime type of the asset served, and the specific properties upon the asset the browser looks, which vary by browser, to determine if the cache is out of data.
And you can always add the page caching if you want some control over it at the server side (so it's not recreated at the server every time)http://support.microsoft.com/kb/308375
Chad
Aha, why generate all my pages and not my stylesheet? Good thinking, and maintainable too
Michel
+21  A: 
.classA, .classB, .classC {
   border-color: #CCC;
}

.classA {
   border-width: 1px;
   border-style: solid;
}

...

But you can't use the short-hand syntax to define the border anymore.

KennyTM
Nice, i don't actually use that, putting selectors more than once in the stylesheet. i'm seeing this as reverse css coding: not say "classA is border-color this and font that", but say "border-color this is applied to ClassA and ClassB, and font that is applied to Class C". Very nice.
Michel
I do this kind of thing regularly, if I don't want borders on all sides, i.e. `border-width: 1px 0; border-style: solid; border-color: #ccc`
DisgruntledGoat
+6  A: 

An element may have multiple classes assigned. So you could create one style that defines you border color and use it in conjunction with other classes for other attributes:

.bordercolor { border-color:#CCCCCC; }

<div class="bordercolor otherstyle">
Ray
+1  A: 

Aside from generating it from a .NET page you could use some sort of pre-processor. There are general use ones such as m4 or CSS specific ones such as LESS.

RoToRa
+3  A: 

You can serve your CSS as a php file with the type text/css. Then that way you can use all the PHP variables you want. This works in every browser. Here is an example:

http://mailmarkup.org/mmlorg.php

+6  A: 

You can't really define variables in CSS, but you can sort of do what you are after a few ways. First, you could apply multiple classes to your element and just keep color in it's own class.

.myBorderColor { border-color: #000000; }
.myOtherClass { font-weight: bold; }

<div class="myBorderColor myOtherClass">content</div>

The other alternative is to actually cascade your styles so more than 1 block is applied.

div.a { font-weight: bold; }
div.b { color: #cccccc; }

div.a div.b { border-color: #000000; }

That way you are still controlling your color from 1 place.

jaltiere
Just make sure you don't follow the style our web designer uses and name the classes "fontbold" and "borderblack"/"bordergray"/"borderblue"... That is really bad style as the names should tell how it's used, not how it looks.
dbemerlin
Using a class `.myBorderColor` is precisely what dbmerlin is refering to.
ANeves
@dbemerlin i think when i would start to apply this technique i would be tempted too to use names like borderblue and borderblack, because i think it would be easy in the beginning to use these kind of names in stead of more descriptive names. But you're right, that's not the way to go.
Michel
I agree, my naming was just to show the concept. CSS can get ugly in a hurry if you start naming things like that.
jaltiere
The catch to giving a style a name like "blackBorderBox", in case this isn't apparent, is that if you use a name like this, and then use that class everywhere that you want a box with a black border, and then later you decide that those boxes should have a blue border ... do you change the name and all the references? What a pain! Or do you leave the name and leave readers to wonder why it's called "black" when in fact it's blue. Or worse ...
Jay
(continued) What if you decide that SOME of these boxes should be changed to have a blue border? Now you have to check every reference to figure out which should be blue and which should be black. Much smarter to create classes for, say, "QuoteBox" and "CatPictureBox" and "HeadlineBox", even if they're all identical now, because tomorrow they might not be. (Yes, this lesson can be overlearned and you end up creating 600 classes for every possible use of italics, don't go crazy.)
Jay
A class name like myBorderColour is absolutely evil. Run fast.
Nicholas Wilson
+4  A: 

I think you may be referring to CSS variables. Unfortunately they are not well supported.

dnagirl
That would be great *if* it was supported ...
C. Ross
Yes! but indeed, Unfortunately...
Michel
+2  A: 

A number of folks have done HttpHandlers that add variable support to CSS. Basically, the HttpHandler takes care of replacing the variables with their values before the client sees the CSS. Examples include:

This will certainly work. I have not tried it in any of my applications so I cannot speak to performance implications.

Tom Cabanski
+5  A: 

You could also use a "higher-level" CSS. Sass and Less both offer variables. They work by writing in a language that is a superset of CSS and then you compile it to CSS when you are testing/deploying. There are hooks for RoR for both, there might be one for asp.net.

henrikh
cool. Less looks very promising.
Michel
I should mention that Sass has an alternate syntax which is similar to CSS's.
henrikh
+1  A: 

I was thinking about the T4 template too. It's Visual Studio only i think, so it's not the most generic way to go, but i thought i'd share it.

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" language="C#v3.5" debug="true" hostSpecific="true" #>
<#@ output extension=".css" #>
<# string font = " font-family: Verdana, Helvetica;\n\tfont-size: 11px;";#>

body { 
    <#= font #>
}
table.Bid {
    background-color:red;
    <#= font #>
}

This generates a test.css file with this content:

body { 
     font-family: Verdana, Helvetica;
    font-size: 11px;
}
table.Bid {
    background-color:red;
     font-family: Verdana, Helvetica;
    font-size: 11px;
}
Michel