Skip to content

Commit 53174de

Browse files
committed
[docs][zh] translated scoped-css.md
1 parent ecbd57e commit 53174de

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

docs/zh/guide/scoped-css.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Scoped CSS
1+
# 有作用域的 CSS
22

3-
When a `<style>` tag has the `scoped` attribute, its CSS will apply to elements of the current component only. This is similar to the style encapsulation found in Shadow DOM. It comes with some caveats, but doesn't require any polyfills. It is achieved by using PostCSS to transform the following:
3+
`<style>` 标签有 `scoped` 属性时,它的 CSS 只作用于当前组件中的元素。这类似于 Shadow DOM 中的样式封装。它有一些注意事项,但不需要任何 polyfill。它通过使用 PostCSS 来实现以下转换:
44

55
``` html
66
<style scoped>
@@ -14,7 +14,7 @@ When a `<style>` tag has the `scoped` attribute, its CSS will apply to elements
1414
</template>
1515
```
1616

17-
Into the following:
17+
转换结果:
1818

1919
``` html
2020
<style>
@@ -28,48 +28,48 @@ Into the following:
2828
</template>
2929
```
3030

31-
## Mixing Local and Global Styles
31+
## 混用本地和全局样式
3232

33-
You can include both scoped and non-scoped styles in the same component:
33+
你可以在一个组件中同时使用有作用域和无作用域的样式:
3434

3535
``` html
3636
<style>
37-
/* global styles */
37+
/* 全局样式 */
3838
</style>
3939

4040
<style scoped>
41-
/* local styles */
41+
/* 本地样式 */
4242
</style>
4343
```
4444

45-
## Child Component Root Elements
45+
## 子组件的根元素
4646

47-
With `scoped`, the parent component's styles will not leak into child components. However, a child component's root node will be affected by both the parent's scoped CSS and the child's scoped CSS. This is by design so that the parent can style the child root element for layout purposes.
47+
使用 `scoped` 后,父组件的样式将不会渗透到子组件中。不过一个子组件的根节点会同时受其父组件有作用域的 CSS 和子组件有作用域的 CSS 的影响。这样设计是为了让父组件可以从布局的角度出发,调整其子组件根元素的样式。
4848

49-
## Deep Selectors
49+
## 深度作用选择器
5050

51-
If you want a selector in `scoped` styles to be "deep", i.e. affecting child components, you can use the `>>>` combinator:
51+
如果你希望 `scoped` 样式中的一个选择器能够作用得“更深”,例如影响子组件,你可以使用 `>>>` 操作符:
5252

5353
``` html
5454
<style scoped>
5555
.a >>> .b { /* ... */ }
5656
</style>
5757
```
5858

59-
The above will be compiled into:
59+
上述代码将会编译成:
6060

6161
``` css
6262
.a[data-v-f3f3eg9] .b { /* ... */ }
6363
```
6464

65-
Some pre-processors, such as Sass, may not be able to parse `>>>` properly. In those cases you can use the `/deep/` combinator instead - it's an alias for `>>>` and works exactly the same.
65+
有些像 Sass 之类的预处理器无法正确解析 `>>>`。这种情况下你可以使用 `/deep/` 操作符取而代之——这是一个 `>>>` 的别名,同样可以正常工作。
6666

67-
## Dynamically Generated Content
67+
## 动态生成的内容
6868

69-
DOM content created with `v-html` are not affected by scoped styles, but you can still style them using deep selectors.
69+
通过 `v-html` 创建的 DOM 内容不受作用域内的样式影响,但是你仍然可以通过深度作用选择器来为他们设置样式。
7070

71-
## Also Keep in Mind
71+
## 还有一些要留意
7272

73-
- **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](https://stevesouders.com/efws/css-selectors/csscreate.php) where you can test the differences yourself.
73+
- **CSS 作用域不能代替 class**。考虑到浏览器渲染各种 CSS 选择器的方式,当 `p { color: red }` 设置了作用域时 (即与特性选择器组合使用时) 会慢很多倍。如果你使用 class 或者 id 取而代之,比如 `.example { color: red }`,性能影响就会消除。你可以在[这块试验田](https://stevesouders.com/efws/css-selectors/csscreate.php)中测试它们的不同。
7474

75-
- **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.
75+
- **在递归组件中小心使用后代选择器!** 对选择器 `.a .b` 中的 CSS 规则来说,如果匹配 `.a` 的元素包含一个递归子组件,则所有的子组件中的 `.b` 都将被这个规则匹配。

0 commit comments

Comments
 (0)