question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scoped styles don't apply to v-html injections

See original GitHub issue

Version

11.0.0

Reproduction link

https://github.com/foundryspatial-duncan/vue-loader-issue-demo/blob/master/src/App.vue

Steps to reproduce

  1. In a single-file Vue component (.vue file), add some scoped styles (with the scoped property on the style tag).
  2. Inject some HTML that matches your scoped style’s selector with v-html

What is expected?

The styles would affect the injected HTML

What is actually happening?

The styles do not affect the injected HTML


Looks like HTML injected with v-html aren’t given the “data-v-[hash]” property (like data-v-08ce5946). I think there’s a proposal somewhere to only add the data-v-[hash] to the component’s root element and then prefix all the styles to scope them. That would solve this issue as well.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

204reactions
MikaelEdebrocommented, Mar 7, 2018

You can use the /deep/ selector to get your styles to be applied:

<div class="description" v-if="description" v-html="description"></div>

.description {
  /deep/ p {
    margin-bottom: 10px;
  }
}
17reactions
alancwoocommented, Aug 27, 2018

I stumbled upon this today and oddly, nothing was working (unscoped css, global css).

I noticed that while this solution worked, the /deep/ selector was deprecated by some time ago and throws errors in IE: https://stackoverflow.com/questions/25609678/what-do-deep-and-shadow-mean-in-a-css-selector

I then found this: https://medium.com/@brockreece/scoped-styles-with-v-html-c0f6d2dc5d8e

Which recommends something along the lines of:

.description >>> p {
  margin-bottom: 10px;
}

Which works as well, without a deprecated selector.

edit: Then again, the vue docs say >>> is just an alias for /deep/, so I’m not sure anymore https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scoped styles with v-html - Medium
Scoped styles with v-html ... Scoped css is awesome and I strongly encourage its use when building reusable components. It stops the css...
Read more >
Applying global CSS to injected HTML (v-html) - Stack Overflow
In single-file components, scoped styles will not apply to content inside v-html, because that HTML is not processed by Vue's template ...
Read more >
Styling Vue Single-File Components - This Dot Labs
Scoped styles allows us to write CSS that only applies to the component we are working in. Here's an example from the Vue...
Read more >
Built-in Directives - Vue.js
In Single-File Components, scoped styles will not apply to content inside v-html , because that HTML is not processed by Vue's template compiler....
Read more >
Understanding Vue's Deep CSS Selector - Telerik
We're going to make them loop with a v-for and render three of them per parent, ... Scoped styles apply only to this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found