Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,30 @@
---
title: Inject CSS
type: snippet
language: javascript
tags: [browser,css]
cover: dark-leaves-5
dateModified: 2020-10-22T20:23:47+03:00
---
Injects the given CSS code into the current document
- Use `Document.createElement()` to create a new `style` element and set its type to `text/css`.
- Use `Element.innerText` to set the value to the given CSS string.
- Use `Document.head` and `Element.appendChild()` to append the new element to the document head.
- Return the newly created `style` element.
```js
const injectCSS = css => {
let el = document.createElement('style');
el.type = 'text/css';
el.innerText = css;
document.head.appendChild(el);
return el;
};
```
```js
injectCSS('body { background-color: #000 }');
// '<style type="text/css">body { background-color: #000 }</style>'
```