You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add simple support for CSS modules
* add test case
* add README
* add static class replacement
* add styles entry in script
* update README
* update README
* improve test case
* change test case
* remove class replacement
* update README
* remove unused codes
* add support for preprocessor
* improve test case
* add default module support
* update docs
[CSS Modules](https://github.com/css-modules/css-modules) aims to solve class & animation name conflicts. It replaces all the local names with unique hashes and provides a name-to-hash map. So you can write short and general names without worrying any conflict!
4
+
5
+
With vue-loader, you can simply use CSS Modules with `<style module>`.
6
+
7
+
The name-to-hash map `$style` will be injected as a computed property.
8
+
9
+
Example:
10
+
11
+
```html
12
+
<stylemodule>
13
+
.red { color: red; }
14
+
/*
15
+
becomes
16
+
._8x_KsHmyrocTNd7akA_LL { color: red; }
17
+
*/
18
+
</style>
19
+
20
+
<template>
21
+
<h2v-bind:class="$style.red"></h2>
22
+
</template>
23
+
24
+
<script>
25
+
exportdefault {
26
+
ready() {
27
+
console.log(this.$style.red)
28
+
// => _8x_KsHmyrocTNd7akA_LL
29
+
}
30
+
}
31
+
</script>
32
+
```
33
+
34
+
If you need mutiple `<style>` tags with `module` (or you hate `$style` being injected), you can specify the module name with `<style module="moduleName">`. `moduleName` will get injected instead.
35
+
36
+
Example:
37
+
38
+
```html
39
+
<stylemodule="foo"src="..."></style>
40
+
<stylemodule="bar"src="..."></style>
41
+
42
+
<template>
43
+
<h2v-bind:class="foo.red"></h2>
44
+
<h2v-bind:class="bar.red"></h2>
45
+
</template>
46
+
```
47
+
48
+
## Tips
49
+
50
+
1. Animation names also get transformed. So, it's recommended to use animations with CSS modules.
51
+
52
+
2. You can use `scoped` and `module` together to avoid problems in descendant selectors.
53
+
54
+
3. Use `module` only (without `scoped`), you are able to style `<slot>`s and children components. But styling children components breaks the principle of components. You can put `<slot>` in a classed wrapper and style it under that class.
55
+
56
+
4. You can expose the class name of component's root element for theming.
Copy file name to clipboardExpand all lines: docs/en/features/scoped-css.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,4 +48,4 @@ Into the following:
48
48
49
49
4.**Scoped styles do not eliminate the need for classes**. Due to the way browsers render various CSS selectors, `p { color: red }` will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in `.example { color: red }`, then you virtually eliminate that performance hit. [Here's a playground](http://stevesouders.com/efws/css-selectors/csscreate.php) where you can test the differences yourself.
50
50
51
-
5.**Be careful with descendant selectors in recursive components!** For a CSS rule with the selector `.a .b`, if the element that matches `.a` contains a recursive child component, then all `.b` in that child component will be matched by the rule.
51
+
5.**Be careful with descendant selectors in recursive components!** For a CSS rule with the selector `.a .b`, if the element that matches `.a` contains a recursive child component, then all `.b` in that child component will be matched by the rule. To avoid class name conflicts, you can use [CSS Modules](features/css-modules.md).
0 commit comments