I am trying to insert a style rule directly into the head of a document.
It inserts the style element but not the code itself. No errors or anything.
JavaScript
x
23
23
1
var thumbStyles = document.createElement("style");
2
document.head.appendChild(thumbStyles);
3
thumbStyles.sheet.insertRule(
4
"figure#styleThumbs {
5
position: absolute;
6
left: 0px;
7
bottom: 0px;
8
}
9
")
10
thumbStyles.sheet.insertRule(
11
"figure#styleThumbs img {
12
outline: 1px solid black;
13
cursor: pointer;
14
opacity: 0.75;
15
}
16
")
17
thumbStyles.sheet.insertRule(
18
"figure#styleThumbs img:hover {
19
outline: 1px solid red;
20
opacity: 1.0;
21
}
22
");
23
Picture of HTML Head with style but no style code
I also asked this question on FCC but have no answers
Advertisement
Answer
The inserted CSS is not displayed in the DOM, but it does work.
JavaScript
1
5
1
var thumbStyles = document.createElement("style");
2
document.head.appendChild(thumbStyles);
3
thumbStyles.sheet.insertRule(`#test{
4
background-color: dodgerblue;
5
}`);
JavaScript
1
3
1
<div id="test">
2
This is a test
3
</div>