0

I have 4 different css files that are identical except they use different colour schemes i.e. red, blue, green. At the start of my program a user chooses which colour scheme they want and then the corresponding css file is chosen. Is there a way to just have 1 css file and have all colour schemes located in that file then when a user chooses a scheme have each item in the css file have the chosen colour applied to them?

I am using javaFX 8.

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Have you looked into using CSS variables? – A Haworth Sep 08 '21 at 03:50
  • Similar: [Dynamically Colored Window with CSS](https://stackoverflow.com/questions/58087164/javafx-dynamically-colored-window-with-css). Also, see the section in the Oracle tutorial: [Skinning the scene](https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/apply-css.htm#CHDGHCDG). While you could do it without multiple CSS files, depending on the app and requirements, it may be better form to provide multiple CSS files. – jewelsea Sep 08 '21 at 17:22
  • You could also consider applying [JavaFX 17's dataURI stylesheet capability](https://bugs.openjdk.java.net/browse/JDK-8267554) (if you were willing to perform a recommended upgrade from JavaFX 8). – jewelsea Sep 09 '21 at 13:37

1 Answers1

0

As A Haworth mentioned, you can probably do something with CSS variables. I don't know how javaFX works, but hopefully you can do something similar to what I wrote below. I set a CSS variable to the body and then have jQuery change that variable when the user clicks the buttons.

$(document).ready(function() {
   $("body").on("click", "#blue", function() {
     $("body").css("--main-color", "blue");
   });
   $("body").on("click", "#red", function() {
     $("body").css("--main-color", "red");
   });
});
:body {
  --main-color: blue;
}

.div-1 {
  background: var(--main-color);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div class="div-1">Div 1</div>
  <button id='blue'>Blue</button>
  <button id='red'>Red</button>
</body>
</html>