-3
My goal is to create dynamic themes where I can change the colors of the classes with js, for example after a request in the database.
The example I’ll give is, a class that defines the primary color of the site, example of my css:
.bg-primary { background-color: black }
And I would like via Javascript to change the class by changing the background-color
, because it is used by various elements. This would happen after the page is already loaded, making it possible to change the theme just by clicking on a button as an example:
after the change now the class would look like this
.bg-primary { background-color: blue }
Note: I am building this on a simple site only with html/css/js with no framework for studies.
is it possible? or do I need to approach this problem in another way?
You need to make some code that changes settings, maybe with https://developer.mozilla.org/en-US/docs/Web/API/Window/Window.localStorage solve your problem. Write in . css find complicated.
– novic
Thinking here, I now understood your question better: You wanted to change the content of the class, right? As far as I know this is impossible, javascript can not change the CSS file at runtime and maybe even if it could, we would have to update the page for the browser to reload the file, so it would be something very complicated. I know it is possible to have multiple CSS files and have javascript replace the
href
from the link tag that loads the CSS file. But I never did this.– Felipe Carriel
@Felipecarriel that’s right, I really believe it is not possible to change the content of the class at runtime, I will study another way to attack this problem
– Lennon S. Bueno
I think the title of the question is wrong, since, according to the body of the question, you don’t want to change a class, but rather a CSS rule - the class stays the same
.bg-primary
. Maybe you may be complicating things unnecessarily, since that is seldom necessary.– Luiz Felipe
Check it out here https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model
– Felipe Carriel