1

I have a Github Pages site which I made from scratch. I want to make themes that the user can choose from on the site with radio buttons. So, I made 2 different CSS Stylesheets called darkTheme.css and lightTheme.css. Normally if I had only one stylesheet, I could just link it normally in the head. In my case I have 2. I have two major questions.

<input name="theme" value="darkTheme" id="darkTheme" type="radio" checked/>
<p>Dark</p>
<br>
<input name="theme" value="darkTheme" id="darkTheme" type="radio" />
<p>Dark</p>

So, I want the theme to be dark if a variable called theme = "dark"; also, theme becomes dark when the first radio button is checked. And then vise-versa.

  • Does it help? http://stackoverflow.com/questions/11833759/add-stylesheet-to-head-using-javascript-in-body – Dmytro Vyprichenko May 31 '15 at 20:16
  • 1
    Could you not simply have one stylesheet, with both sets of styles, that follow a pattern such as `body.dark selector`, `body selector`, and add/remove a dark class on the body? – verism May 31 '15 at 20:17
  • Well, I thought of that except there are like tons of things and I already have the css file finished. –  May 31 '15 at 20:22

2 Answers2

0

Instead of physically swapping CSS stylesheets from the head, why not create a global CSS selector for each of the themes within your stylesheet?

You can use onClick or onChange events on the radio buttons to know when they've been changed.

You can then use jQuery's addClass and removeClass functions to alter the class of the highest up (in terms of nesting) element that needs restyling, in my example I use the body tag. This will allow for dynamic changing of CSS.

$(document).ready(function() {
    $('input[type=radio][name=theme]').change(function() {
        if (this.value == 'darkTheme') {
            $('body').removeClass('light');
            $('body').addClass('dark');
        }
        else if (this.value == 'lightTheme') {
            $('body').removeClass('dark');
            $('body').addClass('light');
        }
    });
});

The styling is especially easy when you have SCSS or SASS for nesting:

.dark {
  h1 {
     color: black;
  }
  /* Dark Styles */
}

.light {
  h1 {
    color: grey;
  }
  /* Light Styles */
}

But standard CSS is fine too:

.dark h1 {
  color: black;
}

.light h1 {
  color: grey;
}

The second you change the class on the DOM element the browser will trigger a rerender and all of your theme styles will waterfall down and take effect.

This technique also allows you the freedom to put all of the selector stuff for each theme in its own css file (dark.css and light.css) and then you can simply import both in the head instead of swapping them.

All stylesheets in the head compile down to one set of styles for the browser anyways. The styles from each stylesheet will only be enabled when the correct class selector is on (in my example) the body tag.

Johnny
  • 53
  • 1
  • 6
0

Try using an alternate stylesheet.

<link rel="stylesheet" href="theme1.css" />
<link rel="alternate stylesheet" href="theme2.css" />

The switch between what is the stylesheet and what is the alternate stylesheet.

OneStig
  • 868
  • 2
  • 11
  • 24