In my case the reason was trivial, but actually took a lot of time to find.

Styles were not working, because I was putting them in the styles attribute under test section.

Make sure you add styles under build section.

Answer from Bartosz546 on Stack Overflow
Discussions

Module resolution used to load styles from `angular.json` doesn't work with package `exports`
Per this discussion: #16980 (comment) module resolution currently works for entries in the angular.json styles array only if the style file is declared as the package's main entry, but doesn't currently support package exports. This is ultimately problematic for working in a Yarn pnp environment. More on github.com
🌐 github.com
4
July 14, 2022
angular-cli.json styles/scripts block do not load css/js file
OS? angular-cli: 1.0.0-beta.18 node: 6.9.0 os: linux x64 Versions. angular-cli: 1.0.0-beta.18 node: 6.9.0 os: linux x64 Repro steps. add bootstrap css file like this "styles": [ "../... More on github.com
🌐 github.com
22
October 23, 2016
How to include custom styles with Angular CLI - Stack Overflow
This however does not work... Kindly advise! ... As per your folder structure, correct path is assets/css/styles.css so change it in your angular-cli.json file as below and check it. More on stackoverflow.com
🌐 stackoverflow.com
angular-cli how to add global styles? - Stack Overflow
The background color does not work on the body tag. Upon inspecting the body tag I can see that the background-color was applied but overruled by scaffolding.less:31 ... As of the beta.14 release of the CLI (which uses Angular 2.0 final), a global stylesheet can be linked inside angular-cli.json ... More on stackoverflow.com
🌐 stackoverflow.com
March 29, 2020
🌐
GitHub
github.com › angular › angular-cli › issues › 17772
Complex style configuration in angular.json not working with npm 'linked' CSS · Issue #17772 · angular/angular-cli
May 20, 2020 - add a complex style configuration to angular.json that refers the css in the package: "styles": [ "src/styles.scss", { "input": "node_modules/@company/some-package/dist/some-package.css", "bundleName": "some-package", "inject": true } ], When running ng build or ng serve the css is not bundled and as such the styles are not applied. When I refer the css directly it works:
Published   May 20, 2020
Author   DavidUrting
🌐
GitHub
github.com › angular › angular-cli › issues › 23568
Module resolution used to load styles from `angular.json` doesn't work with package `exports` · Issue #23568 · angular/angular-cli
July 14, 2022 - The request is that module resolution for loading styles (and scripts) from the angular.json is expanded to support package exports, which would allow Angular Material to adjust their ng-add to use a package-relative path instead of a node_modules relative one. Within a new project, run ng add @angular/material After completion, change the styles array in the angular.json to this:
Author   michaelfaith
🌐
GitHub
github.com › angular › angular-cli › issues › 2843
angular-cli.json styles/scripts block do not load css/js file · Issue #2843 · angular/angular-cli
October 23, 2016 - OS? angular-cli: 1.0.0-beta.18 node: 6.9.0 os: linux x64 Versions. angular-cli: 1.0.0-beta.18 node: 6.9.0 os: linux x64 Repro steps. add bootstrap css file like this "styles": [ "../node_modules/bootstrap/dist/css/bootstrap.css", "styles...
Author   axetroy
🌐
Jeffryhouser
jeffryhouser.com › index.cfm › 2018 › 11 › 6 › Why-arent-Styles-working-with-my-Angular-6-Build
Jeffry Houser's Blog: Why aren't Styles working with my Angular 6 Build?
When you use extractCSS, the files are combined into a single CSS file instead of being created in a js file. Part of the minimization process seems to be screwing up the order that files are compressed, which of course affects the final CSS and why our styles were not showing up correctly. This caused a few grumblings among my team about how borked Angular CLI is, but I still enjoy working with it.
🌐
GitHub
github.com › angular › angular-cli › issues › 15730
Angular 8 does not load the scripts and styles described in angular.json · Issue #15730 · angular/angular-cli
October 1, 2019 - I need to use bootstrap4 with Angular8, but when mapping style files and scripts in angular.json, when running the server, the files are not loaded. I know you could put the link directly in index.html, but if the oriented form is in angular.json it should be working.
Find elsewhere
🌐
GitHub
github.com › angular › angular-cli › issues › 10045
.angular-cli.json styles not working properly · Issue #10045 · angular/angular-cli
March 22, 2018 - I've got a similar issue: fonts loaded via url() do not get it into the bundle when using AOT compilation ... { "styles": [ "node_modules/material-design-iconic-font/dist/css/material-design-iconic-font.min.css", "node_modules/roboto-fontface/css/roboto/roboto-fontface.css" ] }
Top answer
1 of 14
48

As of the beta.14 release of the CLI (which uses Angular 2.0 final), a global stylesheet can be linked inside angular-cli.json under the "styles" key. This is a reference to a file relative to the src/ directory, which is style.css by default.

Leveraging this method you could:

  • Copy the global styles into src/styles.css
  • Use CSS imports to import external rules into styles.css
  • Add more global styles via the apps[0].styles property in angular-cli.json

See also Global Styles in angular-cli's wiki.

2 of 14
22

Sounds like a CSS selectivity conflict between your linked CSS file and the compiled CSS in JavaScript that Angular places as Embedded CSS in the head or your web page, but which is stored in the memory of the browser.

This is why I always recommend you stop using Angular's poorly designed embedded CSS system and use linked CSS only. To do that you need to change your Angular JSON Setting file to remove the Embedded Style sheet, then Add a Setting to Tell Angular to copy your Linked CSS to the "dist" folder so your HTML link can access it. But see below for details...it helps to understand how CSS works in Angular. It is NOT intuitive!

HOW CSS WORKS IN GOOGLE ANGULAR

For starters, none of the above answers explains what is REALLY going on in Angular's CSS system or why you might be seeing cascading problems. In an Angular project, when you add styles paths directly into the "angular.json"(newer) or "angular-cli.json"(older) settings file, Angular grabs all those CSS files, compiles them into a JavaScript file, then spits them out as embedded styles in the head of your HTML file:

This angular.json setting:

  "styles": [
    "src/styles.css",
  ],

Gets turned into HTML and added to a gigantic embedded <style> tag that JavaScript creates in the DOM of your browser:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Project</title>

<style>
  .mystyle {background:blue;}/* everything in styles.css gets dumped in here! */
</style>

<body>
...

Bad CSS design! You can only see this if you go to a browser and use F12 and look at the actual DOM and CSS angular spits out in memory in your browser. If you use component styling, the same thing happens when those components get loaded into the DOM by Angular modules, etc. They get dumped into ANOTHER embedded style element under the first one.

I'm not 100% sure the Angular guys understood what they were doing adding embedded styles like this. Any CSS in your linked styles could be hidden with these embedded style systems Angular uses if they are inserted AFTER your linked styles.

One of the things I don't like about Angular is if you place CSS IMPORT tags inside your "styles.css" file (for example to bootstrap), the compiler also grabs those and spits those out into these embedded style tags, as well. Traditionally, importing one style sheet into another was used to control cascade order as imported sheets were inserted before other styles in the parents page. We use @import to hide styles that are not supported in very old browsers, and manage large libraries of styles by function this way. Because Angular ignores all that now and jumbles all this CSS together, you're left with a gigantic embedded inline style block in the head of your "index.html" file, and its very difficult to control cascading orders this way.

Angular's style system also doesn't support skinning of websites or themes using this inline system, nor global caching of linked styles between pages which would save bandwidth. And it doesn't matter WHERE you put your style sheets in your folder structure, as people above mention. If they are referenced in the angular.json, they all get compiled into these embedded style blobs at the head of your page in an order you cannot control.

For that reason, I recommend you REMOVE ALL STYLE REFERENCES FROM "ANGULAR.JSON"! Then add them back into your "index.html" manually as links like this:

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

...then changing your angular.json settings file by removing the styles entry in your styles array path list then pasting it back into your assets array list so Angular knows where to migrate your CSS folders and files in the dist folder, like so:

        "assets": [
          "src/styles",
        ],
        "styles": [
        ],

When you now compile your Angular application, JavaScript ignores your CSS file (in terms of compiling it with the JavaScript and later building it into a <style> CSS block) and instead pushes that file as a plain vanilla resource CSS file into your "dist" folder of files. When you push that to production, your <link> CSS tag can now send that CSS file to the user's browser, which is normally how most websites deliver CSS in websites today.

Your Angular application is now using just <link> or external CSS files for styles in your index.html file rather than <styles> or embedded CSS. You can still use this internal or "embedded" Angular-managed CSS system. I am not 100% against it. In rare cases, embedded CSS was great for supporting offline web pages. Angular has a nice system for controlling and manipulating CSS this way. But it is just not traditionally how CSS was used in most caching systems the past 20 years. Using "external" or <link> CSS, your main styles are now stored in the user's "native" browser cache for much longer periods of time, saving bandwidth and rendering/paint speed in the browser.

<link> CSS gives you back complete control of your websites styling, cross-browser fixes, skins, etc. Your front-end designers also have control over it and updates are independent of kooky polyfills, preprocessing, scripting dependencies, etc. You also now gain back full control of your cascade order, which is critical if you want full control over that.

Linking external styles also has huge caching advantages over Google Angular's broken CSS system, as the browser's naturally cache all this on page refreshes or revisits. We've been using CSS this way since the 1990's so Im baffled why Google went back to an old inline style system. Linked styles are just superior for managing and caching css. We also add versioning query strings to the end of linked CSS files (/mystyles.css?v=1.2) so you can force refreshes, etc. Again to do this, REMOVE all references to CSS in the angular.json file and manually add them in as links in the head of your index.html file.

I do think you can safely use the Angular's modular or component-based styling system as long as you understand that when you lazy load or preload Angular modules those embedded style elements do get pushed down from the server to the user on every new visit or refresh of the script block. Also understand that way modern browsers work today is not how older ones worked.....new browsers do not parse embedded styles after linked ones, but honor the order you inserted them into the head of your HTML page. In other words today, the order you put them in the page could be unpredictable in Angular when it finally delivers the HTML. Cascade order is today now heavily controlled by the HTML designer, not the W3C recommendations.

I think that's the main purpose of Angular's modular styling system....to take over control of CSS away from the designer and hope they don't notice. But to be honest, its unnecessary if you use external sheets and follow basic cascading rules.

🌐
CopyProgramming
copyprogramming.com › howto › styles-in-angular-json-not-working
Json: Angular.json Not Functioning Properly for the Provided Styles
August 20, 2023 - Angular add different style to a JSON, highlight-js is of course the directive that will tell HighlightJs to do its work; ngNonBindable prevents Angular from trying to interpret the content of the textArea as Angular code; lang="javascript": you can optionally specify the language for accurate coloration; Also, don't forget to import the HighlightJsModule … ... material styles are not being applied in angular app adding styles based on json data root are not getting applied in angular cli project
🌐
GitHub
github.com › manfredsteyer › ngx-build-plus › issues › 380
Order of CSS "styles" array in angular.json is not preserved · Issue #380 · manfredsteyer/ngx-build-plus
February 2, 2023 - When building with the default angular builder, the order of CSS styles follows the order of the array above, meaning that src/styles.scss always overrides any styles added by e.g. bootstrap.min.css. But when using ngx-build-plus this order is not preserved (not sure if it's reversed or not), and the only way to have styles.scss at the bottom of the compiled output, is to put it 1st (first) on the list above.
Author   ktsangop
🌐
Pluralsight
pluralsight.com › blog › guides
Building StyleSheets Using the Angular CLI | Online Courses, Learning Paths, and Certifications - Pluralsight
August 26, 2020 - If the stylesheet you want to include happens to be an SCSS file (or another CSS preprocessor), instead of altering the angular.json file, be sure to simply import the file you need directly into your global stylesheet.
🌐
Medium
medium.com › @pedrorolo › angular-setting-angular-json-b485dedc29d7
Angular: Setting angular.json to handle multiples themes, assets and environments during build time | by Pedro Ribeiro Bastos Soares | Medium
October 25, 2024 - Create one environment for each context and use interfaces to force all of them to have the same properties. Configure angular.json. Use styles and assets key to add SCSS files to the final bundle, stylePreprocessorOptions key to tell which directory contains the theme to be used, and fileReplacements to define which environment file to be used.
🌐
GitHub
github.com › angular › angular-cli › issues › 11770
Can't use styles input/output in CLI 6+ · Issue #11770 · angular/angular-cli
August 3, 2018 - Schema validation failed with the following errors: Data path ".styles[0]" should NOT have additional properties(output). Data path ".styles[0]" should be string. Data path ".styles[0]" should match exactly one schema in oneOf. The use case is we want to have a vendor.css file for 3rd party css and a separate site.css for in-house css. These need to be 2 separate files, and generated as 2 separate <link> tags in the index.html, instead of being bundled together in one. Please port over from Angular 5, the input/output properties and functionality of styles section, so that we may generate multiple css outputs.
Published   Aug 03, 2018
🌐
Reddit
reddit.com › r/angular › what is the order of priority for styles defined in angular.json and components? if using 2 css libraries with their own typographies, how can i prioritize one over the other?
r/angular on Reddit: What is the order of priority for styles defined in angular.json and components? If using 2 css libraries with their own typographies, how can I prioritize one over the other?
December 8, 2020 -

The angular app (v9) I'm working on has a bunch of different styles included.

In angular.json, in the options.styles array, I have something like:

"styles": [
  "src/styles.sass",
  "node_modules/font-awesome/css/font-awesome.css",
  "vendor/roboto.css",
  "node_modules/dragula/dist/dragula.css",
  "node_modules/ng2-daterangepicker/assets/daterangepicker.css",
  "node_modules/@browsingagain11/my-ui-library/dist/assets/my-ui/styles/style.css"
],

And then in for example, src/styles.sass, I have a bunch of imports at top, including @angular/material/theming

Does having something lower in the styles array have priority over styles defined higher up in the array?

Also, an issue I'm running into is that the material design typography and designs are being overwritten by the typographies defined in the my-ui-library style.css file.

I wanted to keep typography of material design, so I removed the css style from the styles array and instead imported into my single component that wanted those styles: @import url('~@browsingagain11/my-ui-library/dist/assets/my-ui/styles/style.css')

But now I experience issues where things like icons from my-ui-library do not display and I believe its because styles higher up are cascading down.

Css is hard..

Edit:

Just to clarify a bit, when I include

@import url('~@browsingagain11/my-ui-library/dist/assets/my-ui/styles/style.css')

in my angular.json file styles array at the very bottom, the icons from this library end up showing. When I'm viewing source, icons and color gradients shown in ::before and ::after selectors show as expected (except font style size is a bit weird but thats another story..)

The problem of doing this addition to the styles array is the typography it introduces overwrites the typography that the site originally used (which was from material design).

If I were to instead remove the import and try and containerize it inside the component styling using:

@import url('~@browsingagain11/my-ui-library/dist/assets/my-ui/styles/style.css')

at the top of the component styles file, things like typography from this import seem to be working within this container. But, the ::before and ::after selectors are not showing at all.

It seems very convoluted using 2 different libraries that seem to overwrite each other, but we are in the process of migrating styles over and we haven't finalized designs for all components. So I'm trying to containerize styles in the specific components themselves. But they do not work as intended.

Top answer
1 of 1
2
Not really. Or better said, it doesnt depend (most of the time) in the order of the CSS files, based on my own experience. Why? Some css styles applied are (almost for sure) using “!important” so even if they are at the top or at the bottom, the rule would be overriding any other. This happens a lot when using 3rd parties CSS libraries (all trying to be !important at the same time) or when you have a chaos of styles and you go through the easy “catch this. Its !important” way of work. On the other hand, and this is something I’ve seen even in senior’s work, don’t forget the “weight” or “value”: while the selector “div .card” and “.card” could look potentially the same, it is not. The first one has a “higher value” since it is “more specific” and hence it will override the latter one even if it appears in the same file of in other imported files. Chances are that libraries (or stylesheets) are applying selectors with different “values” so,ie, in a world with just 2 stylesheets if the former declares a very specific selector1 and the latter a relaxed selector1 and at the same time the latter declares a very specific selector2 and the former a relaxed selector2...then...you would end with selector1 from former and selector2 from latter (no matter the order in the array). On the other hand remember about shadowing styles, a component style has priority over the global styles.scss and/or externals. Double check if CSS being applied in the Component is overriding any externals. My suggestion: Open Inspector and start looking your components to see what and how is overriding your desired styles. If a style is being overriden, you can try adding “!important” to that specific rule in your new stylesheet. Adding “!important” is not the proper “senior” way to do it, but once you have migrated the styles and deleted the old files, you can remove the “!important” (find and replace) and it will work perfectly. Hope it helps!