Vuetify - CSS 在组件内不起作用(生效) [英] Vuetify - CSS not working (taking effect) inside component
问题描述
案例 1
我们正在尝试将自定义样式应用于呈现的 vuetify 元素:
<v-text-field v-model="name" label="Name"/></模板><样式范围>.input-group__input {背景:红色;}</风格>
但是没有任何变化.<小时>案例 2
我们正在尝试为 v-html
呈现的元素设置样式(例如外部 html).例如,我们尝试在 img
上应用自定义宽度和高度,但它不起作用:
<div v-html="customHTML"></div></模板><脚本>导出默认{数据:() =>({customHTML:`<img src="https://vuetifyjs.com/static/doc-images/carousel/planet.jpg">`;})}<样式范围>图像{宽度:200px;高度:200px;}</风格>
如何将自定义 CSS 应用于这些元素?
注意:确保按照 文档
此外,如果组件损坏,请确保您的应用程序包含在 v-app
标签中:
<v-内容>//..</v-content></v-app>
文档说:
<块引用>为了使您的应用程序正常工作,您必须将其包装在v-app
组件.
<小时>
解决方案
使用 vue-loader
的 深度选择器 >>>
像这样:
案例 1:
>>>.input-group__input {背景:红色;}
情况 2:
>>>img {宽度:200px;高度:200px;}
因此不需要从 标签中删除
scoped
属性.
摘自 docs 关于预处理器(例如,如果您使用 ):
某些预处理器,例如 Sass,可能无法正确解析 >>>
.在这些情况下,您可以改用 /deep/
组合器 - 它是 >>>
的别名,并且工作方式完全相同.
注意:深度选择器被实现在 v12.2.0
说明
在这两种情况下,CSS 更改都不会生效,因为您尝试设置样式的元素不是组件的一部分,因此没有 data-v-xxxxxxx
属性,该属性用于使用 时当前范围(组件)中的样式元素.
当使用 scoped
属性时,我们告诉 vue 仅将 css 应用于具有 data-v-xxxxxxx
的元素,但 不嵌套 元素.因此我们需要明确地使用深度选择器.
例如,如果 #1
呈现的
看起来像这样:
//注意这里的 `data-v-61b4012e`:<div data-v-61b4012e class="input-group input-group--text-field primary--text"><label>名称</label><div class="input-group__input">//注意这里没有 `data-v-61b4012e`<input tabindex="0" aria-label="Name" type="text">
<div class="input-group__details"></div>
如果 #2
呈现的 v-html
看起来像这样:
//注意这里的`data-v-61b4012e`//并且图像上没有 `data-v-61b4012e`<img src="https://vuetifyjs.com/static/doc-images/carousel/planet.jpg">
还是不行?
如果您尝试覆盖某些样式(内联样式)并且此解决方案不起作用,您可能需要查看更多关于CSS 特异性.
<小时>错误?
即使您正确定位并使用深度选择器,也有可能不会像您期望的那样应用样式.检查在呈现的 html 中的父元素之一上是否有相应的 data-v-xxxxxx 属性,可能会出现未应用的情况(错误),因此无法通过作用域 css 定位它.一个例子是v-select渲染的v-menu,但可能遇到其他类似的未来的错误.
Case 1
We are trying to apply custom a style to a rendered vuetify element:
<template>
<v-text-field v-model="name" label="Name" />
</template>
<style scoped>
.input-group__input {
background: red;
}
</style>
But there are no changes.
Case 2
We are trying to style elements rendered by
v-html
(e.g. external html). For example, we try to apply custom width and height on the img
, but it doesn't work:<template>
<div v-html="customHTML"></div>
</template>
<script>
export default {
data: () => ({
customHTML: `<img src="https://vuetifyjs.com/static/doc-images/carousel/planet.jpg">`;
})
}
</script>
<style scoped>
img {
width: 200px;
height: 200px;
}
</style>
How to apply custom CSS to those elements?
NOTE: Make sure to include styles as per docs
Also in case of broken components make sure that your app is wrapped inside v-app
tag:
<v-app>
<v-content>
//..
</v-content>
</v-app>
In order for your application to work properly, you must wrap it in a
v-app
component.
Solution
use vue-loader
's deep selectors >>>
like so:
Case 1:
>>>.input-group__input {
background: red;
}
Case 2:
>>>img {
width: 200px;
height: 200px;
}
So there is no need to remove scoped
attribute from <style>
tag.
Excerpt from docs with regards to pre-processors (for example if you are using <style lang="scss" scoped>
):
Some pre-processors, such as Sass, may not be able to parse
>>>
properly. In those cases you can use the/deep/
combinator instead - it's an alias for>>>
and works exactly the same.
Note: deep-selectors were implemented in v12.2.0
Explanation
In both cases CSS changes are not taking effect because elements you are trying to style are not part of your component, and thus don't have data-v-xxxxxxx
attribute, which is used for styling elements in the current scope (component) when using <style scoped>
.
When using scoped
attribute we tell the vue to apply css only to elements with data-v-xxxxxxx
, but not nested elements. Thus we need to explicitly use deep-selectors.
For example, in case #1
rendered <v-text-field>
will look like so:
// notice `data-v-61b4012e` here:
<div data-v-61b4012e class="input-group input-group--text-field primary--text">
<label>Name</label>
<div class="input-group__input"> // and notice no `data-v-61b4012e` here
<input tabindex="0" aria-label="Name" type="text">
</div>
<div class="input-group__details"></div>
</div>
And in case #2
rendered v-html
looks like this:
<div data-v-61b4012e> // notice `data-v-61b4012e` here
// and no `data-v-61b4012e` on image
<img src="https://vuetifyjs.com/static/doc-images/carousel/planet.jpg">
</div>
Still not working?
If you are trying to override some style (inline-style) and this solution did not work, you might want to see more about CSS specificity.
Bugs?
It's possible that even if you target properly and use deep-selectors that style is not applied as you would expect. Check if there is coresponding data-v-xxxxxx property on one of the parent elements in rendered html, there might be cases (bugs) when it's not applied, so then there is no way to target it via scoped css. One example was v-menu rendered by v-select, but probably possible to encounter other similar bugs in the future.
这篇关于Vuetify - CSS 在组件内不起作用(生效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!