Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom css and js to angular 4

In angular 2, I could just add them in the index.html like this:

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

But in angular 4 it has to be added to the angular-cli.json file in the styles and script array section.

In my situation they don't seem to be loaded at all. No error but when I view the app in the browser, I know the styles didn't load. When I add the styles and js to the index.html file too it doesn't get loaded, I get a 404 in the console.

How do I fix this? Below is my version of Angular cli: @angular/cli : 1.0.2 Node : 6.9.2

like image 667
marvin ralph Avatar asked May 29 '17 12:05

marvin ralph


People also ask

How do I include external CSS and JS in angular 12?

Adding external CSS in Angular Project.Go to your project folder > Click on src folder > Click on assets folder > Create a folder called css > and paste your css files here.

Can we use both CSS and SCSS in angular?

With the help of Angular CLI, you can install CSS or SCSS on your project and start working on that in a suitable way. If you are working with the CSS or SCSS in your angular project then it is very easy for you as compared to most other frameworks.

How we can add external CSS related to the particular browser in angular?

You can include css and js file in your angular project using npm command or direct insert link into src/index. html.

Can I add JavaScript to CSS file?

JavaScript can also be used to load a CSS file in the HTML document.


1 Answers

If you are using angular cli then go to your .angular-cli.json and then you can get "styles":[ ] as well "scripts":[ ] inside these you can add your css files and js files

 "styles": [
            yourfirst-css-file , //comma (,)is important
            your-second-css-file
          ],

Eg:-

"styles": [
        "styles.css",
        "../node_modules/bootstrap/dist/css/bootstrap.min.css"
      ],

"scripts": [
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/tether/dist/js/tether.js",
        "../node_modules/bootstrap/dist/js/bootstrap.js"
      ],

and 404 error means that the file is not found.Try and append the complete "url" folder(src).

like image 144
Asumoon Avatar answered Oct 22 '22 16:10

Asumoon