如何在 Vue 文件中使用内链 CSS 样式
Vue 3 有一种方便的方法来本地化组件中的 CSS。那就是使用 <style scoped>
,您无需拥有一个大型 CSS 文件或多个 CSS 文件即可使您的网站看起来更漂亮。 只需将 CSS 放入 <style scoped>
标签,CSS 将应用于该组件。
app.vue
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<HelloWorld msg="Welcome to Your Vue.js App" />
</template>
<script>
import HelloWorld from "./components/HelloWorld.vue";
export default {
name: "App",
components: {
HelloWorld,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
HelloWorld.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<p class="text">This text is in a component with a {{ html }}</p>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
html: `<style scoped>`,
};
},
props: {
msg: String,
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
.text {
color: pink;
}
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: 如何使用 Vue 读取文件
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论