在vue中使用样式

本文介绍了在Vue.js中如何应用样式,包括使用class样式和内联样式的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用class样式

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> 
    <style>
       .red {
           color: red;
       } 
       .thin {
           font-weight: 200;
       }
       .italic {
           font-style: italic;
       }
       .active {
           letter-spacing: 0.5em;
       }
    </style>
</head>
<body>
    <div id="app">
        <!--<h1 class="red thin">这是一个h1</h1>-->

        <!--1.传递一个数组,这里的class需要用v-bind绑定-->
        <!--<h1 :class="['red', 'thin']">这是一个h1</h1>-->

        <!--在数组中使用三元表达式-->
        <!--<h1 :class="['red', 'thin', flag?'active':'']">这是一个h1</h1>-->

        <!--和以上效果相同,用对象来代替三元表达式-->
        <!--<h1 :class="['red', 'thin', {'active':flag}]">这是一个h1</h1>-->

        <!--对象的属性是类名,属性名可带引号可不带引号-->
        <h1 :class="objClass">这是一个h1</h1>
    </div>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                flag: false,
                objClass: {red: true, thin:true, italic:true, active:false}
            },
            methods: {
                
            }
        })
    </script>
</body>
</html>

2.使用内联样式

在这里插入图片描述

<body>
    <div id="app">
        <!--<h1 :style="{color:'red', 'font-weught':200}">这是一个h1</h1>-->
        <!--<h1 :style="styleClass">这是一个h1</h1>-->
        <h1 :style="[styleClass, styleClass2]">这是一个h1</h1>
    </div>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                styleClass:  {color:'red', 'font-weught':200} ,
                styleClass2: {'font-style':'italic'}        
            },
            methods: {
                
            }
        })   
    </script>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值