I have a page which has <link>
in the header that loads the CSS named light.css
. I also have a file named dark.css
. I want a button to swap the style of the page all together (there are 40 selectors used in css file and some do not match in two files).
How can I remove reference to light.css
with JS and remove all the styles that were applied and then load dark.css
and apply all the styles from that? I can't simply reset all of the elements, since some of the styles are applied through different css files and some are dynamically generated by JS. Is there a simple, yet effective way to do that without reloading the page? Vanilla JS is preferable, however I will use jQuery for later processing anyways, so jQ is also fine.
This question is pretty old but I would suggest an approach which is not mentioned here, in which you will include both the CSS files in the HTML, but the CSS will be like
light.css
and dark.css will be like
basicall every selector in dark.css will be a child of
.dark_layout
Then all you need to do is to change the class of body element if someone selects to change the theme of the website.
And now all your elements will have the dark css once the user clicks on
#changetheme
. This is very easy to do if you are using any kind of CSS preprocessors.You can also add CSS animations for backgrounds and colors which makes the transition highly smooth.
Using jquery you can definitely swap the css file. Do this on button click.
Or as sam's answer, that works too. Here is the jquery syntax.
You can include all the stylesheets in the document and then activate/deactivate them as needed.
In my reading of the spec, you should be able to activate an alternate stylesheet by changing its
disabled
property from true to false, but only Firefox seems to do this correctly.So I think you have a few options:
Toggle
rel=alternate
Set and toggle
disabled
Toggle
media=none
You can select a stylesheet node with getElementById, querySelector, etc.
(Avoid the nonstandard
<link disabled>
. Setting HTMLLinkElement#disabled is fine though.)You can create a new link, and replace the old one with the new one. If you put it in a function, you can reuse it wherever it's needed.
The Javascript:
The HTML:
For simplicity, I used inline javascript. In production you would want to use unobtrusive event listeners.
Using jquery .attr() you can set href of your link tag .i.e
Sample code
Maybe I'm thinking too complicated, but since the accepted answer was not working for me I thought I'd share my solution as well.
Story:
What I wanted to do was to include different 'skins' of my page in the head as additional stylesheets that where added to the 'main' style and switch them by pressing a button on the page (no browser settings or stuff).
Problem:
I thought @sam's solution was very elegant but it did not work at all for me. At least part of the problem is that I'm using one main CSS file and just add others on top as 'skins' and thus I had to group the files with the missing 'title' property.
Here is what I came up with.
First add all 'skins' to the head using 'alternate':
Note that I gave the main CSS file the title='main' and all others have a class='style-skin' and no title.
To switch the skins I'm using jQuery. I leave it up to the purists to find an elegant VanillaJS version:
What it does is it iterates over all available skins, takes the (soon) active one, sets the title to 'main' and activates it. All other skins are disabled and title is removed.