2

I just created a new angular2 app using ng new myapp and I'm having already an error on loading style in index.html. I just added one line in the index.html :

<link rel="stylesheet" href="./css/styles.css">

of course I have added a folder and the file in src/css/styles.css. And in chrome result shows that

localhost/:8 GET http://localhost:4200/css/styles.css in red (status = 404) not found.

Any idea what would be the error?

When clicking on ctrl + follow link inside the editor, I got the css file.

2 Answers 2

3

This approach might work but, not recommended. And, in a project generated by CLI it's highly recommended to add the styles in assets folder

Assume that the path is './assets/css/styles.css'

Make sure your path is correct

Recommended way of doing that is -

In the angular-cli.json styles array 'apps[0].styles' add the following

"styles": [
  ...
  "./assets/css/styles.css"
  ...
]

Make sure your (default style extension in .angular-cli.json) is

"defaults": {
  "styleExt": "css"
}

And rerunning with an npm start would work

UPDATE

"defaults": {"styleExt": } is for the purpose of generating components. If the style works fine without changing this, just go ahead with it. See the comments for the reason

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

4 Comments

your could add any css file into styles array, even your default ext is css or less
Yes, default ext is to generate style files when generating the component. Don't know whether that's fixed, but i had issue on compiling scss when i had the default ext as css in my project. Added that to be in the safe side. Thanks for pointing that. @TiepPhan
just by moving css foler to assets solves the issue. many thanks Saiyaff
Hope you link it using the .angular-cli.json because even if your approach (linking in the index.html) works fine, it is not recommended. You're most welcome and happy coding @KOul
0

just ng new then for development run ng serve, for production run ng build or ng build --prod, Angular CLI will inject css, js external link for you.

you don't need to specify css link in index.html

2 Comments

I need to inject my own css file. My question is why I do have this error loading message ?
put your css file into assets folder, not src.

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.