html - 如何在 Vuetify 组件上应用自定义/覆盖 CSS?

标签 html css vue.js vuetify.js vue-material

假设我在我的 Vue 组件中添加了 Vuetify 的 v-text-field 组件,例如

<v-text-field v-model="email"name="email" type="email" color="#90C143" label="Email">

当我检查该元素时,它会生成正常的 HTML,如

<div class="v-input v-input--has-state theme--light v-text-field v-text-field--is-booted" style="">
      <div class="v-input__control">
        <div class="v-input__slot">
          <div class="v-text-field__slot">
            <label for="input-39" class="v-label theme--light" style="left: 0px; right: auto; position: absolute;">Email
            </label>
            <input name="email" id="input-39" type="email">
          </div>
          <div class="v-input__append-inner">
            <div class="v-input__icon v-input__icon--">
              <i role="button" class="v-icon notranslate v-icon--link material-icons theme--light" style="">                    
              </i>
            </div>
          </div>
        </div>
      </div>
    </div>

如果我想在不影响功能的情况下为 v-text-field 自定义整个 CSS,我必须处理什么

最佳答案

在组件中添加一个 css 类:

<style scoped>
.text-field{
    color: #90C143 !important; /* this will override the existing property applied */
    /* add whatever properties you want */
}
</style>

然后在组件中将其添加到类而不是颜色属性:

<v-text-field v-model="email"name="email" type="email" class="text-field" label="Email">

您只能使用 vuetify documentation 中给出的预定义类.

如果你想在颜色属性上使用自定义颜色,你可以设置你的 在 main.js 中的 Vuetify 对象中拥有自己的主题:

Vue.use(Vuetify)

const vuetify = new Vuetify({
  theme: {
    themes: {
      light: {
        primary: '#90C143',
        secondary: '#b0bec5',
        anchor: '#8c9eff',
      },
    },
  },
})

现在您可以在任何组件中使用指定的主题覆盖:

<v-text-field v-model="email"name="email" type="email" color="primary" label="Email">

您也可以在 app.vue 中全局应用 CSS:

<style>
/* don't use scoped css */

.theme--light.v-text-field>.v-input__control>.v-input__slot:before {
    border-color: #90C143;
}

.theme--light.v-label {
    color: #90C143;
}

.theme--light.v-input:not(.v-input--is-disabled) input, .theme--light.v-input:not(.v-input--is-disabled) textarea {
    color: #90C143;
}
</style>

关于html - 如何在 Vuetify 组件上应用自定义/覆盖 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58461273/

相关文章:

vue.js - 在组件和 Axios 中使用时如何使数据工作?

javascript - 从 vuex 模块中监听 Action /突变

javascript - 如何为 VueJS 创建单条目 JS 文件

jquery - 如何改进包括 css 和 jquery 文件的页面加载

html - 当我有一个向左浮动的导航栏和一个向右浮动的导航栏时,如何使嵌入式 youtube 视频随页面缩小

html - 在输入字段上禁用浏览器自动填充(所有浏览器)

css - 指向模板中的自定义设置

javascript - instagram 喜欢将鼠标悬停在图像上时显示的计数

jquery - 如何在使用 jQuery 时控制动画速度

html - Github 的 electron 不允许嵌入标签?