0

Is it possible to use variables within a CSS document, possibly by using Javascript?

For example if I have an overall variable "colorOne" assigned to "#ffffff".

Is it then possible to to call "colorOne" later? e.g. "color: colorOne;"?

I could use find and replace to replace instances but this becomes very tedious, is there an alternative?

2

6 Answers 6

4

In pure css - NO!

But you can use many tools that allow to do it and compile css for you:

  1. Less CSS - http://lesscss.org/
  2. Compass - http://compass-style.org/ (to compile SASS/SCSS http://sass-lang.com/)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use a preprocessor, for instance: http://lesscss.org/

What this enables you to do is write your CSS like this

@colorOne: #FFFFFF;

.main_links {
  color: @colorOne;
}
h2 {
  color: @colorOne;
}

and the output once it's compiled will be

.main_links {
  color: #FFFFFF;
}
h2 {
  color: #FFFFFF;
}

There are more reasons to use LESS than just assigning variables, it's powerful and allows you to do a lot more, like nested styles, have a look at the documentation: http://lesscss.org/#synopsis

Comments

2

This is not possible with pure CSS.

However, you can use a third party library, such as LESS, to achieve this:

@nice-blue: #5B83AD;

#header { color: @nice-blue; }

Comments

2

Dont forget SASS too. http://sass-lang.com

Syntatically Awesome Stylesheets.

Comments

2

You can't use variables in pure CSS, but aside from things like LESS, there is another way (not the cleanest admittedly, but it works...sort of).

Set up classes which represent your variables:

.color-black {
    color: #000000;
}

.color-red {
    color: #FF0000;
}

You can then specify multiple classes against an element:

<div class="some-class other-class color-black"></div>
<div class="some-class other-class color-red"></div>

Thereafter, whenever you change the value in color-black, or color-red, this will change the color across all elements using that class.

As I've stated, it's not the cleanest way to do things, and can lead to bloated html/css, but it can have it's uses...say for example in a testing environment if you're testing color schemes / designs.

Note: One other thing to mention is that using this method allows you to swap out classes dynamically using javascript, as you have mentioned in your question.

Example (with jQuery)

$("#MyDiv").mouseenter(function() {
    $(this).removeClass("color-red").addClass("color-black");
}).mouseleave(function() {
    $(this).removeClass("color-black").addClass("color-red");
});

Comments

1

You would have to use something like LESS:

This would allow you to use variables:

@colorOne: #5B83AD;

#header { color: @colorOne; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.