-2

I am trying to use a JS variable in CSS.

In JavaScript it's:

var bodybg = "#fff"

And in CSS:

body {
  background-color: bodybg;
}

How do I achieve that?

0

2 Answers 2

2

You can manipulate style with JavaScript.

For example:

<p id="p2">Hello World!</p>

<script>
 var bodybg = '#fff';
 document.getElementById("p2").style.color = bodybg;
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Who upvotes a "you can not." when there is already a duplicate vote which shows you can?
Can I change color from gradient ? Css gradient
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
0

This change body background color

var bodybg = "#fff";
document.querySelector('body').style.backgroundColor = bodybg;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.