There is nothing wrong with @ygmantell's solution but there are other SugaCube specific methods that can be used to apply CSS based styling to a story.
note: It is generally a better idea to create CSS styling rules within the Story Stylesheet area and to then to apply those rules to the relevant HTML elements you want styles, applying styling directly to an HTML element can make it harder to change that styling later.
1. Using a Passage Tag to control the styling.
If you add a Tag with a predetermined name (like known-tag for instance) then you can use a CSS selector like the follow to apply style to Passages that have been assigned it.
/* Via a known Passage Tag. */
body[data-tags="known-tag"] {
background-color: grey;
}
body[data-tags="known-tag"] a {
color: black;
}
2. Using the <<addclass>> macro to add a CSS class to a HTML element.
If you add a CSS class with a predetermined name (like known-class for instance) to a HTML element (like the html element) then you can use a CSS selector like the follow to apply style to the story.
/* Via link which assigns a known CSS class to html element. */
html.known-class body {
background-color: brown;
}
... and you could use a <<link>> macro like the following to call the <<addclass>> macro like so
<<link "Apply CSS class">>
<<addclass "html" "known-class">>
<</link>>
3. Using the Setting API to create a Theme setting.
You could use the Setting.addList() function to setup a set of CSS based themes which the player can select from, the following Javascript example should be place in the Story Javascript area and is a cut down version of one of the Setting.addList() examples.
var settingThemeNames = ["(none)", "Bright Lights"];
var settingThemeHandler = function () {
// cache the jQuery-wrapped <html> element
var $html = $("html");
// remove any existing theme class
$html.removeClass("theme-bright-lights");
// switch on the theme name to add the requested theme class
switch (settings.theme) {
case "Bright Lights":
$html.addClass("theme-bright-lights");
break;
}
};
Setting.addList("theme", {
label : "Choose a theme.",
list : settingThemeNames,
onInit : settingThemeHandler,
onChange : settingThemeHandler
}); // default value not defined, so the first array member "(none)" is used.
... you also need to create a CSS selector that targets the theme-bright-lights class like so.
/* Via a Setting which asssigns a theme CSS class to html element. */
html.theme-bright-lights body {
background-color: orange;
}
note: One advantage of using the Settings method is that the selected theme persists between readings/play-through of the story/game.