How can I style v-html content with scoped css using vue-loader?
Simple example: component.vue
<template>
<div class="icon" v-html="icon"></icon>
</template>
<script>
export default {
data () {
return {icon: '<svg>...</svg>'}
}
}
</script>
<style scoped>
.icon svg {
fill: red;
}
</style>
generate html
<div data-v-9b8ff292="" class="icon"><svg>...</svg></div>
generate css
.info svg[data-v-9b8ff292] { fill: red; }
As you can see v-html content don't have data-v attribute, but generate css have data-v attribute for svg.
I know this is expected behavior for vue-loader (https://github.com/vuejs/vue-loader/issues/359). And in this issue descendent selectors mentioned. But as you can see I use it in my css and it's not worked.
How can I style v-html content?
With scoped , the parent component's styles will not leak into child components. However, a child component's root node will be affected by both the parent's scoped CSS and the child's scoped CSS. This is by design so that the parent can style the child root element for layout purposes.
Prerequisites: Familiarity with the core HTML, CSS, and JavaScript languages, knowledge of the terminal/command line. Vue components are written as a combination of JavaScript objects that manage the app's data and an HTML-based template syntax that maps to the underlying DOM structure.
Definition and Usage The scoped attribute is a boolean attribute. When present, it specifies that the styles only apply to this element's parent element and that element's child elements (not the entire document). Note: The scoped attribute is deprecated and will be removed.
The :scope CSS pseudo-class represents elements that are a reference point for selectors to match against. /* Selects a scoped element */ :scope { background-color: lime; } Currently, when used in a stylesheet, :scope is the same as :root , since there is not at this time a way to explicitly establish a scoped element.
I am using vue-loader 15.9.1
. The >>>
solution did not work for me (no effect) whereas the /deep/
method resulted in building errors...
Here is what worked instead:
.foo ::v-deep .bar { color: red; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With