Werecommendfollowing`.lazy.css` naming convention for lazy styles and the `.css` for basic `style-loader` usage (similar to other file types, i.e. `.lazy.less` and `.less`).
Whenyou`lazyStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
>⚠️Behaviorisundefinedwhen`unuse` is called more often than `use`.Don'tdothat.
Werecommendfollowing`.lazy.css` naming convention for lazy styles and the `.css` for basic `style-loader` usage (similar to other file types, i.e. `.lazy.less` and `.less`).
Whenyou`lazySingletonStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
>⚠️Sourcemapsdonotwork.
>⚠️Behaviorisundefinedwhen`unuse` is called more often than `use`.Don'tdothat.
Worksthesameasa[`lazyStyleTag`](#lazyStyleTag), but if the code is executed in IE6-9, turns on the [`lazySingletonStyleTag`](#lazySingletonStyleTag)mode.
>ℹ️Theloaderwilldynamicallyinsertthe`<link href="path/to/file.css" rel="stylesheet">` tag at runtime via JavaScript. You should use [MiniCssExtractPlugin](https://webpack.js.org/plugins/mini-css-extract-plugin/) if you want to include a static `<link href="path/to/file.css" rel="stylesheet">`.
Bydefault,the`style-loader` appends `<style>`/`<link>` elements to the end of the style target, which is the `<head>` tag of the page unless specified by `insert`.
Ifyoutargetan[iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement) make sure you have sufficient access rights, the styles will be injected into the content document head.
####`String`
#####`Selector`
Allowstosetupcustom[queryselector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) where styles inject into the DOM.
>⚠DonotforgetthatthiscodewillbeusedinthebrowserandnotallbrowserssupportlatestECMAfeatureslike`let`, `const`, `arrow function expression` and etc. We recommend using [`babel-loader`](https://webpack.js.org/loaders/babel-loader/) for support latest ECMA features.
>⚠DonotforgetthatsomeDOMmethodsmaynotbeavailableinolderbrowsers,werecommendeduseonly[DOMcorelevel2properties](https://caniuse.com/#search=DOM%20Core), but it is depends what browsers you want to support
**webpack.config.js**
```js
module.exports={
module:{
rules:[
{
test:/\.css$/i,
use:[
{
loader:"style-loader",
options:{
insert:require.resolve("modulePath"),
},
},
"css-loader",
],
},
],
},
};
```
Anew`<style>`/`<link>` elements will be inserted into at bottom of `body`tag.
>⚠DonotforgetthatthiscodewillbeusedinthebrowserandnotallbrowserssupportlatestECMAfeatureslike`let`, `const`, `arrow function expression`andetc,werecommenduseonlyECMA5features,butitisdependswhatbrowsersyouwanttosupport
>⚠DonotforgetthatsomeDOMmethodsmaynotbeavailableinolderbrowsers,werecommendeduseonly[DOMcorelevel2properties](https://caniuse.com/#search=DOM%20Core), but it is depends what browsers you want to support
>⚠DonotforgetthatthiscodewillbeusedinthebrowserandnotallbrowserssupportlatestECMAfeatureslike`let`, `const`, `arrow function expression`andetc,werecommenduseonlyECMA5features,butitisdependswhatbrowsersyouwanttosupport
**webpack.config.js**
```js
module.exports={
module:{
rules:[
{
test:/\.css$/i,
use:[
{
loader:"style-loader",
options:{
injectType:"styleTag",
styleTagTransform:require.resolve("module-path"),
},
},
"css-loader",
],
},
],
},
};
```
####`Function`
Transformtagandcsswheninsert'style'tagintotheDOM.
>⚠DonotforgetthatthiscodewillbeusedinthebrowserandnotallbrowserssupportlatestECMAfeatureslike`let`, `const`, `arrow function expression`andetc,werecommenduseonlyECMA5features,butitisdependswhatbrowsersyouwanttosupport
>⚠DonotforgetthatsomeDOMmethodsmaynotbeavailableinolderbrowsers,werecommendeduseonly[DOMcorelevel2properties](https://caniuse.com/#search=DOM%20Core), but it is depends what browsers you want to support
**webpack.config.js**
```js
module.exports={
module:{
rules:[
{
test:/\.css$/i,
use:[
{
loader:"style-loader",
options:{
injectType:"styleTag",
styleTagTransform:function(css,style){
// Do something ...
style.innerHTML=`${css}.modify{}\n`;
document.head.appendChild(style);
},
},
},
"css-loader",
],
},
],
},
};
```
###`base`
Thissettingisprimarilyusedasaworkaroundfor[cssclashes](https://github.com/webpack-contrib/style-loader/issues/163) when using one or more [DllPlugin](https://robertknight.me.uk/posts/webpack-dll-plugins/)'s. `base` allows you to prevent either the _app_'s css (or _DllPlugin2_'s css) from overwriting _DllPlugin1_'s css by specifying a css module id base which is greater than the range used by _DllPlugin1_ e.g.:
TherearesomecasesinwhichusingESmodulesisbeneficial,likeinthecaseof[moduleconcatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
Thiscanbeachievedbyusingthe[mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin), because it creates separate css files.
For`development` mode (including `webpack-dev-server`) you can use `style-loader`, because it injects CSS into the DOM using multiple `<style></style>`andworksfaster.
>⚠Donotusetogether`style-loader` and `mini-css-extract-plugin`.