Vue.js style v-html with scoped css
Asked Answered
K

5

28

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?

Kakapo answered 30/6, 2017 at 10:54 Comment(0)
M
21

As stated in my answer here:

New version of vue-loader (from version 12.2.0) allows you to use "deep scoped" css. You need to use it that way:

<style scoped> now support "deep" selectors that can affect child components using the >>> combinator:

.foo >>> .bar { color: red; } will be compiled into:

.foo[data-v-xxxxxxx] .bar { color: red; }

More informations on the release page of vue-loader

Muscarine answered 30/6, 2017 at 15:6 Comment(4)
Awesome, thanks! It's definitely should be mentioned in the docs.Kakapo
Adding because it happened to me, if the above doesn't work, .foo /deep/ .bar might work instead.Copacetic
The above comments needs to be it's own answer, I was struggling with this until I read the comment.Autoionization
This doesn't work for me with vue-loader 15.9.1. Anyone having the same issue ?Postpaid
P
42

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; }

Postpaid answered 21/5, 2020 at 21:21 Comment(4)
None of the other answers worked, but this was perfect!Norward
Thank you so much! Only your answer helpedLyle
Thank you! Just add let more context, here is the documentation link about it: vue-loader.vuejs.org/guide/scoped-css.html#deep-selectorsStowe
::v-deep is now deprecated. You should switch to :deep(.bar) instead.Jimenez
M
21

As stated in my answer here:

New version of vue-loader (from version 12.2.0) allows you to use "deep scoped" css. You need to use it that way:

<style scoped> now support "deep" selectors that can affect child components using the >>> combinator:

.foo >>> .bar { color: red; } will be compiled into:

.foo[data-v-xxxxxxx] .bar { color: red; }

More informations on the release page of vue-loader

Muscarine answered 30/6, 2017 at 15:6 Comment(4)
Awesome, thanks! It's definitely should be mentioned in the docs.Kakapo
Adding because it happened to me, if the above doesn't work, .foo /deep/ .bar might work instead.Copacetic
The above comments needs to be it's own answer, I was struggling with this until I read the comment.Autoionization
This doesn't work for me with vue-loader 15.9.1. Anyone having the same issue ?Postpaid
V
8

Using /deep/ selector with SCSS didn't work for me but then I tried using ::v-deep selector

e.g

::v-deep a {
  color: red;
}

See this answer

Veinlet answered 29/3, 2021 at 8:44 Comment(3)
There's an SFC to change this to :deep(a). Getting a warning [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead.Varnado
@Varnado can you share the link?Veinlet
Looks like this is the one github.com/vuejs/rfcs/blob/master/active-rfcs/…Varnado
A
7

AS Sarumanatee said if the accepted answer doesn't work try:

.foo /deep/ .bar { color: red; }
Autoionization answered 2/12, 2019 at 17:50 Comment(1)
This one worked for me, "::v-deep" also. I don't know which is best ?Margy
S
0

Use :deep {}

In your case you should do:

<template>
   <div class="icon" v-html="icon"></div>
</template>

<style scoped>
.icon {
   :deep {
      svg {
         fill: red;
      }
   }
}
</style>
Socman answered 15/3 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.