05. Class and Style Bindings Flashcards

1
Q

Binding HTML Classes

“Object Syntax”

A

We can pass an object to v-bind:class to dynamically toggle classes.

You can have multiple classes toggled by having more fields in the object.

he v-bind:class directive can also co-exist with the plain class attribute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Array Syntax

A

We can pass an array to v-bind:class to apply a list of classes:

<div></div>
data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Condicionales en las clases

A

If you would like to also toggle a class in the list conditionally, you can do it with a ternary expression:

<div></div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

With Components

A

When you use the class attribute on a custom component, those classes will be added to the component’s root element. Existing classes on this element will not be overwritten.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Binding Inline Styles

Object Syntax

A

The object syntax for v-bind: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:

It is often a good idea to bind to a style object directly so that the template is cleaner:

<div></div>

data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}

Again, the object syntax is often used in conjunction with computed properties that return objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Array Syntax

A

The array syntax for v-bind:style allows you to apply multiple style objects to the same element:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Auto-prefixing

A

When you use a CSS property that requires vendor prefixes in v-bind:style, for example transform, Vue will automatically detect and add appropriate prefixes to the applied styles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Multiple Values

A

Starting in 2.3.0+ you can provide an array of multiple (prefixed) values to a style property, for example:

<div></div>

This will only render the last value in the array which the browser supports. In this example, it will render display: flex for browsers that support the unprefixed version of flexbox.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly