Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 11, 2023 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/9557252262fa7133146b35131487fe70 to your computer and use it in GitHub Desktop.
Save code-boxx/9557252262fa7133146b35131487fe70 to your computer and use it in GitHub Desktop.
Javascript Dynamic Load CSS

JAVASCRIPT DYNAMIC CSS

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Dynamically Load CSS</title>
</head>
<body>
<!-- (A) TEST TEXT BLOCK -->
<p id="demo">FOO BAR!</p>
<input type="button" onclick="loadcss()" value="Load CSS">
<!-- (B) DYNAMICALLY LOAD CSS -->
<script>
function loadcss () {
// (B1) CREATE NEW <LINK> TAG
var stylesheet = document.createElement("link");
stylesheet.rel = "stylesheet";
stylesheet.type = "text/css";
stylesheet.href = "1b-demo.css";
// (B2) OPTIONAL - ON SUCCESSFUL LOAD & ERROR
stylesheet.onload = () => alert("CSS loaded");
stylesheet.onerror = e => alert("Error loading CSS");
// (B3) APPEND <LINK> TAG TO <HEAD>
document.head.appendChild(stylesheet);
}
</script>
</body>
</html>
#demo {
font-weight: 700;
font-size: 2em;
color: red;
}
<!DOCTYPE html>
<html>
<head>
<title>Dynamically Create CSS</title>
</head>
<body>
<!-- (A) TEST TEXT BLOCK -->
<p id="demo">FOO BAR!</p>
<input type="button" onclick="insertcss()" value="Insert CSS">
<!-- (B) DYNAMICALLY CREATE CSS -->
<script>
function insertcss () {
let stylesheet = new CSSStyleSheet();
stylesheet.insertRule("#demo { color: red; font-weight: 700; }");
document.adoptedStyleSheets = [stylesheet];
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Dynamically Replace CSS</title>
<!-- (A) CSS STYLE -->
<style>
#demo { color: blue; }
</style>
</head>
<body>
<!-- (B) TEST TEXT BLOCK -->
<p id="demo">FOO BAR!</p>
<input type="button" onclick="replacecss()" value="Replace CSS">
<!-- (C) DYNAMICALLY REPLACE CSS -->
<script>
function replacecss () {
let stylesheet = document.styleSheets[0];
stylesheet.deleteRule(0);
stylesheet.insertRule("#demo { color: red; font-weight: 700; }");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Variables</title>
<!-- (A) CSS STYLES -->
<style>
/* (A1) CSS VARIABLES */
:root {
--demo-color: black;
--demo-weight: 500;
}
/* (A2) USE CSS VARIABLES */
#demo {
color: var(--demo-color);
font-weight: var(--demo-weight);
}
</style>
</head>
<body>
<!-- (B) TEST TEXT BLOCK -->
<p id="demo">FOO BAR!</p>
<input type="button" onclick="cssvar()" value="Replace CSS Var">
<!-- (C) DYNAMICALLY CHANGE CSS VAR -->
<script>
function cssvar () {
document.documentElement.style.setProperty("--demo-color", "red");
document.documentElement.style.setProperty("--demo-weight", "700");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment