7

Does anyone know how to import a scoped CSS file in Vue 3?

I tried including the code in the script tag (seen below) but it just includes the CSS globally. Then I tried adding it to the src attribute for the style tag, which works, but it ignores the styles within the style tag (also seen below).

Script Tag Include

<script>
import '@/assets/sass/styles.sass'
export default {   
...

Style Tag Include

<style lang="sass" src="@/assets/sass/styles.sass" scoped>
/* wrapper style ignored if I include src */
#wrapper   
...
0

5 Answers 5

9

Ok, was able to figure it out right after posting the question. For anyone else having this issue, you can simply call import within your style tag and it will maintain scoped status. Example below ...

<style lang="sass" scoped>
import '@/assets/sass/styles.sass'
/* wrapper is now recognized */
#wrapper   
...
Sign up to request clarification or add additional context in comments.

Comments

7

you try for css

<style lang="css" scoped src="../../../public/css/company/style.css"></style>

1 Comment

3

@Gnoll Team's answer did not work for me, at least not with with pure css. What worked was dedicated style element with src, and on top of that you can add second style element with inline css.

<style lang="css" scoped src="./style.css"></style>
<style lang="css" scoped>
# more css here
</style>

But there is a caveat with scoped css. You can not use CSS custom properties (--*) defined in :root selector. Workaround is to use :first-child instead and make sure that <template> contains single root element.

Comments

1

So that other styles are not ignored, you can use @import to import style rules from other stylesheets. The ~@/ characters are used to identify the src root of the vue, in this case Vue 3, for other versions of Vue one should check the correct import way.

<style lang="sass" scoped>
@import '~@/assets/sass/styles.sass';

/* more sass here */
</style>

Note the caveat with scoped css explained by @jurijs-kovzels.

Comments

0

For what it's worth I didn't find what I was looking for in this post. Here's the skinny, I'm using Vitepress and I want to override some styles that are obviously scoped but I'm using a standalone override css file.

I can't seem to attach any sort of "scoped" status to the standalone css file. Also the way that it's included / imported is through a JS file. Cause I thought maybe I could declare scoped in my import.

I'll update this if I figure out how to work around this.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.