Skip to content

Commit dc98d29

Browse files
committed
class and style
1 parent b47a194 commit dc98d29

File tree

1 file changed

+126
-39
lines changed

1 file changed

+126
-39
lines changed

src/guide/essentials/class-and-style.md

Lines changed: 126 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
aside: deep
3+
---
4+
15
# Class and Style Bindings
26

37
A common need for data binding is manipulating an element's class list and its inline styles. Since they are both attributes, we can use `v-bind` to handle them: we only need to calculate a final string with our expressions. However, meddling with string concatenation is annoying and error-prone. For this reason, Vue provides special enhancements when `v-bind` is used with `class` and `style`. In addition to strings, the expressions can also evaluate to objects or arrays.
@@ -14,16 +18,18 @@ We can pass an object to `:class` (short for `v-bind:class`) to dynamically togg
1418

1519
The above syntax means the presence of the `active` class will be determined by the [truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) of the data property `isActive`.
1620

17-
You can have multiple classes toggled by having more fields in the object. In addition, the `:class` directive can also co-exist with the plain `class` attribute. So given the following template:
21+
You can have multiple classes toggled by having more fields in the object. In addition, the `:class` directive can also co-exist with the plain `class` attribute. So given the following state:
1822

19-
```vue-html
20-
<div
21-
class="static"
22-
:class="{ active: isActive, 'text-danger': hasError }"
23-
></div>
23+
<div class="composition-api">
24+
25+
```js
26+
const isActive = ref(true)
27+
const hasError = ref(false)
2428
```
2529

26-
And the following data:
30+
</div>
31+
32+
<div class="options-api">
2733

2834
```js
2935
data() {
@@ -34,6 +40,17 @@ data() {
3440
}
3541
```
3642

43+
</div>
44+
45+
And the following template:
46+
47+
```vue-html
48+
<div
49+
class="static"
50+
:class="{ active: isActive, 'text-danger': hasError }"
51+
></div>
52+
```
53+
3754
It will render:
3855

3956
```vue-html
@@ -44,10 +61,19 @@ When `isActive` or `hasError` changes, the class list will be updated accordingl
4461

4562
The bound object doesn't have to be inline:
4663

47-
```vue-html
48-
<div :class="classObject"></div>
64+
<div class="composition-api">
65+
66+
```js
67+
const classObject = reactive({
68+
active: true,
69+
'text-danger': false
70+
})
4971
```
5072

73+
</div>
74+
75+
<div class="options-api">
76+
5177
```js
5278
data() {
5379
return {
@@ -59,12 +85,30 @@ data() {
5985
}
6086
```
6187

62-
This will render the same result. We can also bind to a [computed property](./computed) that returns an object. This is a common and powerful pattern:
88+
</div>
6389

6490
```vue-html
6591
<div :class="classObject"></div>
6692
```
6793

94+
This will render the same result. We can also bind to a [computed property](./computed) that returns an object. This is a common and powerful pattern:
95+
96+
<div class="composition-api">
97+
98+
```js
99+
const isActive = ref(true)
100+
const error = ref(null)
101+
102+
const classObject = computed(() => ({
103+
active: isActive.value && !error.value,
104+
'text-danger': error.value && error.value.type === 'fatal'
105+
}))
106+
```
107+
108+
</div>
109+
110+
<div class="options-api">
111+
68112
```js
69113
data() {
70114
return {
@@ -82,14 +126,27 @@ computed: {
82126
}
83127
```
84128

129+
</div>
130+
131+
```vue-html
132+
<div :class="classObject"></div>
133+
```
134+
85135
### Array Syntax
86136

87137
We can pass an array to `:class` to apply a list of classes:
88138

89-
```vue-html
90-
<div :class="[activeClass, errorClass]"></div>
139+
<div class="composition-api">
140+
141+
```js
142+
const activeClass = ref('active')
143+
const errorClass = ref('text-danger')
91144
```
92145

146+
</div>
147+
148+
<div class="options-api">
149+
93150
```js
94151
data() {
95152
return {
@@ -99,6 +156,12 @@ data() {
99156
}
100157
```
101158

159+
</div>
160+
161+
```vue-html
162+
<div :class="[activeClass, errorClass]"></div>
163+
```
164+
102165
Which will render:
103166

104167
```vue-html
@@ -121,26 +184,22 @@ However, this can be a bit verbose if you have multiple conditional classes. Tha
121184

122185
### With Components
123186

124-
> This section assumes knowledge of [Vue Components](/guide/essentials/component-basics). Feel free to skip it and come back later.
125-
126-
When you use the `class` attribute on a custom component with a single root element, those classes will be added to this element. Existing classes on this element will not be overwritten.
187+
> This section assumes knowledge of [Components](/guide/essentials/component-basics). Feel free to skip it and come back later.
127188
128-
For example, if you declare this component:
189+
When you use the `class` attribute on a component with a single root element, those classes will be added to the component's root element, and merged with any existing class already on it.
129190

130-
```js
131-
const app = Vue.createApp({})
191+
For example, if we have a component named `my-component` with the following template:
132192

133-
app.component('my-component', {
134-
template: `<p class="foo bar">Hi!</p>`
135-
})
193+
```vue-html
194+
<!-- child component template -->
195+
<p class="foo bar">Hi!</p>
136196
```
137197

138198
Then add some classes when using it:
139199

140200
```vue-html
141-
<div id="app">
142-
<my-component class="baz boo"></my-component>
143-
</div>
201+
<!-- when using the component -->
202+
<my-component class="baz boo"></my-component>
144203
```
145204

146205
The rendered HTML will be:
@@ -164,20 +223,20 @@ When `isActive` is truthy, the rendered HTML will be:
164223
If your component has multiple root elements, you would need to define which component will receive this class. You can do this using `$attrs` component property:
165224

166225
```vue-html
167-
<div id="app">
168-
<my-component class="baz"></my-component>
169-
</div>
226+
<!-- my-component template using $attrs -->
227+
<p :class="$attrs.class">Hi!</p>
228+
<span>This is a child component</span>
170229
```
171230

172-
```js
173-
const app = Vue.createApp({})
231+
```vue-html
232+
<my-component class="baz"></my-component>
233+
```
174234

175-
app.component('my-component', {
176-
template: `
177-
<p :class="$attrs.class">Hi!</p>
178-
<span>This is a child component</span>
179-
`
180-
})
235+
Will render:
236+
237+
```html
238+
<p class="baz">Hi!</p>
239+
<span>This is a child component</span>
181240
```
182241

183242
You can learn more about component attribute inheritance in [Non-Prop Attributes](/guide/components/attrs.html) section.
@@ -188,10 +247,17 @@ You can learn more about component attribute inheritance in [Non-Prop Attributes
188247

189248
The object syntax for `:style` is pretty straightforward - it looks almost like CSS, except it's a JavaScript object. You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names:
190249

191-
```vue-html
192-
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
250+
<div class="composition-api">
251+
252+
```js
253+
const activeColor = ref('red')
254+
const fontSize = ref(30)
193255
```
194256

257+
</div>
258+
259+
<div class="options-api">
260+
195261
```js
196262
data() {
197263
return {
@@ -201,12 +267,27 @@ data() {
201267
}
202268
```
203269

204-
It is often a good idea to bind to a style object directly so that the template is cleaner:
270+
</div>
205271

206272
```vue-html
207-
<div :style="styleObject"></div>
273+
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
274+
```
275+
276+
It is often a good idea to bind to a style object directly so that the template is cleaner:
277+
278+
<div class="composition-api">
279+
280+
```js
281+
const styleObject = reactive({
282+
color: 'red',
283+
fontSize: '13px'
284+
})
208285
```
209286

287+
</div>
288+
289+
<div class="options-api">
290+
210291
```js
211292
data() {
212293
return {
@@ -218,6 +299,12 @@ data() {
218299
}
219300
```
220301

302+
</div>
303+
304+
```vue-html
305+
<div :style="styleObject"></div>
306+
```
307+
221308
Again, the object syntax is often used in conjunction with computed properties that return objects.
222309

223310
### Array Syntax

0 commit comments

Comments
 (0)