11

I have a file that I need to use in another css file.

For that I have added this to angular.json file:

   "styles": ["src/assets/css/theme.scss"],

Then I try to use it in a css file:

@import "theme.scss";

Angular can not find this path.

2
  • 1
    You need to reference the right path when you import the file. If the theme.scss is in the same folder as the css file, then you can use @Import './theme.scss'; otherwise (if for example is one level above): @import '../theme.scss'; Commented Dec 19, 2019 at 13:14
  • you don't need to "import" the file Commented Mar 17, 2023 at 21:43

2 Answers 2

12

If you want to point to a css/scss file within a component, you can add a shortcut to your styles folder in your angular.json in the options node like so :

        "my-app": {
            "root": "...",
            "sourceRoot": "...src",
            "projectType": "application",
            "architect": {

                "build": {
                    [...]
                    "options": {
                        [...]
                        "stylePreprocessorOptions": {
                          "includePaths": [
                            "projects/my-app/src/styles" // path to your styles folder
                          ]
                        }
                    }
                    
                },

            }
        },

Then, whenever you use @import "...";, it will start from the css folder, so you can use @import "theme.scss";

Sign up to request clarification or add additional context in comments.

3 Comments

+1 this is a good answer, but it doesn't point out that the source code for theme.scss will be moved to the /dist folder during the build, and people can then download the source code from the web server. The source code is never used, because SASS has imported it into the JavaScript bundle. So use the above option, but move your code away from your assets folder. That folder is for static files.
I get error, that not allowed stylePreprocessorOptions
@OPV, it should be added at the same level as "options". I'll fix my answer
5

go to angular.json and include "styles": ["../assets/css/theme.scss","styles.css"],

Comments

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.