1

I am still new to CSS/Javascript/JQuery. I am defining a couple of colors as classes in a css file. Then, I want to use one of them as the background color of a HTML page:

.wf_blue { color:rgb(2, 12, 40); }
.wf_white { color:rgb(255, 252, 247); }

body{background-color: wf_blue;}

The above does not work. I tried with JQuery, but unsuccessfully. I have seen other questions on SO, but could not figure out the solution. What am I doing wrong?

4 Answers 4

3

your classes can only be used in the HTML markup like this

<body class="wf_blue">....

Alternative you can set this class with jQuery

$('body').addClass('wf_blue');

If you would like to use variables in CSS, you can take a look at SASS or LESS, which are CSS preprocessors

Sign up to request clarification or add additional context in comments.

2 Comments

When I do this, the background is not updated. It seems like I have to declare a background color in the CSS, not a color (then it works). I was wondering if it is possible to re-use a color as a background color.
No that's no how things work in CSS. Still, if you would like to reuse elements/colors take a look at the links above.
2

If you want to use variables in CSS, then you have to use an intermediate language, like less or Sass.

Comments

1

First, the CSS should be:

.wf_blue { background-color: rgb(2, 12, 40); }
.wf_white { background-color: rgb(255, 252, 247); }

In plain HTML

<body class="wf_blue">

With jQuery:

$(document).ready(function() {
  $("body").addClass("wf_blue");
});

2 Comments

This works when I define the color as a background color. I was wondering if it is possible to re-use a color as a background color. Is it?
No, color and background-color are separate. As topek says, look into SASS or LESS for reusable variables in CSS
1

If you do not want to use a javascript library (such as jquery or prototype), you can write directly: A. HTML:

 <body class="wf_blue">

B. javascript:

 document.getElementsByTagName ('body') [0]. addClass ('wf_blue');

thanks

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.