12
<template>
  <div class="container">
    <head>
      <link rel="stylesheet" href="~assets/css/style-light.css" />
      <link rel="stylesheet" href="~assets/css/login-light.css" />
    </head>
  </div>
</template>

Importing css like above results in this error

vue.runtime.esm.js:5717 GET http://localhost:3000/~assets/css/login-light.css net::ERR_ABORTED 404 (Not Found)

Is there really no other way loading css other than putting the whole css in the template?

1
  • You can only have one head element per page. It cannot reside in the body. Aside from that, it would maybe work, but the CSS does not exist at that specified path. Commented Feb 18, 2020 at 10:22

3 Answers 3

32

The first thing you need to know is, you can't declare a html head inside any place, neither in yours tamplate, neither in yours components, neither in yours pages, neither in nowhere.

Keep in mind that you can't use a html tags for this, you will use a json schema.

Now about you doubt if you want to import the CSS as globally, the correct place is inside your nuxt.config.js, inside this file, you have a property called head, and inside the head we will configure all the imports.

Nuxt2

https://v2.nuxt.com/docs/configuration-glossary/configuration-css/

So, inside nuxt.config.js find your head session, and then create new property called css, some thing like this:

...
head: {
  css: [
    '~/assets/style/app.styl',
    '~/assets/style/main.css'
  ],
}
...

In Nuxt2, you will need a CSS loader installed in your application too, so have sure you had installed a "stylus" and "stylus-loader" in your app.

[edit] add nuxt3

Nuxt3

https://nuxt.com/docs/getting-started/styling#the-css-property

In nuxt3 you can import inside the nuxt-config.ts outside of head property, directly in css property:

export default defineNuxtConfig({
...
  css: [`assets/styles/main.scss`],
...
})

In Nuxt3 with SCSS, you will need "sass" and "sass-loader" pakages, so have sure you have it installed before.

npm i D sass sass-loader

Import inside components

Another way, is import your css/scss directly inside your component or pages. This will work in both versions Nuxt2 and Nuxt3. For this you can do some thing like this:

<style lang="scss" scoped>
@import "./myCustomCss.css";
</style>
    OR
<style scoped src="./myCustomCss.css">
</style>
Sign up to request clarification or add additional context in comments.

3 Comments

<style scoped> @import '~/assets/style/main.css'; </style>
interesting, can you try this for us? <style scoped src="@/assets/styles/mystyles.css">
another thing, do you have a css loader installed? here i have this 2 packages installed: "stylus": "^0.54.7" and "stylus-loader": "^3.0.2".
4

try to import your CSS files in script like this :

<script>
import "@/assets/css/style-light.css";
import "@/assets/css/login-light.css";

/// 

</script>

5 Comments

now i got this Cannot find module '~assets/css/style-light.css'
Use @/ instead of ~
I got this instead Error: Can't resolve 'normalize.css'
sure you can @HenriqueVanKlaveren , take a look at this codesandbox.io/s/…
Using the head() function right now in Nuxt is causing weird failures for me right now on certain pages, so I'm using this method instead since it's working 100% of the time. Thank you for the tip.
2

You could bring your files in using the head method like so :

head () {
return {
  link: [
    { rel: 'stylesheet', href: '/style-light.css' },
    { rel: 'stylesheet', href: '/login-light.css' }
  ]
} 

}

You should also move these css files into the static folder. See this discussion on the Vue forum https://forum.vuejs.org/t/nuxt-import-css-file-and-js-file/42498

6 Comments

when should I use static and when should i use assets?
I think that the css files that you put into the assets folder are going to be compiled by webpack, so if you're using any pre-processor, this will be transformed into plain old css. Using the static folder webpack doesn't touch anything in this foler.
which one do you suggest if I have a bunch of pure css files that needs to be loaded in specific pages?
I suppose it really depends how many a 'bunch' is. Are these files already minified etc.. It also depends on which files will be loaded on certain pages. If it's not an enormous amount of code and it's already minified I don't think you should have a problem putting them into the static file.
You should probably look at Henriques solution, honestly I think thats a better way of doing it!
|

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.