2

I am developing an application using Vuejs/Nuxtjs. I would like to import some styles from the external CSS file and would like to include those styles only to that particular Vuejs/Nuxtjs component. Hence, I tried to add the scoped and @import to my styles but this is not working for me.

Code:

<style scoped>
@import "@/assets/css/drawflow.css";

.nodeContainer{
  padding-top: 10px;
}

.node{
  border-radius: 20px;
}
</style>

I tried to search about this issue and tried some of the alternatives mentioned here such as: 1.

<style scoped src="@/assets/css/drawflow.css">
.nodeContainer{
  padding-top: 10px;
}

.node{
  border-radius: 20px;
}
</style>
<style scoped lang="css" src="@/assets/css/drawflow.css">
.nodeContainer{
  padding-top: 10px;
}

.node{
  border-radius: 20px;
}
</style>

For some reason nothing seems to work and styles are not working. If I remove the scoped from <style> then everything is working perfectly but the styles are leaking to another component as well. Can someone please let me know how to fix this issue?

*** Edited based on @kissu response ***

thanks for your suggestion and response. As mentioned in the comment on your answer, I have created the sample project in Codesandbox.

When I use it without scoped and adding all styles in a single <style> tag my application looks something like this:

https://user-images.githubusercontent.com/29374160/168595607-700c099f-5adf-4d6c-86ab-ac9885f2e659.gif

When I use the style with scoped as mentioned in your answer then my application looks something like this:

https://user-images.githubusercontent.com/29374160/168595645-60edfb85-c409-40f0-8483-39e2d60bb4a7.gif

My application is not picking the styles from drawflow.css at all when I use scoped, hence it appears something like this. Note: I have not made any other changes apart from your answer.

I hope I am able to express my issue, I am not sure what's wrong here. Can someone please help me solve this issue?

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98

1 Answers1

1

You could use CSS modules as shown here but this is probably not what you want overall.

There is also this solution but it's importing the whole thing globally (it's not scoped).


But at the end, the simplest solution is to write it like that

<style scoped src="@/assets/css/drawflow.css">
</style>

<style scoped>
.nodeContainer {
  padding-top: 10px;
}

.node {
  border-radius: 20px;
}
</style>

The CSS import will be scoped and the other style will be scoped too.

I have to say that it's more tricky than in SASS but it works well (and installing SASS for just that is not worth anyway).

kissu
  • 40,416
  • 14
  • 65
  • 133
  • Hello, really sorry I missed your notification. Actually, I have tried this approach before but for some reason, this did not work for me. To confirm again I tried this based on your answer and it's not working at all. For some reason, if I use scoped then the styles from `drawflow.css` are not at all taken into consideration. Not sure why. Can you please provide some other work-around. – BATMAN_2008 May 16 '22 at 07:02
  • I have created a sample project for the demonstration of the same. If you get a chance can you please look into this and provide me with some suggestion? https://codesandbox.io/s/cocky-matan-kvqnu?file=/pages/index.vue:8088-8093. As of now I have made the changes suggested by you and CSS values from sheet are not added, if I revert back to my code without scoped and removing 2 styles and making one then everything is working correctly. – BATMAN_2008 May 16 '22 at 07:20
  • @BATMAN_2008 I've checked your project and successfully made it work there. I'm not sure what is wrong with your project. Either you have issues with CSS in general, maybe the need to use some deep selectors. But everything looks and works perfectly on my side there. It's not the first time you do have a CSS issue but I can tell that it's not coming from here so far. Maybe it's the understanding of CSS, the usage of the devtools or the fact that your project used a non-scoped approach initially but I cannot help more here. The approach given above is the correct one, the bug is elsewhere. – kissu May 16 '22 at 09:22
  • Thanks a lot for your response. Did you make any changes to the project to make it work? Because I see that styles are not added when I am using your approach. Just wanted to know how it's working for you. – BATMAN_2008 May 16 '22 at 10:57
  • I have modified the question and added 2 GIFs, one `without scoped` and one with `scoped` to show the difference. Can you please have a look and let me know if you are seeing something similar issue in your code. Looking forward to your response. Thanks in advance. – BATMAN_2008 May 16 '22 at 12:52
  • did you get a chance to look at my explanation and any workaround that I can try. Hope you are also able to see the same issue. Can you please have a look and provide some work-around? Looking forward to your response, thank you in advance. – BATMAN_2008 May 17 '22 at 12:52