Reputation: 133
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.
var thumbStyles = document.createElement("style");
document.head.appendChild(thumbStyles);
thumbStyles.sheet.insertRule(
"figure#styleThumbs { \
position: absolute; \
left: 0px; \
bottom: 0px; \
} \
")
thumbStyles.sheet.insertRule(
"figure#styleThumbs img { \
outline: 1px solid black; \
cursor: pointer; \
opacity: 0.75; \
} \
")
thumbStyles.sheet.insertRule(
"figure#styleThumbs img:hover { \
outline: 1px solid red; \
opacity: 1.0; \
} \
");
Picture of HTML Head with style but no style code
I also asked this question on FCC but have no answers
Upvotes: 2
Views: 257
Reputation: 89414
The inserted CSS is not displayed in the DOM, but it does work.
var thumbStyles = document.createElement("style");
document.head.appendChild(thumbStyles);
thumbStyles.sheet.insertRule(`#test{
background-color: dodgerblue;
}`);
<div id="test">
This is a test
</div>
Upvotes: 2