PHP前端开发

vue中css如何引用变量

百变鹏仔 3周前 (09-25) #VUE
文章标签 变量
在 vue.js 中引用 css 变量有两种方法:使用模板字符串,通过 var(--变量名) 语法引用变量。通过 标签的 :root 选择器,定义变量到文档根元素上。

Vue 中引用 CSS 变量

在 Vue.js 中引用 CSS 变量的方法有两种:

1. 使用模板字符串

这种方法使用模板字符串语法来动态引用 CSS 变量。语法如下:

立即学习“前端免费学习笔记(深入)”;

<style>.box {  width: var(--box-width);  height: var(--box-height);}</style>

在这里,--box-width 和 --box-height 是 CSS 变量。

2. 使用 标签的 :root 选择器

这种方法使用 :root 选择器将 CSS 变量定义到文档根元素上。语法如下:

<style>:root {  --box-width: 100px;  --box-height: 100px;}</style>

之后,可以在 Vue 组件中使用这些变量,就像在普通的 CSS 中一样:

<template><div class="box"></div></template>

注意事项: