4

I've just started using Less. I am now looking to change background color as per a value passed from the HTML file. For example in HTML file select blue color than all background set blue and if select yellow than all background set Yellow.

The mechanism is to change color for look and fill. User can change his theme as per his requirement. So when user changes the color all theme of site color should be changed. Below is how I was thinking of passing the selected color

In this snapshot Html Select blue Color

And this is how I expect the value to be assigned in the less.css file.

In this snapshot less file description

So, How can I achieve this scenario?

5
  • Modifying vars from the front-end is not recommended as per Less website. How many such color options do you have? Can't you create static classes for each color? Commented May 30, 2016 at 6:41
  • 1
    For the most part, LESS is compiled prior to rendering in the browser. So there's no communication between the HTML and LESS files once it's served to the end user. It's more likely that you'll want to use Javascript to toggle a new "theme" class when the button is pressed. Add more detail about what you're trying to accomplish (what's the mechanism for changing colors / why) and I'll see if I can help. Commented May 30, 2016 at 6:42
  • hi,harry.. there are 20 color available.. so can i make static class for each color?? Commented May 30, 2016 at 6:45
  • The mechanism to change color for look and fill. wise user can change his theme as per his requirement.. so when user change the color all theme of site color should be change. Commented May 30, 2016 at 6:49
  • hi, For Example In drop down there are multiple color available now user can change whatever color that pass in the less file and change that color in page dynamically.. than how can achieve this.. using less.. Commented May 30, 2016 at 7:01

1 Answer 1

5

I would recommend you to do something like the following:

  • Give the user a pre-defined list of theme colors to choose from (a drop-down maybe). Don't allow for entering any color they wish to, that would be a pain.
  • When the select any color append, the color name to the body as a class using jQuery/JS.
  • In Less, write the below code, compile it statically and link the compiled CSS to your page.

    body { 
      background-color: red; /* default */
      &.red {background-color: red;} 
      &.green{background-color: green;} 
      &.blue{background-color: blue;}
    }

window.onload = function() {
  document.querySelector('#color-chooser').addEventListener('change', function() {
    document.body.className = this.value;
  });
}
/* compiled CSS based on the Less code provided above */

body {
  background-color: red;
  /* default */
}
body.red {
  background-color: red;
}
body.green {
  background-color: green;
}
body.blue {
  background-color: blue;
}
<label>Select Background Color:</label>
<select id='color-chooser'>
  <option value='red'>Red</option>
  <option value='blue'>Blue</option>
  <option value='green'>Green (Hex)</option>
</select>


Alternately, if you insist on doing it dynamically via the front-end then you can use modifyVars option:

Note that I wouldn't recommend this option as the Less website itself states that it should be used only when there is no other choice.

HTML:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet/less" type="text/css" href="styles.less" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/2.7.1/less.min.js"></script>
</head>
<body>
  <label>Select Background Color:</label>
  <select id='color-chooser'>
    <option value='red'>Red</option>
    <option value='blue'>Blue</option>
    <option value='#0F0'>Green (Hex)</option>
  </select>
  <script type="text/javascript" language="javascript">
    document.querySelector('#color-chooser').addEventListener('change', function(){
      less.modifyVars({ /* this sets the color to the variable */
        '@bgcolor': this.value
      });
    });
  </script>  
</body>
</html>

styles.less:

body {
  background-color: @bgcolor;
}
@bgcolor: red;

Plunker Demo (couldn't put this into a snippet)

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

1 Comment

@RaviShah: You don't need 15 rep for accepting. That limit is only for voting. You can accept by just clicking that tick mark.

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.