Create a Style Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Style

Introduction

You can create a <style> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
</head>/*from   w w w  .  j a va 2  s  .c  o  m*/
<body>

<button onclick="myFunction()">create a STYLE element, and place it in the head section</button>

<script>
function myFunction() {
    var x = document.createElement("STYLE");
    var t = document.createTextNode("body {font: 40px caption;}");
    x.appendChild(t);
    document.head.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials