我們可以在 v-bind:style 直接設置樣式:
直接添加樣式屬性
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 菜鳥教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px',background:'red' }">內聯樣式</div>
</div>
<script>
new Vue({ el: '#app', data: { activeColor: 'green', fontSize: 30 } }) </script>
</body>
</html>
效果如圖:

綁定到樣式對象
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 菜鳥教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<div v-bind:style="styleObject">綁定樣式對象</div>
</div>
<script>
new Vue({ el: '#app', data: { styleObject: { color: 'green', fontSize: '30px', background:'red' } } }) </script>
</body>
</html>
效果如圖:

多樣式綁定
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 菜鳥教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<div v-bind:style="[baseStyles, overridingStyles]">多樣式綁定</div>
</div>
<script>
new Vue({ el: '#app', data: { baseStyles: { color: 'green', fontSize: '30px', background:'red' }, overridingStyles: { 'font-weight': 'bold' } } }) </script>
</body>
</html>
效果如圖:

注意:當 v-bind:style 使用需要特定前綴的 CSS 屬性時,如 transform ,Vue.js 會自動偵測並添加相應的前綴。
